How to run Mocha tests written in tsx?
Asked Answered
G

1

8

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?

Gyve answered 21/9, 2017 at 10:8 Comment(0)
S
1

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.

Samaniego answered 12/11, 2019 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.