Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#60278 [jest] Make jest.mock type-safe by @…
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieulag authored May 11, 2022
1 parent c5a1947 commit fe81c93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions types/jest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ declare namespace jest {
/**
* Mocks a module with an auto-mocked version when it is being required.
*/
function doMock(moduleName: string, factory?: () => unknown, options?: MockOptions): typeof jest;
// tslint:disable-next-line no-unnecessary-generics
function doMock<T = unknown>(moduleName: string, factory?: () => T, options?: MockOptions): typeof jest;
/**
* Indicates that the module system should never return a mocked version
* of the specified module from require() (e.g. that it should always return the real module).
Expand Down Expand Up @@ -177,7 +178,8 @@ declare namespace jest {
/**
* Mocks a module with an auto-mocked version when it is being required.
*/
function mock(moduleName: string, factory?: () => unknown, options?: MockOptions): typeof jest;
// tslint:disable-next-line no-unnecessary-generics
function mock<T = unknown>(moduleName: string, factory?: () => T, options?: MockOptions): typeof jest;

/**
* The mocked test helper provides typings on your mocked modules and even
Expand Down
6 changes: 6 additions & 0 deletions types/jest/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,18 @@ jest.autoMockOff()
.doMock('moduleName', jest.fn())
.doMock('moduleName', jest.fn(), {})
.doMock('moduleName', jest.fn(), { virtual: true })
.doMock<{animal: string}>('moduleName', () => ({animal: 'cat'}))
// $ExpectError
.doMock<{animal: string}>('moduleName', () => ({name: 'tom'}))
.dontMock('moduleName')
.enableAutomock()
.mock('moduleName')
.mock('moduleName', jest.fn())
.mock('moduleName', jest.fn(), {})
.mock('moduleName', jest.fn(), { virtual: true })
.mock<{animal: string}>('moduleName', () => ({animal: 'cat'}))
// $ExpectError
.mock<{animal: string}>('moduleName', () => ({name: 'tom'}))
.resetModules()
.isolateModules(() => {})
.retryTimes(3)
Expand Down

0 comments on commit fe81c93

Please sign in to comment.