I’m trying to get “absolute” import paths working in our mocha test files. These work fine for our Vite/TS React app.
import { agent } from 'tests/shared/supertest'
I’ve tried adding ts-node/tsconfig-paths to the config per their recommendation, but it still errors with Cannot find package 'tests' imported from testsconfigsmochasetup.ts
I tried using ts-mocha too but for people using ESM like we are, it’s not working either: https://github.com/piotrwitek/ts-mocha/issues/74
.mocharc.json:
{
"extensions": ["ts"],
"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm", "no-warnings"],
"require": [
"ts-node/register",
"tsconfig-paths/register",
"tests/configs/mocha/setup.ts",
"tests/configs/mocha/teardown.ts",
"jsdom-global/register"
],
"slow": 5000,
"timeout": 60000
}
tsconfig.node.json:
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
"baseUrl": "."
},
"include": ["vite.config.ts"]
}
tsconfig.json:
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "."
},
"include": ["app", "./tests/**/*.ts", ".storybook/*.ts", ".storybook/*.tsx", "./playwright.config.ts", "*.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}