Skip to content

[Bug]: Cause ts error in jest.config.ts when import 'ts-jest' #3815

@crowcc

Description

@crowcc

Version

29.0.1

Steps to reproduce

Here's my code in jest.config.ts

import type {Config} from 'jest';
import {pathsToModuleNameMapper} from 'ts-jest';

import {compilerOptions} from './tsconfig.json';

const jestConfig: Config = {
  globals: {
    aaa: true
  }, 
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {prefix: '<rootDir>/'})
};

export default jestConfig;

Then the type declare in ts-jest causes the type of globals error:
Property ''ts-jest'' is missing in type ''{ aaa: true; }'' but required in type 'ConfigGlobals'.ts(2741)
types.d.ts(10, 13): ''ts-jest'' is declared here.

declare module '@jest/types' {
    namespace Config {
        interface ConfigGlobals {
            'ts-jest': TsJestGlobalOptions;
        }
    }
}

Expected behavior

There shouldn't be the type error when globals doesn't contain 'ts-jest' since the ts-jest config has been moved to transform.

Actual behavior

The ts type error occurs:
Property ''ts-jest'' is missing in type 'xxx'(what you write in globals) but required in type 'ConfigGlobals'.ts(2741)
types.d.ts(10, 13): ''ts-jest'' is declared here.

Debug log

Error: Jest: Failed to parse the TypeScript config file xxxxx/jest.config.ts
TSError: ⨯ Unable to compile TypeScript:
jest.config.ts:13:3 - error TS2741: Property ''ts-jest'' is missing in type '{ aaa: true; } but required in type 'ConfigGlobals'.

Additional context

No response

Environment

System:
  OS: macOS Monterey
Binaries:
  Node: 16.14.2
  pnpm: 6.24.2
npmPackages:
  jest: 29.0.1
  typescript: 4.5.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions