I'm following the manual setup as described here to configure Jest in Next.js 14.1.3 with TypeScript.
When run jest
, it complaints that 'ts-node' is required:
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
However, the installation step did mention ts-node
npm install -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom
Is this a documentation error or I'm missing something here?
I had the same issue and found that the issue was because the configuration was saved as .ts
. So what I did to fix the issue was to rename the configuration file to jest.config.js
and change the content like below:
/**
* 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);