Skip to content
Jest assert over single or specific argument/parameters with .toHaveBeenCalledWith and expect.anything()
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.editorconfig
.gitignore
.prettierrc
LICENSE
README.md
package-lock.json
package.json

README.md

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')
        );
      });
    });
  });
You can’t perform that action at this time.