我按照此处所述的手动设置使用 TypeScript 在 Next.js 14.1.3 中配置 Jest。
运行时
jest
,它抱怨需要“ts-node”:
Error: Jest: Failed to parse the TypeScript config file C:\Users\dummy\playground\jest.config.ts
Error: Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed
Error: Cannot find package 'ts-node' imported from C:\Users\dummy\playground\node_modules\jest-config\build\readConfigFileAndSetRootDir.js
不过安装步骤确实提到了
ts-node
npm install -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom
这是文档错误还是我在这里遗漏了一些东西?
我也遇到了同样的问题,发现问题是因为配置保存为
.ts
。所以我解决这个问题的方法是将配置文件重命名为jest.config.js
并更改内容,如下所示:
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
const nextJest = require('next/jest')
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
})
/** @type {import('jest').Config} */
const config = {
// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
// The test environment that will be used for testing
testEnvironment: "jsdom",
};
module.exports = createJestConfig(config);