Skip to content

Commit

Permalink
fix(jest-next.js): rectify broken jest moduleNameMapper configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi committed May 6, 2022
1 parent dd184a4 commit dc0a1c8
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions jest.config.js
Expand Up @@ -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: '<rootDir>/',
});

/** @type {import('ts-jest').InitialOptionsTsJest} */
const customJestConfig = {
collectCoverage: true,
Expand All @@ -26,9 +22,6 @@ const customJestConfig = {
'!**/Header.tsx',
],
moduleDirectories: ['node_modules', '<rootDir>/'],
moduleNameMapper: {
...paths,
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testEnvironment: 'jest-environment-jsdom',
};
Expand All @@ -38,13 +31,26 @@ 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: '<rootDir>/',
});
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)',
...jestConfig.transformIgnorePatterns.filter(
pattern => pattern !== '/node_modules/'
),
];
return { ...jestConfig, transformIgnorePatterns };

return { ...jestConfig, moduleNameMapper, transformIgnorePatterns };
};

0 comments on commit dc0a1c8

Please sign in to comment.