最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Using nextJest with multiple projects - Stack Overflow

programmeradmin4浏览0评论

I have the following jest.config.ts:

import type { Config } from "jest";
import nextJest from "next/jest.js";

const createJestConfig = nextJest({ dir: "./" });

const config: Config = {
  projects: [
    {
      displayName: "backend",
      testMatch: ["<rootDir>/tests/**/*.test.ts"],
      testEnvironment: "node",
      clearMocks: true,
      preset: "ts-jest",
      globalSetup: "<rootDir>/tests/globalSetup.ts",
      moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1",
      },
    },
    {
      displayName: "frontend",
      testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
      testEnvironment: "jsdom",
      clearMocks: true,
      preset: "ts-jest",
      setupFilesAfterEnv: ["<rootDir>/src/jest.setup.ts"],
      moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1",
      },
    },
  ],
};

export default createJestConfig(config);

This works for my backend tests, but fails to compile any jsx. After a bunch of fiddling, I figured out that the issue is with using nextJest alongside projects. The following frontend only config works:

import type { Config } from "jest";
import nextJest from "next/jest.js";

const createJestConfig = nextJest({ dir: "./" });

const config: Config = {
  displayName: "frontend",
  testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
  testEnvironment: "jsdom",
  clearMocks: true,
  preset: "ts-jest",
  setupFilesAfterEnv: ["<rootDir>/src/jest.setup.ts"],
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
  },
};

export default createJestConfig(config);

It looks like whatever modifications nextJest makes to the config, it doesn't know how to do that for projects.

Is there a way to still use nextJest? Or at least a clean way to reproduce whatever transformations nextJest is doing to my jest config?

I know I can just print the working config that nextJest generates and manually build my jest.config.ts from that, but that seems messy and brittle and I'd really like to avoid it if possible!

I have the following jest.config.ts:

import type { Config } from "jest";
import nextJest from "next/jest.js";

const createJestConfig = nextJest({ dir: "./" });

const config: Config = {
  projects: [
    {
      displayName: "backend",
      testMatch: ["<rootDir>/tests/**/*.test.ts"],
      testEnvironment: "node",
      clearMocks: true,
      preset: "ts-jest",
      globalSetup: "<rootDir>/tests/globalSetup.ts",
      moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1",
      },
    },
    {
      displayName: "frontend",
      testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
      testEnvironment: "jsdom",
      clearMocks: true,
      preset: "ts-jest",
      setupFilesAfterEnv: ["<rootDir>/src/jest.setup.ts"],
      moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1",
      },
    },
  ],
};

export default createJestConfig(config);

This works for my backend tests, but fails to compile any jsx. After a bunch of fiddling, I figured out that the issue is with using nextJest alongside projects. The following frontend only config works:

import type { Config } from "jest";
import nextJest from "next/jest.js";

const createJestConfig = nextJest({ dir: "./" });

const config: Config = {
  displayName: "frontend",
  testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
  testEnvironment: "jsdom",
  clearMocks: true,
  preset: "ts-jest",
  setupFilesAfterEnv: ["<rootDir>/src/jest.setup.ts"],
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
  },
};

export default createJestConfig(config);

It looks like whatever modifications nextJest makes to the config, it doesn't know how to do that for projects.

Is there a way to still use nextJest? Or at least a clean way to reproduce whatever transformations nextJest is doing to my jest config?

I know I can just print the working config that nextJest generates and manually build my jest.config.ts from that, but that seems messy and brittle and I'd really like to avoid it if possible!

Share Improve this question asked yesterday JulianJulian 2,64423 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The process of writing up the question got my brain working, and I worked out a reasonable solution:

import type { Config } from "jest";
import nextJest from "next/jest.js";

const createJestConfig = nextJest({ dir: "./" });

const buildConfig = async (config: Config): Promise<Config> => {
  return await createJestConfig(config)();
};

export default async (): Promise<Config> => {
  const backendConfig = await buildConfig({
    displayName: "backend",
    testMatch: ["<rootDir>/tests/**/*.test.ts"],
    testEnvironment: "node",
    clearMocks: true,
    preset: "ts-jest",
    globalSetup: "<rootDir>/tests/globalSetup.ts",
    moduleNameMapper: {
      "^@/(.*)$": "<rootDir>/src/$1",
    },
  });

  const frontendConfig = await buildConfig({
    displayName: "frontend",
    testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
    testEnvironment: "jsdom",
    clearMocks: true,
    preset: "ts-jest",
    setupFilesAfterEnv: ["<rootDir>/src/jest.setup.ts"],
    moduleNameMapper: {
      "^@/(.*)$": "<rootDir>/src/$1",
    },
  });

  const config: Config = {
    projects: [backendConfig, frontendConfig],
  };

  return config;
};

I'm not going to mark it as an answer right away in case someone knows a better approach, but this seems ok to me.

发布评论

评论列表(0)

  1. 暂无评论