jest: [Bug]: Interface 'JestImportMeta' incorrectly extends interface 'ImportMeta'

Version

29.0.1

Steps to reproduce

  1. add @types/jest@29.0.0 and jest@29.0.1 to a typescript project
  2. Add jest.createMockFromModule call
  3. see compiler error

Expected behavior

Don’t see any error.

Actual behavior

$ tsc --noEmit
node_modules/@jest/environment/build/index.d.ts:329:26 - error TS2430: Interface 'JestImportMeta' incorrectly extends interface 'ImportMeta'.
  The types returned by 'jest.createMockFromModule(...)' are incompatible between these types.
    Type 'unknown' is not assignable to type 'T'.
      'T' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.

329 export declare interface JestImportMeta extends ImportMeta {
                             ~~~~~~~~~~~~~~


Found 1 error in node_modules/@jest/environment/build/index.d.ts:329

Additional context

@types/jest declares as function createMockFromModule<T>(moduleName: string): T; while @jest/environment declares as createMockFromModule(moduleName: string): unknown;

Environment

System:
    OS: Linux 5.10 Ubuntu 20.04.4 LTS (Focal Fossa)
    CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
  Binaries:
    Node: 16.16.0 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.17.0 - ~/src/renovate/node_modules/.bin/npm
  npmPackages:
    jest: 29.0.1 => 29.0.1

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (19 by maintainers)

Most upvoted comments

I can confirm this works for me also with jest v29.1.0 and @types/jest v29.0.3. Thanks for the fix!

I’m getting this error with latest jest (29.0.3) and latest @types/jest (29.0.3):

node_modules/@jest/environment/build/index.d.ts:330:26 - error TS2430: Interface 'JestImportMeta' incorrectly extends interface 'ImportMeta'.
  The types returned by 'jest.requireActual(...)' are incompatible between these types.
    Type 'unknown' is not assignable to type 'TModule'.
      'TModule' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.

330 export declare interface JestImportMeta extends ImportMeta {
                             ~~~~~~~~~~~~~~


Found 1 error in node_modules/@jest/environment/build/index.d.ts:330

I fixed this by removing @types/jest, and adding imports of the globals to every test file, some variation of:

import { afterEach, beforeEach, expect, test } from '@jest/globals'

@mrazauskas after that’s released, can we add a type test here that we’re compatible?

import {jest as realJest} from '@jest/globals';

expectAssignable<typeof jest>(realJest);

or something like that?

Of course that will yell at us if we become stricter in the future, but I’d rather know and deal with that as the issues crop up

Ah nice! Would be awesome to align - makes it easier to make their types based on ours in the future 👍

Thanks! I looked though quickly lots of interesting and useful details. For instance, it would be nice if jest.requireActual<T>() would take a type arg. In my todo list, will come back to it soon.