How to force test, executed with ts-jest, to use node_modules in another folder?

Viewed 449

I have the following folder structure, the unique twist of which is the common shared via symlink.

root
├── projects
│   ├── A
│   │   ├── node_modules
│   │   ├── common (symlink ../common)
│   │   ├── srcA.ts
│   │   └── testA.ts
│   ├── B
│   │   ├── node_modules
│   │   ├── common (symlink ../common)
│   │   ├── srcB.ts
│   │   └── testB.ts
│   └── common
│       ├── srcC.ts
│       └── testC.ts

Note that the symlinked folder, common doesn't have its own node_modules. Instead, it is using the node_modules of each project separately. This setup works fine during regular runtime, but during tests, I am getting errors.

I've originally hoped that I can simply use jest in the same way I run the projects. However, jest is unable to follow symlinks. Due to that I've made the following jest.config.js (note that roots loads tests both from the src of the project and also from common):

module.exports = {
  rootDir: "./",
  roots: ["../common/", "src/"],
  preset: 'ts-jest',
  resolver: "jest-pnp-resolver",
  setupFiles: ["react-app-polyfill/jsdom"],
  testMatch: ["**/?(*.)(spec|test).{js,jsx,ts,tsx}"],
  testEnvironment: "jsdom",
  testURL: "http://localhost",
  transform: {
    "^.+\\.(js|jsx|ts|tsx)$": "ts-jest",
    "^.+\\.css$": "<rootDir>/scripts/config/jest/cssTransform.js",
    "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/scripts/config/jest/fileTransform.js",
  },
  transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$", "^.+\\.module\\.(css|sass|scss)$"],
  moduleNameMapper: {
    "^react-native$": "react-native-web",
    "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy",
    "^src/(.*)": "<rootDir>/src/$1",
    "^common/(.*)": "<rootDir>/common/$1",
  },
  moduleFileExtensions: ["web.js", "js", "web.ts", "ts", "web.tsx", "tsx", "json", "web.jsx", "jsx", "node"],
};

However, during tests testC.ts is unable to find the node_modules of its associated project. I've made several tries to force ts-jest to use the node_modules of the project:

module.exports = {
  moduleDirectories: ["/home/large-project/projects/A"],
  globals: {
    "ts-jest": {
      tsConfig: {
        baseUrl: "/home/large-project/projects/A",
      },
    },
  },
};

However, it doesn't seem to change anything. How can I make typescript, via ts-jest, to tell testC.ts to use the node_modules in the project in which I am executing the test?

0 Answers
Related