Rolly
Rolly

Reputation: 3365

unit test mocha Visual Studio Code describe is not defined

If i run in the console the test runs fine

        mocha --require ts-node/register tests/**/*.spec.ts

Note: I installed mocha and mocha -g

I want to run unit test from Visual Studio Code

launcgh.js file

            "version": "0.2.0",
            "configurations": [
                {
                    "type": "node",
                    "request": "launch",
                    "name": "Mocha Tests",
                    "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
                    "args": [
                        "--require", 
                        "ts-node/register",
                        "-u",
                        "tdd",
                        "--timeout",
                        "999999",
                        "--colors",
                        "${workspaceFolder}/tests/**/*.spec.ts"
                    ],
                    "internalConsoleOptions": "openOnSessionStart"
                },

Very simple Test file

            import { expect } from 'chai';
            const hello = () => 'Hello world!'; 
            describe('Hello function', () => {
                it('should return hello world', () => {
                    const result = hello();
                    expect(result).to.equal('Hello world!');
                });
            });

but in the Visual studio Code debug console

            /usr/local/bin/node --inspect-brk=15767 node_modules/mocha/bin/_mocha --require ts-node/register -u tdd --timeout 999999 --colors /Applications/MAMP/htdocs/ddd-board-game/backend/tests/**/*.spec.ts 
            Debugger listening on ws://127.0.0.1:15767/bdec2d9c-39a7-4fb7-8968-8cfed914ea8d
            For help, see: https://nodejs.org/en/docs/inspector
            Debugger attached.
            /Applications/MAMP/htdocs/ddd-board-game/backend/tests/dummy.spec.ts:3
            source-map-support.js:441
            describe('Hello function', () => {
            ^
            ReferenceError: describe is not defined
            source-map-support.js:444
                at Object.<anonymous> (/Applications/MAMP/htdocs/ddd-board-game/backend/tests/dummy.spec.ts:1:1)
                at Module._compile (internal/modules/cjs/loader.js:701:30)
                at Module.m._compile (/Applications/MAMP/htdocs/ddd-board-game/backend/node_modules/ts-node/src/index.ts:414:23)                

Upvotes: 40

Views: 14420

Answers (4)

Yorai Levi
Yorai Levi

Reputation: 750

related: https://github.com/Microsoft/vscode/issues/5627
added 👇 to .vscode-test.mjs

mocha: {
    ui: 'bdd',
},
// .vscode-test.mjs
import { defineConfig } from '@vscode/test-cli';

export default defineConfig({
    files: 'out/test/**/*.test.js',
    mocha: {
        ui: 'bdd',
    },
});

Upvotes: 0

Rolly
Rolly

Reputation: 3365

Finally!!! After a long search and reading some tutorials and comments I found the solution: the problem was with the config.

Open the test config file and delete the following lines:

            "-u", <<<< delete this line
            "tdd", <<<< delete this line

launch.js

        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Mocha Tests",
                "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
                "args": [
                    "--require", 
                    "ts-node/register",
                    "-u", <<<< delete this line
                    "tdd", <<<< delete this line
                    "--timeout",
                    "999999",
                    "--colors",
                    "${workspaceFolder}/tests/**/*.spec.ts"
                ],
                "internalConsoleOptions": "openOnSessionStart"
            },

Run the test again and it will work.

Upvotes: 117

Rolly
Rolly

Reputation: 3365

this is my configuration in jun 2020 if anyone is looking for.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Mocha Tests",
            "program": "${workspaceFolder}/node_modules/.bin/mocha",
            "args": [
                "--extension",
                "ts",
                "--watch",
                "src",
                "--require",
                "ts-node/register",
                "${workspaceFolder}/src/**/*.spec.ts"
            ],
            "internalConsoleOptions": "openOnSessionStart"
        }   
    ]
}

Change "src", with your custom location and "${workspaceFolder}/src/**/*.spec.ts" with your custom test files

Upvotes: 0

MazBeye
MazBeye

Reputation: 912

I've stumbled upon mocha docs here:

Interfaces and UI Switch

TLDR;

--ui, -u switch has two options: bdd and tdd. However, it also states it will be defaulted to bdd when --ui, -u switch is not supplied.

Hence, when you're using --ui tdd switch, you're expected to use TDD interface which provides suite(), test(), suiteSetup(), suiteTeardown(), setup(), and teardown() compared to BDD's describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach() approach.

That explains why it screams describe function is not defined.

Upvotes: 40

Related Questions