From b641de08eaa0aa2ff4ca691e9f9f2801b4544837 Mon Sep 17 00:00:00 2001 From: Silvio Ginter Date: Wed, 23 Aug 2017 16:33:19 +0200 Subject: [PATCH] [@types/jest] Fix tsc rejects constructor arguments for Mock, as it works like intended (#19268) * Fix tsc rejects constructor arguments for Mock, as it works like intended * apply code format --- types/jest/index.d.ts | 2 +- types/jest/jest-tests.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 396e9a0de0..3c9eb27d93 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -475,7 +475,7 @@ declare namespace jest { } interface Mock extends Function, MockInstance { - new (): T; + new (...args: any[]): T; (...args: any[]): any; } diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 7e5ed2567f..ff01b154ad 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -539,6 +539,36 @@ describe('Mocks', () => { const anotherIns: jest.Mocked = new anotherMock() as any; anotherIns.testMethod.mockImplementation(() => 1); }); + + it('jest.fn() accepts constructor arguments', () => { + interface TestLog { + log(...msg: any[]): void; + } + + class LogMock extends jest.fn((verbose?: boolean) => { + const mockLog = () => { + if (verbose) { + return jest.fn((...args) => { + const subj = args.shift() || ""; + console.log(subj, ...args); + }); + } + return jest.fn(); + }; + + return { + log: mockLog() + }; + }) { + } + + const nonVerboseLog = new LogMock(); + nonVerboseLog.log("this is completely catched by jest"); + expect(nonVerboseLog.log).toBeCalledWith("this is completely catched by jest"); + const verboseLog = new LogMock(true); + verboseLog.log("this should also be printed to the console"); + expect(verboseLog.log).toBeCalledWith("this should also be printed to the console"); + }); }); // https://facebook.github.io/jest/docs/en/expect.html#resolves