unit test mocha Visual Studio Code describe is not defined

主宰稳场 提交于 2020-05-11 03:55:40

问题


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)                

回答1:


Finally !!! after a long search, red some tutorials and comments i found the solution. The problem was with the config

Open the test config file and delet 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.




回答2:


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.



来源:https://stackoverflow.com/questions/55283725/unit-test-mocha-visual-studio-code-describe-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!