From aa933a722efa2dc339ba0285332206273eb9954e Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 9 May 2022 13:52:12 -0500 Subject: [PATCH 1/3] Update moduleNameMapper jest config and remove extra deps --- examples/with-jest/package.json | 4 ---- packages/next/build/jest/jest.ts | 9 +++++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/with-jest/package.json b/examples/with-jest/package.json index 23e5d9ed94c8..d17e6fa98558 100644 --- a/examples/with-jest/package.json +++ b/examples/with-jest/package.json @@ -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" } diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index ab3108d792da..448f2515eb86 100644 --- a/packages/next/build/jest/jest.ts +++ b/packages/next/build/jest/jest.ts @@ -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)$': @@ -92,6 +88,11 @@ export default function nextJest(options: { dir?: string } = {}) { '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|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 From 426afcfe6cae049f9a442e80e032884a9bcad585 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 9 May 2022 14:03:29 -0500 Subject: [PATCH 2/3] move svg mock to its own entry for easier overriding --- packages/next/build/jest/jest.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/next/build/jest/jest.ts b/packages/next/build/jest/jest.ts index 448f2515eb86..256e11beb36c 100644 --- a/packages/next/build/jest/jest.ts +++ b/packages/next/build/jest/jest.ts @@ -85,10 +85,13 @@ 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 From e57345fba86b0b8c9fd6ccdcb09018545a2f3f89 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 10 May 2022 10:05:25 -0500 Subject: [PATCH 3/3] remove package script --- examples/with-jest/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/with-jest/package.json b/examples/with-jest/package.json index d17e6fa98558..c637cdf07f34 100644 --- a/examples/with-jest/package.json +++ b/examples/with-jest/package.json @@ -2,7 +2,6 @@ "private": true, "scripts": { "dev": "next dev", - "lint": "next lint", "build": "next build", "start": "next start", "test": "jest --watch",