Created
May 25, 2022 01:29
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
projects: [ | |
'<rootDir>/jest.frontend.config.js', | |
'<rootDir>/jest.server.config.js', | |
], | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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