Jest Config: How to use `projects`
Answered
Thrianta posted this in #help-forum
ThriantaOP
When I uncomment the following lines in my
jest.config.ts I get an syntax error from render(<TestBtn />)import nextJest from "next/jest"
import type { Config } from "jest"
const withNextJest = nextJest()
const config: Config = {
coverageProvider: "v8",
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/app/$1",
/* need to add: other layers of my app here */
},
// projects: [
// {
// displayName: "jsdom",
testEnvironment: "jsdom",
testMatch: ["<rootDir>/src/app/**/*test.tsx"],
testPathIgnorePatterns: ["<rootDir>/src/e2e-tests/"],
// },
/* need to add: other runners for my other layers */
// ],
}
export default withNextJest(config)Answered by Thrianta
I found a repo using multiple jest config files and this fixed everything for me.
https://github.com/sibelius/next-jest-multiproject
https://github.com/sibelius/next-jest-multiproject
// jest.config.ts
import { Config } from "jest"
export const sharedConfig: Config = {
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/app/$1",
...
},
}
const config: Config = {
projects: ["<rootDir>/jest.frontend.ts", "<rootDir>/jest.backend.ts"],
}
export default config
// jest.frontend.ts
import nextJest from "next/jest"
import type { Config } from "jest"
import { sharedConfig } from "./jest.config"
const withNextJest = nextJest()
const config: Config = {
...sharedConfig,
displayName: "jsdom",
testEnvironment: "jsdom",
testMatch: ["<rootDir>/src/app/**/*test.tsx"],
testPathIgnorePatterns: ["<rootDir>/src/e2e-tests/"],
}
export default withNextJest(config)
// jest.backend.ts
import nextJest from "next/jest"
import type { Config } from "jest"
import { sharedConfig } from "./jest.config"
const withNextJest = nextJest()
const config: Config = {
...sharedConfig,
displayName: "node",
testEnvironment: "node",
testMatch: ["<rootDir>/src/**/*test.ts"],
testPathIgnorePatterns: ["<rootDir>/src/e2e-tests/", "<rootDir>/src/app/"],
}
export default withNextJest(config)2 Replies
ThriantaOP
Error:
$ p jest
FAIL jsdom src/app/lib/components/test-btn/test.tsx
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
Details:
C:\...\src\app\lib\components\test-btn\test.tsx:12
(0, react_2.render)(<_1.TestBtn />);
^
SyntaxError: Unexpected token '<'
at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:1505:14)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.177 s
Ran all test suites.ThriantaOP
I found a repo using multiple jest config files and this fixed everything for me.
https://github.com/sibelius/next-jest-multiproject
https://github.com/sibelius/next-jest-multiproject
// jest.config.ts
import { Config } from "jest"
export const sharedConfig: Config = {
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/app/$1",
...
},
}
const config: Config = {
projects: ["<rootDir>/jest.frontend.ts", "<rootDir>/jest.backend.ts"],
}
export default config
// jest.frontend.ts
import nextJest from "next/jest"
import type { Config } from "jest"
import { sharedConfig } from "./jest.config"
const withNextJest = nextJest()
const config: Config = {
...sharedConfig,
displayName: "jsdom",
testEnvironment: "jsdom",
testMatch: ["<rootDir>/src/app/**/*test.tsx"],
testPathIgnorePatterns: ["<rootDir>/src/e2e-tests/"],
}
export default withNextJest(config)
// jest.backend.ts
import nextJest from "next/jest"
import type { Config } from "jest"
import { sharedConfig } from "./jest.config"
const withNextJest = nextJest()
const config: Config = {
...sharedConfig,
displayName: "node",
testEnvironment: "node",
testMatch: ["<rootDir>/src/**/*test.ts"],
testPathIgnorePatterns: ["<rootDir>/src/e2e-tests/", "<rootDir>/src/app/"],
}
export default withNextJest(config)Answer