mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-14 12:09:04 +08:00
[@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:
2
types/jest/index.d.ts
vendored
2
types/jest/index.d.ts
vendored
@@ -475,7 +475,7 @@ declare namespace jest {
|
||||
}
|
||||
|
||||
interface Mock<T> extends Function, MockInstance<T> {
|
||||
new (): T;
|
||||
new (...args: any[]): T;
|
||||
(...args: any[]): any;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user