Reputation: 1
I'm working on a Next.js project and want to set up Jest for testing both React components (using jsdom) and database-related logic (using node environment). I have a working configurations for testing components and database-related logic (both tested separately). But when I use projects to configure test for both environments and provide the array of objects with configurations only node environment works.
import type { Config } from "jest";
import nextJest from "next/jest.js";
const createJestConfig = nextJest({
dir: "./",
});
const config: Config = {
projects: [
{
displayName: "react",
preset: "ts-jest",
testEnvironment: "jsdom",
moduleNameMapper: {
"^@/app/(.*)$": "<rootDir>/app/$1",
},
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
transform: {
"^.+.tsx?$": ["ts-jest", {}],
},
testMatch: ["<rootDir>/app/__test__/**/*.jsdom.test.ts?(x)"],
},
{
displayName: "database",
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: {
"^@/app/(.*)$": "<rootDir>/app/$1",
},
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
transform: {
"^.+.tsx?$": ["ts-jest", {}],
},
testMatch: ["<rootDir>/app/__test__/**/*.node.test.ts?(x)"],
},
],
};
export default createJestConfig(config);
error:
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.
Details:
/home/user/app-admin-hub/app/__test__/components/Navigation.jsdom.test.tsx:44
(0, react_1.render)(<Navigation_1.default />);
^
SyntaxError: Unexpected token '<'
I tried to swap these objects.
Deleted node environment configuration completely (left jsdom config inside projects:[] only).
Upvotes: 0
Views: 19