diff --git a/jest.config.js b/jest.config.js index c1d29c807b..a33baa5738 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,10 +2,6 @@ const { pathsToModuleNameMapper } = require('ts-jest'); const { compilerOptions } = require('./tsconfig.json'); const nextJest = require('next/jest'); -const paths = pathsToModuleNameMapper(compilerOptions.paths, { - prefix: '/', -}); - /** @type {import('ts-jest').InitialOptionsTsJest} */ const customJestConfig = { collectCoverage: true, @@ -26,9 +22,6 @@ const customJestConfig = { '!**/Header.tsx', ], moduleDirectories: ['node_modules', '/'], - moduleNameMapper: { - ...paths, - }, setupFilesAfterEnv: ['/jest.setup.js'], testEnvironment: 'jest-environment-jsdom', }; @@ -38,7 +31,19 @@ const createJestConfig = nextJest({ })(customJestConfig); module.exports = async () => { + // Create Next.js jest configuration presets const jestConfig = await createJestConfig(); + + // Custom `moduleNameMapper` configuration + const paths = pathsToModuleNameMapper(compilerOptions.paths, { + prefix: '/', + }); + const moduleNameMapper = { + ...jestConfig.moduleNameMapper, + ...paths, + }; + + // Custom `transformIgnorePatterns` configuration const transformIgnorePatterns = [ // Transform ESM-only modules in `node_modules`. '/node_modules/(?!next-mdx-remote|@mdx-js|@react-hook)', @@ -46,5 +51,6 @@ module.exports = async () => { pattern => pattern !== '/node_modules/' ), ]; - return { ...jestConfig, transformIgnorePatterns }; + + return { ...jestConfig, moduleNameMapper, transformIgnorePatterns }; };