How to use jest-when - 10 common examples

To help you get started, we’ve selected a few jest-when examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / jest-when / jest-when-tests.ts View on Github external
it('Assert the args:', () => {
      const fn = jest.fn();
      when(fn)
        .expectCalledWith(1)
        .mockReturnValue('x');
      let testPassed = false;
      try {
        fn(2); // Will throw a helpful jest assertion error with args diff
      } catch (e) {
        testPassed = true;
      }
      expect(testPassed).toBeTruthy();
    });
github Raagh / angular-karma_test-explorer / __tests__ / core / angular / angular-project-config-loader.tests.ts View on Github external
test("loadTestsFromDefaultProject return valid AngularProject from default project if angular-cli.json is present", async () => {
  // Arrange
  when((fileHelper.doesFileExists as any) as jest.Mock)
    .calledWith("angular.json")
    .mockReturnValue(false)
    .calledWith(".angular-cli.json")
    .mockReturnValue(true);
  fileHelper.readJSONFile.mockReturnValue(angularCliJsonMock.mock);
  const angularProjectConfigLoader = new AngularProjectConfigLoader(fileHelper);

  // Act
  const result = await angularProjectConfigLoader.getDefaultAngularProjectConfig("");

  // Assert
  verifyExpectedResults(result);
});
github stoplightio / spectral / src / rulesets / __tests__ / reader.unit.ts View on Github external
it('given invalid ruleset should output errors', () => {
    const flatRuleset = {
      rules: {
        'rule-1': simpleRule,
      },
    };
    givenRulesets({
      'flat-ruleset.yaml': flatRuleset,
    });
    validateRulesetMock.mockReturnValue(['fake errors']);
    when(formatAjvMock)
      .calledWith(['fake errors'])
      .mockReturnValue('fake formatted message');

    return expect(readRulesFromRulesets('flat-ruleset.yaml')).rejects.toEqual({
      messages: ['fake formatted message', "Provided ruleset 'flat-ruleset.yaml' is not valid"],
    });
  });
github DefinitelyTyped / DefinitelyTyped / types / jest-when / jest-when-tests.ts View on Github external
it('should supper verifyAllWhenMocksCalled', () => {
      const fn1 = jest.fn();
      const fn2 = jest.fn();

      when(fn1)
        .expectCalledWith(1)
        .mockReturnValue('z');
      when(fn2)
        .expectCalledWith(1)
        .mockReturnValueOnce('x');
      when(fn2)
        .expectCalledWith(1)
        .mockReturnValueOnce('y');
      when(fn2)
        .expectCalledWith(1)
        .mockReturnValue('z');

      fn1(1);
      fn2(1);
      fn2(1);
      fn2(1);

      expect(verifyAllWhenMocksCalled).not.toThrow();
    });
github DefinitelyTyped / DefinitelyTyped / types / jest-when / jest-when-tests.ts View on Github external
it('Supports compound declarations:', () => {
      const fn = jest.fn();
      when(fn)
        .calledWith(1)
        .mockReturnValueOnce('no')
        .mockReturnValue('yes');
      when(fn)
        .calledWith(2)
        .mockReturnValue('way?');
      when(fn)
        .calledWith(3)
        .mockResolvedValueOnce('no');
      when(fn)
        .calledWith(3)
        .mockResolvedValue('yes');
      when(fn)
        .calledWith(4)
        .mockRejectedValueOnce('no');
      when(fn)
        .calledWith(4)
        .mockRejectedValue('yes');

      expect(fn(1)).toEqual('no');
      expect(fn(1)).toEqual('yes');
      expect(fn(2)).toEqual('way?');
      expect(fn(3)).resolves.toEqual('no');
github stoplightio / spectral / src / rulesets / __tests__ / reader.unit.ts View on Github external
(ruleset.extends || []).forEach(extend =>
        when(resolvePathMock)
          .calledWith(key, extend)
          .mockReturnValue(extend),
      );
github Cookie-AutoDelete / Cookie-AutoDelete / __tests__ / services / CleanupService-spec.ts View on Github external
.calledWith(undefined)
        .mockReturnValue(true);
      when(Lib.undefinedIsTrue)
        .calledWith(true)
        .mockReturnValue(true);
      when(Lib.undefinedIsTrue)
        .calledWith(false)
        .mockReturnValue(false);

      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'default', 'youtube.com')
        .mockReturnValue(whiteListYoutube);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'default', 'google.com')
        .mockReturnValue(wildCardWhiteListGoogle);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'default', 'sub.google.com')
        .mockReturnValue(wildCardWhiteListGoogle);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'firefox-container-1', 'facebook.com')
        .mockReturnValue(wildCardGreyFacebook);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'default', 'examplewithcookiename.com')
        .mockReturnValue(exampleWithCookieName);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(
          sampleState,
          'default',
          'exampleWithCookieNameCleanAllCookiesTrue.com',
        )
        .mockReturnValue(exampleWithCookieNameCleanAllCookiesTrue);
      when(Lib.returnMatchedExpressionObject)
