接口“JestImportMeta”不正确地扩展了接口“ImportMeta”

ni65a41a  于 2022-12-08  发布在  Jest
关注(0)|答案(2)|浏览(184)

我从jest v27迁移到v29时遇到以下错误:

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

尝试使用tsc进行编译时会发生这个错误。
我的package.json中的相关包:

"devDependencies": {
    "@types/jest": "^29.0.0",
    "jest": "^29.0.0",
    "jest-environment-node": "^29.0.0",
    "nodemon": "^2.0.4",
    "ts-jest": "^29.0.0"
}

版本:

  • 节点Js v16.15.1
  • 每分钟v8.11.0
htzpubme

htzpubme1#

这里的问题是tsc也在构建你的测试文件,这并不理想。

溶液:

从tsconfig.json中排除测试文件,如下所示。

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2017",
        "lib": [
            "es2015"
        ],
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "bin",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*",
                "src/types/*"
            ]
        },
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "src/**/*.spec.ts",
        "src/**/*.test.ts"
    ]
}
myss37ts

myss37ts2#

这是类型中的一个错误,已在jest v29.1.0和@types/jest v29.0.3 www.example.com中修复https://github.com/facebook/jest/issues/13199#issuecomment-1260897649

相关问题