I'm using the tsx command (similar to ts-node) to run all my TypeScript code.
How can I use tsx to run my Mocha test suite?
tsx
Add the following "node-option" key to your .mocharc.json, or to the "mocha" section in your package.json:
"node-option"
.mocharc.json
"mocha"
package.json
{ "node-option": ["import=tsx"] }
This causes Mocha to run as if you had run it with node --import=tsx, or NODE_OPTIONS="--import=tsx" mocha.
node --import=tsx
NODE_OPTIONS="--import=tsx" mocha
Note that this requires at Node 18.18.0 or newer. On older Node versions (at least 14.18.0 seems to be required), use the --loader option instead:
--loader
{ "node-option": ["loader=tsx"] }
Copyright © 2021 Jogjafile Inc.
Add the following
"node-option"key to your.mocharc.json, or to the"mocha"section in yourpackage.json:This causes Mocha to run as if you had run it with
node --import=tsx, orNODE_OPTIONS="--import=tsx" mocha.Note that this requires at Node 18.18.0 or newer. On older Node versions (at least 14.18.0 seems to be required), use the
--loaderoption instead: