HugoDF / jest-specific-argument-assert

Jest assert over single or specific argument/parameters with .toHaveBeenCalledWith and expect.anything()

Home Page:https://codewithhugo.com/jest-specific-argument-parameter-assert/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jest-specific-argument-assert

Jest assert over single or specific argument/parameters with .toHaveBeenCalledWith and expect.anything()

See it in action at https://codesandbox.io/s/github/HugoDF/jest-specific-argument-assert/tree/master/

Most of the interesting things happen in ./src/pinger.test.js.

To run the tests:

  • npm install (Node 10+, npm 6+)
  • npm test to run tests (or npm test -- --coverage to run with coverage)

Note the use of expect.anyting() to ignore certain parameters to a function that aren't asserted against for specific tests.

  describe('without search', () => {
    test('calls getPingConfigs with right accountId, searchRegex', async () => {
      await pinger(1);
      expect(mockPingConfig).toHaveBeenCalledWith(
        1,
        expect.anything(),
        expect.anything(),
        new RegExp('.*')
      );
    });
  });
  describe('offset, limit', () => {
    test('calls getPingConfigs with passed offset and limit', async () => {
      await pinger(1, { offset: 20, limit: 100 });
      expect(mockPingConfig).toHaveBeenCalledWith(
        1,
        20,
        100,
        expect.anything()
      );
    });
    test('calls getPingConfigs with default offset and limit if undefined', async () => {
      await pinger(1);
      expect(mockPingConfig).toHaveBeenCalledWith(1, 0, 50, expect.anything());
    });
  });
  describe('search', () => {
    describe('single-word search', () => {
      test('calls getPingConfigs with right accountId, searchRegex', async () => {
        await pinger(1, {}, 'search');
        expect(mockPingConfig).toHaveBeenCalledWith(
          1,
          expect.anything(),
          expect.anything(),
          new RegExp('search')
        );
      });
    });
    describe('multi-word search', () => {
      test('calls getPingConfigs with right accountId, searchRegex', async () => {
        await pinger(1, {}, 'multi word search');
        expect(mockPingConfig).toHaveBeenCalledWith(
          1,
          expect.anything(),
          expect.anything(),
          new RegExp('multi|word|search')
        );
      });
    });
  });

About

Jest assert over single or specific argument/parameters with .toHaveBeenCalledWith and expect.anything()

https://codewithhugo.com/jest-specific-argument-parameter-assert/

License:MIT License


Languages

Language:JavaScript 100.0%