Skip to content

Commit e05a95a

Browse files
authored
Update moduleNameMapper jest config and remove extra deps (#36787)
* Update moduleNameMapper jest config and remove extra deps * move svg mock to its own entry for easier overriding * remove package script
1 parent fdc071c commit e05a95a

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

examples/with-jest/package.json

-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"private": true,
33
"scripts": {
44
"dev": "next dev",
5-
"lint": "next lint",
65
"build": "next build",
76
"start": "next start",
87
"test": "jest --watch",
@@ -18,10 +17,6 @@
1817
"@testing-library/react": "12.1.2",
1918
"@testing-library/user-event": "13.5.0",
2019
"@types/react": "17.0.38",
21-
"babel-jest": "27.4.5",
22-
"eslint": "8.5.0",
23-
"eslint-config-next": "latest",
24-
"eslint-plugin-testing-library": "5.0.1",
2520
"jest": "27.4.5",
2621
"typescript": "4.5.4"
2722
}

packages/next/build/jest/jest.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ export default function nextJest(options: { dir?: string } = {}) {
7676
...resolvedJestConfig,
7777

7878
moduleNameMapper: {
79-
// Custom config will be able to override the default mappings
80-
// moduleNameMapper is matched top to bottom hence why this has to be before Next.js internal rules
81-
...(resolvedJestConfig.moduleNameMapper || {}),
82-
8379
// Handle CSS imports (with CSS modules)
8480
// https://jestjs.io/docs/webpack#mocking-css-modules
8581
'^.+\\.module\\.(css|sass|scss)$':
@@ -89,9 +85,17 @@ export default function nextJest(options: { dir?: string } = {}) {
8985
'^.+\\.(css|sass|scss)$': require.resolve('./__mocks__/styleMock.js'),
9086

9187
// Handle image imports
92-
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$': require.resolve(
88+
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp)$': require.resolve(
9389
`./__mocks__/fileMock.js`
9490
),
91+
92+
// Keep .svg to it's own rule to make overriding easy
93+
'^.+\\.(svg)$': require.resolve(`./__mocks__/fileMock.js`),
94+
95+
// custom config comes last to ensure the above rules are matched,
96+
// fixes the case where @pages/(.*) -> src/pages/$! doesn't break
97+
// CSS/image mocks
98+
...(resolvedJestConfig.moduleNameMapper || {}),
9599
},
96100
testPathIgnorePatterns: [
97101
// Don't look for tests in node_modules

0 commit comments

Comments
 (0)