Skip to content

Commit 1a23dc4

Browse files
committed
Switch from babel-jest to next/jest package for running unit tests.
1 parent 079e887 commit 1a23dc4

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

jest.config.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
module.exports = {
2-
roots: ["<rootDir>/__tests__/"],
3-
collectCoverageFrom: [
4-
"**/*.{js,jsx,ts,tsx}",
5-
"!**/*.d.ts",
6-
"!**/node_modules/**",
7-
],
1+
const nextJest = require("next/jest");
2+
3+
const createJestConfig = nextJest({
4+
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
5+
dir: "./",
6+
});
7+
8+
// Add any custom config to be passed to Jest
9+
const customJestConfig = {
810
moduleNameMapper: {
911
/* Handle CSS imports (with CSS modules)
1012
https://jestjs.io/docs/webpack#mocking-css-modules */
@@ -18,16 +20,10 @@ module.exports = {
1820
"^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$":
1921
"<rootDir>/__mocks__/fileMock.js",
2022
},
21-
testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/.next/"],
22-
testEnvironment: "jsdom",
23-
transform: {
24-
/* Use babel-jest to transpile tests with the next/babel preset
25-
https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object */
26-
"^.+\\.(js|jsx|ts|tsx)$": ["babel-jest", { presets: ["next/babel"] }],
27-
},
28-
transformIgnorePatterns: [
29-
"/node_modules/",
30-
"^.+\\.module\\.(css|sass|scss)$",
31-
],
23+
roots: ["<rootDir>/__tests__/"],
3224
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
25+
testEnvironment: "jest-environment-jsdom",
3326
};
27+
28+
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
29+
module.exports = createJestConfig(customJestConfig);

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"@testing-library/jest-dom": "^5.16.1",
2424
"@testing-library/react": "^12.1.2",
2525
"autoprefixer": "^10.4.2",
26-
"babel-jest": "^27.4.6",
2726
"cypress": "^9.2.0",
2827
"eslint": "8.6.0",
2928
"eslint-config-next": "^12.0.7",

0 commit comments

Comments
 (0)