My monorepo is set up with TypeScript, WebPack, and ts-jest. The build process is successful, but I'm running into issues with unit testing in the ./demo
sub-project due to the error:
Cannot find module '@mlhaufe/graphics/adapters' or its corresponding type declarations.
<root>/tsconfig.json
{
"compilerOptions": {
"baseUrl": "./packages",
"paths": {
"@mlhaufe/graphics/*": [
"lib/src/*"
]
}
},
"references": [
{
"path": "./packages/lib"
},
{
"path": "./packages/demo"
}
],
"files": [],
"exclude": [
"node_modules"
]
}
<root>/packages/demo/tsconfig.json
{
...
"references": [
{
"path": "../lib"
}
]
<root>/jest.config.mjs
import fs from 'fs';
import path from 'path';
import url from 'url';
import { pathsToModuleNameMapper } from 'ts-jest';
import tsconfig from './tsconfig.json' assert { type: 'json' };
const { compilerOptions } = tsconfig,
__filename = url.fileURLToPath(import.meta.url),
__dirname = path.dirname(__filename),
packageNames = fs.readdirSync(path.resolve(__dirname, './packages'));
/** @type {import('jest').Config} */
export default {
rootDir: compilerOptions.baseUrl,
verbose: true,
testPathIgnorePatterns: [
'<rootDir>/node_modules/',
],
reporters: [
'default',
['jest-junit', { outputDirectory: './coverage' }]
],
// <https://jestjs.io/docs/next/configuration#projects-arraystring--projectconfig>
projects: packageNames.map((name) => ({
displayName: name,
transform: {
'^.+\\.mts$': ['ts-jest', { useESM: true }]
},
moduleFileExtensions: ['js', 'mjs', 'mts'],
roots: [`<rootDir>`],
modulePaths: [compilerOptions.baseUrl],
// required due to ts-jest limitation
// <https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/#support-mts-extension>
resolver: '<rootDir>/mjs-resolver.ts',
// Used the path aliases in tsconfig.json
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>',
useESM: true
}),
// moduleNameMapper: {
// '@mlhaufe/graphics/(.+)': '<rootDir>/packages/lib/src',
// '^(\\.\\.?/.*)\\.mjs$': ['$1.mts', '$0']
// },
testMatch: [`<rootDir>/packages/${name}/**/*.test.mts`],
testPathIgnorePatterns: [`<rootDir>/packages/${name}/dist/`]
}))
};
<root>/package.json
{
...
"workspaces": [
"packages/*"
],
"engines": {
"node": ">=16.0.0"
],
"scripts": {
"build": "npm run build --workspaces",
"build:lib": "npm run build --workspace=packages/lib",
"build:demo": "npm run build --workspace=packages/demo",
"test": "jest --coverage",
"test:lib": "jest --selectProjects=lib --coverage",
"test:demo": "jest --selectProjects=demo --coverage",
"serve:demo": "npm run serve --workspace=packages/demo"
},
...
}
I'm facing an issue where ts-jest is unable to locate the module, even though webpack and TypeScript are functioning correctly. I'm struggling to understand the relationship between the settings outside of the projects
property and those within. I initially thought they would be global and apply to all projects, but it seems that's not the case.
Any insights on this matter would be greatly appreciated. I haven't come across a consistent (and modern) resource detailing how to tackle this challenge.