Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)                
like image 354
Rolly Avatar asked Mar 21 '19 15:03

Rolly


People also ask

How do you run a test case in VS code?

The Testing Explorer is a tree view to show all the test cases in your workspace. You can select the beaker button on the left-side Activity bar of Visual Studio Code to open it. You can also run/debug your test cases and view their test results from there.

What is Mocha command?

If you run mocha --help on the command-line, you will get a list of all the instruction available to you. --allow-uncaught. By default, Mocha attempts to trap uncaught exceptions thrown from running tests and reports these as test failures.


2 Answers

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.

like image 171
Rolly Avatar answered Sep 23 '22 13:09

Rolly


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.

like image 21
MazBeye Avatar answered Sep 23 '22 13:09

MazBeye