[@types/jest] Fix tsc rejects constructor arguments for Mock<T>, as it works like intended (#19268)

* Fix tsc rejects constructor arguments for Mock<T>, as it works like intended

* apply code format
This commit is contained in:
Silvio Ginter
2017-08-23 16:33:19 +02:00
committed by Andy
parent 8fa336c4a9
commit b641de08ea
2 changed files with 31 additions and 1 deletions

View File

@@ -475,7 +475,7 @@ declare namespace jest {
}
interface Mock<T> extends Function, MockInstance<T> {
new (): T;
new (...args: any[]): T;
(...args: any[]): any;
}

View File

@@ -539,6 +539,36 @@ describe('Mocks', () => {
const anotherIns: jest.Mocked<TestApi> = 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<TestLog>((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