有人成功升级到最新的 Jest 版本吗29?
我收到错误:
Error: Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module.
Run Code Online (Sandbox Code Playgroud) 我有一个使用 TypeScript、Jest、Webpack 和 Babel 构建的 React 应用程序(不使用 Create React App)。尝试运行“yarn jest”时,出现以下错误:

我尝试删除所有软件包并重新添加它们。它没有解决这个问题。我看过类似的问题和文档,但仍然误解了一些东西。我什至按照另一个指南从头开始设置这个环境,但我的代码仍然收到这个问题。
依赖包括...
"dependencies": {
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/polyfill": "^7.6.0",
"babel-jest": "^24.9.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-test-renderer": "^16.11.0",
"source-map-loader": "^0.2.4"
},
"devDependencies": {
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-react": "^7.0.0",
"@types/enzyme": "^3.9.2",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^24.0.13",
Run Code Online (Sandbox Code Playgroud)
组件的导入行...
import * as React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import HomePage from "./components/pages";
import {
Footer,
Header,
Navigation,
} from "./components/shared";
Run Code Online (Sandbox Code Playgroud)
测试文件....
import * as React from "react"; …Run Code Online (Sandbox Code Playgroud) SyntaxError: Cannot use import statement outside a module无论我尝试过什么,我都无法摆脱这个错误,这让我非常沮丧。有大佬解决了这个问题吗?我已经阅读了一百万个 stackoverflow 和 github 问题线程。没有明确的解决方案。
这是一个 React、Typescript、Webpack 项目。我正在尝试测试一个模块。但 Jest 不会改变以某种方式模块为普通的 javascript。
/Users/me/dev/Project/project/node_modules/variables/src/variables.js:12
import './main.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
17 |
18 | */
> 19 | import { GlobalVars } from 'variables'
| ^
20 |
21 | export const Vars = new GlobalVars()
22 |
Run Code Online (Sandbox Code Playgroud)
使用env设置babel.config:env.test.preset: ['@babel/plugin-transform-modules-commonjs']
修改transformJest 配置中的设置'^.+\\.jsx?$': 'babel-jest', '^.+\\.tsx?$': …
我正在使用 Typescript 和 Jest。在开玩笑中,如果我想检查我的函数是否被调用,我可以运行
expect(myMockFn).toHaveBeenCalledWith(arrayArgument);
Run Code Online (Sandbox Code Playgroud)
我想检查我的函数是否是使用包含具有某些值的对象的数组参数调用的。例如,
expect(myMockFn).toHaveBeenCalledWith( [{x: 2, y: 3}] );
Run Code Online (Sandbox Code Playgroud)
实际的调用是使用如下所示的参数进行的
[{x: 2, y: 3, id: 'some-guid'}]
Run Code Online (Sandbox Code Playgroud)
所以我的期望失败了,因为我在数组的第一个对象中没有 id 属性,但我想匹配并忽略 ID,因为它每次都会不同,即使其他参数相同。如何使用 Jest 构建这样的 Expect 调用?
我正在尝试将我的项目升级到 React 18,一切都可以在浏览器中的开发和生产模式下运行。但是升级到最新版本后,@testing-library/react我的一些单元测试失败了,其中很多都记录了以下警告:
console.error
Warning: The current testing environment is not configured to support act(...)
at printWarning (node_modules/.pnpm/react-dom@18.0.0_react@18.0.0/node_modules/react-dom/cjs/react-dom.development.js:86:30)
at error (node_modules/.pnpm/react-dom@18.0.0_react@18.0.0/node_modules/react-dom/cjs/react-dom.development.js:60:7)
at isConcurrentActEnvironment (node_modules/.pnpm/react-dom@18.0.0_react@18.0.0/node_modules/react-dom/cjs/react-dom.development.js:25057:7)
at warnIfUpdatesNotWrappedWithActDEV (node_modules/.pnpm/react-dom@18.0.0_react@18.0.0/node_modules/react-dom/cjs/react-dom.development.js:27351:12)
at scheduleUpdateOnFiber (node_modules/.pnpm/react-dom@18.0.0_react@18.0.0/node_modules/react-dom/cjs/react-dom.development.js:25292:5)
at setLoading (node_modules/.pnpm/react-dom@18.0.0_react@18.0.0/node_modules/react-dom/cjs/react-dom.development.js:17342:16)
at _callee2$ (node_modules/.pnpm/@cubejs-client+react@0.29.51_react@18.0.0/node_modules/@cubejs-client/react/src/hooks/cube-query.js:56:7)
Run Code Online (Sandbox Code Playgroud)
我做的第一件事是检查我的版本,清除节点模块并锁定文件以防万一:
react18.0.0react-dom18.0.0@testing-library/react版本:“13.1.1”,但一切看起来都不错吗?
我检查了 React 18 的迁移文档:https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html
其中说最新版本@testing-library/react不需要globalThis.IS_REACT_ACT_ENVIRONMENT = true设置。
但无论如何,我在测试运行之前尝试手动设置。但这也没有解决它,(我尝试了几个版本)
// @ts-ignore
global.IS_REACT_ACT_ENVIRONMENT = true
// @ts-ignore
globalThis.IS_REACT_ACT_ENVIRONMENT = true
// @ts-ignore
self.IS_REACT_ACT_ENVIRONMENT = true
// @ts-ignore
window.IS_REACT_ACT_ENVIRONMENT …Run Code Online (Sandbox Code Playgroud) 我很难让 jest 与使用 ES 模块和import语法的打字稿项目一起工作。\n我的项目最初是为 commonjs 编写的,jest 测试运行良好。但后来我决定切换到 ES 模块(出于学习目的),玩笑不高兴 \xe3\x83\xbd(`\xd0\x94\xc2\xb4)\xef\xbe\x89\n我正在使用的工具:打字稿、玩笑、ts-玩笑
问题从import语法开始。
以下是我尝试过的代码。
\n// projectRoot/src/app.ts\n\nexport default someFunction = (): void => {\n // some code\n}\nRun Code Online (Sandbox Code Playgroud)\n如果
\n// projectRoot/__tests__/app.test.ts\n\nimport someFunction from \'../src/app\'; // without file extension\n\n/* This execute perfectly fine */\nRun Code Online (Sandbox Code Playgroud)\n但
\n// projectRoot/__tests__/app.test.ts\n\nimport someFunction from \'../src/app.ts\' // with .ts\n\n/*\n\xe2\x97\x8f Test suite failed to run\n\n __tests__/app.test.ts:1:25 - error TS2691: An import path cannot end with a \'.ts\' extension. Consider …Run Code Online (Sandbox Code Playgroud) jest我正在使用打字稿的简单 NodeJS 应用程序中运行测试。我的测试抛出一个错误:ReferenceError: structuredClone is not defined。
我没有收到任何 linter 错误,并且代码可以正常编译。
const variableForValidation = structuredClone(variableForValidationUncloned);
Run Code Online (Sandbox Code Playgroud)
包.json:
"dependencies": {
...
},
"devDependencies": {
"@types/jest": "^29.0.0",
"@types/node": "^18.7.15",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"eslint": "^8.23.0",
"jest": "^28.0.1",
"nodemon": "^2.0.19",
"serverless-plugin-typescript": "^2.1.2",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
}
Run Code Online (Sandbox Code Playgroud)
这个github问题向我表明问题已经解决:https://github.com/facebook/jest/issues/12628 - 或者也许我误解了?
我见过类似的 Stack 问题,但使用 Mocha: mocha not recognize StructuredClone is not Defined
我有"jest": "^29.2.1"。我的单元测试输出错误如下:
ts-jest[ts-jest-transformer] (WARN) Define `ts-jest` config under `globals` is deprecated. Please do
transform: {
<transform_regex>: ['ts-jest', { /* ts-jest config goes here in Jest */ }],
},
ts-jest[backports] (WARN) "[jest-config].globals.ts-jest.tsConfig" is deprecated, use "[jest-config].globals.ts-jest.tsconfig" instead.
ts-jest[backports] (WARN) Your Jest configuration is outdated. Use the CLI to help migrating it: ts-jest config:migrate <config-file>.
Run Code Online (Sandbox Code Playgroud)
我jest.config.ts的如下:
const config = {
roots: ['<rootDir>/src'],
verbose: true,
globalSetup: './globalSetup.ts',
testEnvironmentOptions: {
url: 'http://localhost/',
},
setupFilesAfterEnv: [],
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'], …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的自定义 tsconfig 文件没有在 jest.config.ts 中被选取。这是我的 jest.config.ts:
module.exports = {
setupFilesAfterEnv: [`./jest.setup.ts`],
testEnvironment: `jsdom`,
roots: [
`<rootDir>/test`
],
testMatch: [
`**/__tests__/**/*.+(ts|tsx|js)`,
`**/?(*.)+(spec|test).+(ts|tsx|js)`
],
transform: {
"^.+\\.(ts|tsx)$": `ts-jest`
},
globals: {
"ts-jest": {
tsConfig: `tsconfig.jest.json`
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道该配置的其他部分正在应用。例如,如果我删除全局变量上面的键,我的测试就不会运行。另外,我知道 tsconfig.jest.json 文件中指定的更改是修复我的测试所必需的更改,因为如果我在主 tsconfig.json 文件中进行相同的更改,我的测试运行良好。
我还尝试将所需的 tsconfig 编译器选项直接放入 jest.config.ts 文件中,但这似乎也不起作用:
module.exports = {
setupFilesAfterEnv: [`./jest.setup.ts`],
testEnvironment: `jsdom`,
roots: [
`<rootDir>/test`
],
testMatch: [
`**/__tests__/**/*.+(ts|tsx|js)`,
`**/?(*.)+(spec|test).+(ts|tsx|js)`
],
transform: {
"^.+\\.(ts|tsx)$": `ts-jest`
},
globals: {
"ts-jest": {
tsConfig: {
jsx: `react`
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在尝试运行测试文件时遇到错误(我正在使用反应打字稿)
\n \xe2\x97\x8f Test suite failed to run\n\n Jest encountered an unexpected token\n\n Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.\n\n Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.\n\n By default "node_modules" folder is ignored by transformers.\n\n Here's what you can do:\n \xe2\x80\xa2 If …Run Code Online (Sandbox Code Playgroud) ts-jest ×10
jestjs ×9
reactjs ×4
typescript ×4
babel-jest ×3
node.js ×2
babeljs ×1
javascript ×1
jsdom ×1
mocking ×1
next.js ×1
react-tsx ×1
testing ×1
unit-testing ×1