Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created May 25, 2022 01:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/3de2e5d41e3a7781913c9054093d7732 to your computer and use it in GitHub Desktop.
Save sibelius/3de2e5d41e3a7781913c9054093d7732 to your computer and use it in GitHub Desktop.
Jest Multi Project Nextjs myTest.spec.tsx (frontend test, jsdom), anotherTest.server.tsx (backend test, node)
module.exports = {
projects: [
'<rootDir>/jest.frontend.config.js',
'<rootDir>/jest.server.config.js',
],
}
const nextJest = require('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 = {
testRegex: '((\\.|/)(spec))\\.(js|ts|tsx)?$',
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
setupFiles: ['<rootDir>/test/setupFiles.js'],
moduleNameMapper: {
// Handle module aliases (this will be automatically configured for you soon)
'^@/components/(.*)$': '<rootDir>/components/$1',
'^@/pages/(.*)$': '<rootDir>/pages/$1',
},
testEnvironment: 'jsdom',
}
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)
const nextJest = require('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 = {
preset: '@shelf/jest-mongodb',
testRegex: '((\\.|/)(server))\\.(js|ts|tsx)?$',
setupFiles: ['<rootDir>/test/setupFiles.js'],
setupFilesAfterEnv: ['<rootDir>/jest.server.setup.js'],
}
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment