diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts new file mode 100644 index 0000000000..af62a95830 --- /dev/null +++ b/types/jest-when/index.d.ts @@ -0,0 +1,22 @@ +// Type definitions for jest-when 1.1 +// Project: https://github.com/timkindberg/jest-when#readme +// Definitions by: Alden Taylor +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +export interface PartialMockInstance { + mockReturnValue: jest.MockInstance['mockReturnValue']; +} + +export interface When { + (fn: jest.Mocked | jest.Mock): When; + // due to no-unnecessary-generics lint rule, the generics have been replaced with 'any' + // calledWith(...matchers: any[]): PartialMockInstance; + // expectCalledWith(...matchers: any[]): PartialMockInstance; + calledWith(...matchers: any[]): PartialMockInstance; + expectCalledWith(...matchers: any[]): PartialMockInstance; +} + +export const when: When; diff --git a/types/jest-when/jest-when-tests.ts b/types/jest-when/jest-when-tests.ts new file mode 100644 index 0000000000..13da65928d --- /dev/null +++ b/types/jest-when/jest-when-tests.ts @@ -0,0 +1,57 @@ +import { when } from 'jest-when'; + +describe('mock-when test', () => { + it('basic usage:', () => { + const fn = jest.fn(); + when(fn).calledWith(1).mockReturnValue('yay!'); + + const result = fn(1); + expect(result).toEqual('yay!'); + }); + + it('Supports multiple args:', () => { + const fn = jest.fn(); + when(fn).calledWith(1, true, 'foo').mockReturnValue('yay!'); + + const result = fn(1, true, 'foo'); + expect(result).toEqual('yay!'); + }); + + it('Supports jest matchers:', () => { + const fn = jest.fn(); + when(fn).calledWith( + expect.anything(), + expect.any(Number), + expect.arrayContaining([false]) + ).mockReturnValue('yay!'); + + const result = fn('whatever', 100, [true, false]); + expect(result).toEqual('yay!'); + }); + + it('Supports compound declarations:', () => { + const fn = jest.fn(); + when(fn).calledWith(1).mockReturnValue('no'); + when(fn).calledWith(2).mockReturnValue('way?'); + when(fn).calledWith(3).mockReturnValue('yes'); + when(fn).calledWith(4).mockReturnValue('way!'); + + expect(fn(1)).toEqual('no'); + expect(fn(2)).toEqual('way?'); + expect(fn(3)).toEqual('yes'); + expect(fn(4)).toEqual('way!'); + expect(fn(5)).toEqual(undefined); + }); + + 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(); + }); +}); diff --git a/types/jest-when/tsconfig.json b/types/jest-when/tsconfig.json new file mode 100644 index 0000000000..46c037813f --- /dev/null +++ b/types/jest-when/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "jest-when-tests.ts" + ] +} \ No newline at end of file diff --git a/types/jest-when/tslint.json b/types/jest-when/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jest-when/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }