TS2307:找不到模块“@jest/globals”或其相应的类型声明

问题描述 投票:0回答:2

我遇到错误 TS2307:在导入默认测试套件的值时找不到模块“@jest/globals”或其相应的类型声明(但成功通过了

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

describe('Example test', () => {
    test('something', () => {
        expect('').toBeFalsy()
    })
})

我的

package.json

"devDependencies": {
        "@types/jest": "^27.0.3",
        "@types/node": "^16.11.11",
        ...
        "typescript": "^4.4.4",
    },
    "optionalDependencies": {
        ...
    },
    "dependencies": {
        "@jest/globals": "^27.4.2",
        "babel-preset-jest": "^27.4.0",
        ...
    }
}

有人可以帮助我吗?

jestjs ts-jest
2个回答
2
投票

如果您使用

ts-jest
,则包版本应与其他 Jest 库相同。

通过将

@jest/globals
jest
jest-environment-jsdom
的版本降低到
29.1.1
(这是
ts-jest
的最新版本)解决了我的问题:

    "@jest/globals": "29.1.1",
    "jest": "29.1.1",
    "jest-environment-jsdom": "29.1.1",
    "ts-jest": "29.1.1"

0
投票

在我的场景中,出现相同的错误,但有一组不同的 jest 依赖项,我能够在最新版本中使用 @jest/globals,但其他 jest 依赖项必须与主要版本匹配,否则

npm ci
会出错对等依赖冲突。

将它们更改为全部相同的版本不足以修复我的错误。对我有用的是更新我的

tsconfig.json
以包含
moduleResolution

在package.json中:

...
"devDependencies": {
    ...
    "@jest/globals": "^29.7.0",
    "@types/jest": "^27.0.0",
    "jest": "^27.0.0",
    "jest-chrome": "^0.8.0",
    "ts-jest": "^27.0.0",
    ...
}

在 tsconfig.json 中:

...
"compilerOptions": {
    ...
    "module": "ES6",
    "moduleResolution": "Node",
    ...
}

"moduleResolution": "NodeNext"
也有效,但似乎需要更改“模块”值以匹配,而且我没有时间弄清楚该更改会破坏什么。

© www.soinside.com 2019 - 2024. All rights reserved.