Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update moduleNameMapper jest config and remove extra deps #36787

Merged
merged 5 commits into from May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions examples/with-jest/package.json
Expand Up @@ -18,10 +18,6 @@
"@testing-library/react": "12.1.2",
"@testing-library/user-event": "13.5.0",
"@types/react": "17.0.38",
"babel-jest": "27.4.5",
"eslint": "8.5.0",
"eslint-config-next": "latest",
"eslint-plugin-testing-library": "5.0.1",
"jest": "27.4.5",
"typescript": "4.5.4"
}
Expand Down
14 changes: 9 additions & 5 deletions packages/next/build/jest/jest.ts
Expand Up @@ -76,10 +76,6 @@ export default function nextJest(options: { dir?: string } = {}) {
...resolvedJestConfig,

moduleNameMapper: {
// Custom config will be able to override the default mappings
// moduleNameMapper is matched top to bottom hence why this has to be before Next.js internal rules
...(resolvedJestConfig.moduleNameMapper || {}),

// Handle CSS imports (with CSS modules)
// https://jestjs.io/docs/webpack#mocking-css-modules
'^.+\\.module\\.(css|sass|scss)$':
Expand All @@ -89,9 +85,17 @@ export default function nextJest(options: { dir?: string } = {}) {
'^.+\\.(css|sass|scss)$': require.resolve('./__mocks__/styleMock.js'),

// Handle image imports
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$': require.resolve(
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp)$': require.resolve(
`./__mocks__/fileMock.js`
),

// Keep .svg to it's own rule to make overriding easy
'^.+\\.(svg)$': require.resolve(`./__mocks__/fileMock.js`),

// custom config comes last to ensure the above rules are matched,
// fixes the case where @pages/(.*) -> src/pages/$! doesn't break
// CSS/image mocks
...(resolvedJestConfig.moduleNameMapper || {}),
},
testPathIgnorePatterns: [
// Don't look for tests in node_modules
Expand Down