Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created April 19, 2022 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanoats/ce28c8e05fc2a85c2d8a41fcbb9feb39 to your computer and use it in GitHub Desktop.
Save ivanoats/ce28c8e05fc2a85c2d8a41fcbb9feb39 to your computer and use it in GitHub Desktop.
import type { Config } from '@jest/types';
import nextJest from 'next/jest';
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
});
// Add any custom config to be passed to Jest
const customJestConfig: Config.InitialOptions = {
collectCoverageFrom: [
'**/*.{js,jsx,ts,tsx}',
'!**/*.d.ts',
'!**/node_modules/**',
],
coveragePathIgnorePatterns: ['/node_modules/'],
// extensionsToTreatAsEsm: ['.ts'],
globals: {
'ts-jest': {
useESM: true,
},
},
testMatch: ['tests/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
roots: ['<rootDir>/src', '<rootDir>/tests'],
preset: 'ts-jest',
transform: {
'^.+\\.(js|jsx|ts|tsx)$': [
'ts-jest',
{ presets: ['next/babel', '@babel/preset-typescript'] },
],
},
transformIgnorePatterns: [
'/node_modules/',
'^.+\\.module\\.(css|sass|scss)$',
],
testEnvironment: 'jsdom',
testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'],
moduleDirectories: ['node_modules', 'src'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node', 'cjs'],
modulePaths: ['<rootDir>'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleNameMapper: {
// Handle module aliases (this will be automatically configured for you soon)
'^@/components/(.*)$': '<rootDir>/components/$1',
'\\.(scss|sass|css)$': 'identity-obj-proxy',
'^(\\.{1,2}/.*)\\.js$': '$1',
'^@/pages/(.*)$': '<rootDir>/pages/$1',
'^csv-parse/sync': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs',
},
verbose: true,
};
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(customJestConfig);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment