jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.12k stars 6.44k forks source link

[Bug]: Jest fails to read config when coming from external package in TS / ESM / Monorepo #15254

Open fallemand opened 1 month ago

fallemand commented 1 month ago

Version

Jest: 29.7.0 Node: 20.15 ts-node: 10.9.2

Steps to reproduce

In a monorepo setup, using ESM and TS, jest fails to read correctly the config when exported from a different package.

/**
 * Importing the config from a different package doesn't work
 * Error: Jest: Failed to parse the TypeScript config file /projects/jest-esmodules/packages/package/jest.config.ts
 * Error: Must use import to load ES Module: /projects/jest-esmodules/packages/testing/jest.config.base.ts
 */
import { baseConfig } from "@tfe/testing/jest.config.base";
export default baseConfig;

But when copying the config on each package, it works well

export default {
  extensionsToTreatAsEsm: [".ts"],
  transform: {
    "^.+\\.(t|j)sx?$": [
      "@swc/jest",
      {
        jsc: {
          target: "es2021",
        },
      },
    ],
  },
};

Added a minimal setup: https://github.com/fallemand/jest-ts-esm-monorepo

Repo to reproduce

git clone git@github.com:fallemand/jest-ts-esm-monorepo.git
cd jest-ts-esm-monorepo
pnpm i
cd packages/package
pnpm test

Environment

System:
    OS: macOS 14.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 20.15.1 - ~/.nvm/versions/node/v20.15.1/bin/node
    npm: 10.7.0 - ~/.nvm/versions/node/v20.15.1/bin/npm
    pnpm: 9.4.0 - ~/.nvm/versions/node/v20.15.1/bin/pnpm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.