Is there any way to run mocha
tests written in typescript - tsx
?
When I run
mocha --require ts-node/register sometest.tsx
or
mocha --require ts-node/register --compiler ts:ts-node/register --compiler tsx:ts-node/register sometest.tsx
And the error shows:
TSError: ⨯ Unable to compile TypeScript
Cannot use JSX unless the '--jsx' flag is provided. (17004)
By the way, is it possible to debug the tests written in tsx
with WebStorm?
What worked for me was making sure my .tsconfig
is configured with:
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs"
...
}
...
}
My packages:
"mocha": "6.2.2",
"ts-node": "8.5.0",
I think the --compiler
flag for Mocha is deprecated as of today. In IntelliJ IDEA (should be very similar in WebStorm) I have adjusted the settings for Mocha tests so that I could run them in the IDE by opening Run/Debug Configurations (top right corner) and editing the Templates for Mocha.
The extra options are -r ts-node/register
My package.json
test script:
"test": "NODE_ENV=test mocha -r ts-node/register --ui bdd './src/**/*.spec.{js,jsx,ts,tsx}'",
Also try to change the Mocha package from global to local (in your node_modules). The debugging worked for me as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With