github Cookie-AutoDelete / Cookie-AutoDelete / __tests__ / services / CleanupService-spec.ts View on Github external
.mockReturnValue(false);

      when(Lib.getHostname)
        .calledWith('https://google.com/search')
        .mockReturnValue('google.com');
      when(Lib.getHostname)
        .calledWith('http://facebook.com/search')
        .mockReturnValue('facebook.com');
      when(Lib.getHostname)
        .calledWith('http://sub.domain.com')
        .mockReturnValue('sub.domain.com');

      when(Lib.extractMainDomain)
        .calledWith('google.com')
        .mockReturnValue('google.com');
      when(Lib.extractMainDomain)
        .calledWith('facebook.com')
        .mockReturnValue('facebook.com');
      when(Lib.extractMainDomain)
        .calledWith('sub.domain.com')
        .mockReturnValue('domain.com');
    });
github Cookie-AutoDelete / Cookie-AutoDelete / __tests__ / services / CleanupService-spec.ts View on Github external
beforeAll(() => {
      global.browser = {
        i18n: {
          getMessage: () => '',
        },
      };
      when(Lib.undefinedIsTrue)
        .calledWith(undefined)
        .mockReturnValue(true);
      when(Lib.undefinedIsTrue)
        .calledWith(true)
        .mockReturnValue(true);
      when(Lib.undefinedIsTrue)
        .calledWith(false)
        .mockReturnValue(false);

      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'default', 'youtube.com')
        .mockReturnValue(whiteListYoutube);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'default', 'google.com')
        .mockReturnValue(wildCardWhiteListGoogle);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'default', 'sub.google.com')
        .mockReturnValue(wildCardWhiteListGoogle);
      when(Lib.returnMatchedExpressionObject)
        .calledWith(sampleState, 'firefox-container-1', 'facebook.com')
        .mockReturnValue(wildCardGreyFacebook);
      when(Lib.returnMatchedExpressionObject)
github Cookie-AutoDelete / Cookie-AutoDelete / __tests__ / services / CleanupService-spec.ts View on Github external
.calledWith('http://facebook.com/search')
        .mockReturnValue(true);
      when(Lib.isAWebpage)
        .calledWith('http://sub.domain.com')
        .mockReturnValue(true);
      when(Lib.isAWebpage)
        .calledWith('moz-extension://test/settings/settings.html')
        .mockReturnValue(false);

      when(Lib.getHostname)
        .calledWith('https://google.com/search')
        .mockReturnValue('google.com');
      when(Lib.getHostname)
        .calledWith('http://facebook.com/search')
        .mockReturnValue('facebook.com');
      when(Lib.getHostname)
        .calledWith('http://sub.domain.com')
        .mockReturnValue('sub.domain.com');

      when(Lib.extractMainDomain)
        .calledWith('google.com')
        .mockReturnValue('google.com');
      when(Lib.extractMainDomain)
        .calledWith('facebook.com')
        .mockReturnValue('facebook.com');
      when(Lib.extractMainDomain)
        .calledWith('sub.domain.com')
        .mockReturnValue('domain.com');
    });

jest-when

An extension lib for jest

MIT
Latest version published 1 year ago

Package Health Score

59 / 100
Full package analysis

Popular jest-when functions