【问题标题】:Jest - Next.js - Unexpected token error inside jest node-modulesJest - Next.js - jest 节点模块中出现意外的令牌错误
【发布时间】:2022-11-09 04:15:28
【问题描述】:

我在下一个 js 项目中使用以下命令安装了 jest

npm i --save-dev jest @testing-library/react @testing-library/jest-dom jest-environment-jsdom   

然后使用以下代码添加 jest.config.json 文件

const nextJest = require("next/jest");
const createJestConfig = nextJest({
  dir: "./",
});
const customJestConfig = {
  moduleDirectories: ["node_modules", "<rootDir>/"],
  testEnvironment: "jest-environment-jsdom",
};
module.exports = createJestConfig(customJestConfig);

现在,当我运行“npm test”时,出现以下错误

测试套件无法运行

D:\my-project\node_modules\@jest\reporters\build\GitHubActionsReporter.js:67

  #getMessageDetails(failureMessage, config) {
                    ^

SyntaxError: 意外的标记 '('

在对象。 (node_modules/@jest/reporters/build/index.js:75:3)

我尝试了很多解决方案,更新配置文件,添加 babel 插件,都没有奏效。而且我还没有在网上找到任何提及此错误的信息。这里有什么问题?

我的开发依赖版本 -

"devDependencies": {
  "@testing-library/jest-dom": "^5.16.5",
  "@testing-library/react": "^13.4.0",
  "eslint": "8.14.0",
  "eslint-config-next": "12.1.5",
  "jest": "^29.0.3",
  "jest-environment-jsdom": "^29.0.3"
}

【问题讨论】:

  • 你用的是什么版本的节点?
  • 是的。谢谢你。我使用的是 14.4.0,切换到最新及以上错误得到解决。
  • 是的,使用 # 语法的私有方法每个 node.green/#ES2022-features-private-class-methods 至少需要 14.6 个。

标签: javascript next.js jestjs


【解决方案1】:

正如 jonrsharpe 所提到的,修复是确保您安装了更新版本的节点。 此问题不仅限于将 Jest 与 Next.js 解决方案一起使用,还适用于与 React/VanillaJs/etc 一起使用的 Jest

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 2022-11-11
    • 1970-01-01
    • 2019-09-26
    • 2021-10-25
    • 2019-08-09
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多