I am trying to migrate a yarn based monorepo to pnpm. For the context, the repo contains a shared folder, which most other projects use. We are using Jest v26 with React v17, and node v20. Trying to migrate from yarn v1.22.4 to pnpm v9.3.0.
I am having problems with jest-based tests. With yarn, everything works smoothly. After migrating to pnpm, with no changes to dependencies at all, I'm getting this error for every test suite:
FAIL tests/[redacted].test.ts Test suite failed to run
ENOENT: no such file or directory, open '[redacted - project directory]\node:os'
at Runtime.readFile (../../node_modules/.pnpm/[email protected][email protected][email protected]_/node_modules/jest-runtime/build/index.js:1987:21)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure/logger/src/log.ts:4:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure/logger/src/debug.ts:4:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure/logger/src/index.ts:4:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure/core-rest-pipeline/src/log.ts:4:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure/core-rest-pipeline/src/policies/logPolicy.ts:7:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure/core-rest-pipeline/src/createPipelineFromOptions.ts:4:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure/core-rest-pipeline/src/index.ts:43:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure-rest/core-client/src/restError.ts:4:1)
at Object.<anonymous> (../../node_modules/.pnpm/@[email protected]/node_modules/@azure-rest/core-client/src/index.ts:9:1)
The *.ts
files from the log above do not exist in my filesystem - I have tried opening the exact path from the logs, and I also browsed the node_modules
directory. I have no idea where this comes from.
One of the possible solutions I have found on the internet is to migrate to Jest v27 or higher, which indeed solves the issue above. However, v27 comes with breaking changes that breaks a lot of other tests for different reasons.
I have three questions:
- Why the tests work fine with packages installed via yarn, but fail with packages installed via pnpm?
- Why are there paths in the logs mentioned above, that do not exist, yet seem to be resolved fine by jest, considering the error stack?
- Is there any way to resolve this issue, using pnpm but without upgrading jest to v27?
My jest config:
module.exports = {
preset: "ts-jest",
setupFilesAfterEnv: ["./tests/setupTests.ts", "./tests/setupMocks.ts"],
moduleFileExtensions: ["js", "json", "jsx", "ts", "tsx"],
modulePaths: ["src", "test"],
moduleDirectories: ["node_modules", "shared"],
moduleNameMapper: {
"^.+\\.(css|less|scss)$": "<rootDir>/tests/mocks/styleMock.js",
"^@ProjectNameMocks(.*)$": "<rootDir>/storybook/stories/mocks$1",
"^@ProjectNameTests(.*)$": "<rootDir>/tests$1",
"^@ProjectName(.*)$": "<rootDir>/src$1"
},
testEnvironment: "jsdom",
transform: {
"^.+\\.(js|jsx)?$": "babel-jest"
},
transformIgnorePatterns: ["/node_modules/(?!(\\.pnpm|lodash-es|@fluentui|@azure))"],
modulePathIgnorePatterns: ["./bin"],
globals: {
"ts-jest": {
tsconfig: "./tsconfig.json"
}
}
};