I have a project structure that goes like this:
- common-project
- app
- utilities
- src
- folders containing shared code
- src
- utilities
- app
- project-two
- src
- folders containing specific code
- tsconfig.json
- jest.config.ts
- src
- project-three
- src
- folders containing specific code
- tsconfig.json
- jest.config.ts
- src
And I want to be able to extract common code/configurations from the config files (such as jest.config.ts) to my common app, so there is only one place to update them in future. However, using the paths I have set up in my tsconfig (@core) it fails to recognise this "module" anywhere outside of the /src/ folder (in project-two & project-three) when running jest/the project. However if I update this to the relative path (../common-project/etc) then jest/the project runs perfectly fine - with the correct configuration?
and in my tsconfig.json I have:
{"compilerOptions": {"composite": true,"module": "commonjs","resolveJsonModule": true,"declaration": true,"removeComments": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"allowSyntheticDefaultImports": true,"target": "es2019","sourceMap": true,"outDir": "./dist","baseUrl": "./","skipLibCheck": true,"esModuleInterop": true,"strictPropertyInitialization": false,"moduleResolution": "node","lib": ["dom", "es2017"],"paths": {"@core/*": ["../common-project/app/utilities/src/*"] },"strict": true,"noImplicitAny": false,"typeRoots": ["cdk/node_modules/@types", "./node_modules/@types"],"types": ["node", "jest"] },"references": [ {"path": "../common-project" }, {"path": "../common-project/app/utilities" } ],"include": ["./src/**/*.ts","./cdk/**/*.ts","./jest.config.ts","./tsconfig.json" ],"exclude": ["./cdk/cdk.out"]}
and in my jest.config.ts:
import { pathsToModuleNameMapper } from "ts-jest";// import { initialise } from "../common-project/app/utilities/src/shared/config/jest.config";import { initialise } from "@core/shared/config/jest.config";import { compilerOptions } from "./tsconfig.json";export default initialise( pathsToModuleNameMapper(compilerOptions.paths, { prefix: "<rootDir>/", }), {});
// import { initialise } from "../common-project/app/utilities/src/shared/config/jest.config";
^this is the import which works when swapped out
and initialise (the common setup function I'm trying to use for Jest; however this premise will be extended to other configs such as eslint) is in the shared folder of common-project, like:
export function initialise(pathsToModuleNameMapper: any, overrides = {}): any { console.log("INITIALISE!!!"); return { bail: 1, coverageDirectory: "coverage", moduleFileExtensions: ["js", "json", "jsx", "ts", "tsx", "node"], preset: "ts-jest", roots: ["src"], testEnvironment: "node", testMatch: ["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)", ], transform: {"^.+\\.tsx?$": "ts-jest", }, collectCoverage: true, verbose: true, moduleDirectories: ["node_modules"], moduleNameMapper: pathsToModuleNameMapper, ...overrides, };}
So how can I get this to work while using the defined path, @core, instead of a relative path like "../.." in my root directory?
I've done things like this before using gitHub tagging/NPM for adding it as a dependency, but for this project it is a requirement to use the code sharing ability of TS.
Thanks for any help!