Mise
Mise

Reputation: 103

Can't run Mocha tests with TypeScript in VSCode

my test file:

import { expect } from 'chai';

describe('test', () => {
    it('compiles', () => {
        expect(true).is.true;
    });
});

running this npm script works:

"test": "mocha -r esm -r ts-node/register test/**/Test*.t.ts"

example of how it should work: https://adrianhall.github.io/web/2018/07/04/run-typescript-mocha-tests-in-vscode/

in that example they use import { .. } in the test file as well, but for me that only works when I require esm (first mystery, but not a problem in itself).

when I run this configuration in VSCode (from example) it works when I 'npm run test' right before, but when I change my test code and runnning this it crashes on the TS type colon. Without '-r esm' mocha crashes on 'import {' again

{
    "type": "node",
    "request": "launch",
    "name": "Mocha Tests",
    "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
    "args": [
        "--require", "esm",
        "--require", "ts-node/register",
        "--timeout", "999999",
        "--colors", 
        "${workspaceFolder}/test/**/Test*.ts",
    ],
    "internalConsoleOptions": "openOnSessionStart"
}

Upvotes: 3

Views: 2662

Answers (1)

Mise
Mise

Reputation: 103

I made it work with ts-mocha.

package.json script

"test": "ts-mocha -r esm -p tsconfig.json test/**/Test*.ts"

launch.json config

{
    "type": "node",
    "request": "launch",
    "name": "Mocha Tests",
    "runtimeArgs": [
        "${workspaceFolder}/node_modules/ts-mocha/bin/ts-mocha",
        "--timeout", "999999",
        "-r", "esm",
        "-p", "${workspaceFolder}/tsconfig.json", "${workspaceFolder}/test/**/Test*.ts",
    ],
    "console": "integratedTerminal",
    "protocol": "inspector"
}

Upvotes: 4

Related Questions