Over the past year I've had the pleasure of developing a full-stack application using the Remix framework and it was a bit tricky to get Mocha + Chai tests running.
Option 1: Use ts-node
Install ts-node
and then add the following script to package.json:
"test": "env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_COMPILER_OPTIONS='{\"module\": \"esnext\" }' mocha 'app/**/*.test.ts'"
Also create .mocharc.json
in the project root with the following:
{ "node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"] }
Option 2: Compile to commonjs separately
This option is a bit more involved but if you have some demarcation between modules, e.g. server and client, then it
might be worthwhile to have separate compilation for modules in order to have quick running tests. The idea here is to
create a separate pipeline-tsconfig.json
which compiles code into commonjs rather than esm (note the
included
folders and resulting output directory hierarchy):
{ "compilerOptions": { "target": "ES2018", "module": "commonjs", "allowJs": true, "outDir": "build-pipeline", "rootDir": "app", "strict": true, "skipLibCheck": true, "esModuleInterop": true, }, "include": ["app/core", "app/db", "app/services", "app/pipeline"] }
The module (in this example everything under pipeline) can then be tested directly via the following packageJson scrips:
"pipeline:build": "rm -rf ./build-pipeline && tsc -p pipeline-tsconfig.json", "pipeline:watch": "rm -rf ./build-pipeline && tsc -w -p pipeline-tsconfig.json", "pipeline:test": "npm run pipeline:build && mocha 'build-pipeline/**/*.test.js'", "pipeline:test:live": "mocha 'build-pipeline/**/*.test.js'",
Thus, run npm run pipeline:watch
and thereafter npm run pipeline:test:live
to run
continuous tests. npm run pipeline:test
can be used to only run tests (in a CI environment for example).
Happy testing!