Situation 1
Use mocha
in server-side TypeScript projects with compilerOptions.paths
in tsconfig.json
?
Then we need:
mocha
tsconfig-paths
ts-node
{ "ts-node": { "files": true } }
in thetsconfig.json
file (see more here)
Depending on how we import
our modules around in the codebase, point (4) above may not always be needed. In one of my projects, it was crucial.
Then run our test with e.g:
mocha --require ts-node/register --require tsconfig-paths/register
So that compilerOptions.paths
in our tsconfig.json
file is respected.
This way we can test directly against our .ts
files
Situation 2
Use jest
in server-side TypeScript projects?
- add
{ "testEnvironment": "node" }
to the config (e.g. thejest
entry inpackage.json
or equivalents) <rootDir>
token can be used insidemoduleDirectories
moduleNameMapper
can be useful