Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run typescript tests for Mocha in WebStorm

I follow the instruction on this page:

Running Mocha tests in Webstorm

to run Mocha tests in WebStorm, but my tests are written in TypeScript (saved in .ts), I was wondering how do I run these? Here is an example:

describe("My Tests", () => {
    it("Test #1", () => {
        let nluConfig: any = {
            "Provider" : "LUIS",
            "Model" : {
                "ID" : "aaa",
                "Secret" : "bbb"
            }
        };
        debugger;
        let result:any = CNluFactory.getNluProvider(nluConfig);
        expect(result).to.be.an.instanceof(CLuis);
    });
});

Here is how I currently configured WebStorm to run my tests:

enter image description here

This is the error I get when I try to run:

"C:\Program Files\nodejs\node.exe" C:\Repository\Main\node_modules\nyc\bin\nyc.js --reporter=lcovonly --report-dir=C:\Users\User\AppData\Local\Temp\mocha-intellij-coverage- C:\Repository\Main\node_modules\mocha\bin\_mocha --ui bdd --reporter "C:\Program Files\JetBrains\WebStorm 2017.2.1\plugins\NodeJS\js\mocha-intellij\lib\mochaIntellijReporter.js" --recursive C:\Repository\Main\src\test
No test files found

I assume it couldn't recognize the TS file in my test directory?

like image 431
Bill Software Engineer Avatar asked Feb 13 '18 22:02

Bill Software Engineer


1 Answers

Mocha can't run typescript files natively; you need to either pre-compile them and pass the generated .js files to test runner, or compile them on-the-fly using ts-node, for example (--require ts-node/register/transpile-only or the now deprecated --compilers ts:ts-node/register/transpile-only):

enter image description here

Of course, you need to make sure to install typescript and ts-node modules next to mocha

like image 192
lena Avatar answered Sep 23 '22 21:09

lena