Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Mocha tests written in tsx?

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?

like image 619
Tan Dat Avatar asked Nov 08 '22 16:11

Tan Dat


1 Answers

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 IntelliJ IDEA Mocha tests configuration

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.

like image 106
mtbno Avatar answered Nov 15 '22 12:11

mtbno