38

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)                
Rolly
  • 3,205
  • 3
  • 26
  • 35

3 Answers3

110

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.

Alvaro Rodriguez Scelza
  • 3,643
  • 2
  • 32
  • 47
Rolly
  • 3,205
  • 3
  • 26
  • 35
  • 6
    any explanation on why it failed with these switches? – MazBeye Jun 17 '19 at 08:01
  • 2
    Man you saved lot of my time. Thanks! – barath Jun 25 '19 at 20:24
  • add "--recursive" – David Lojudice Sb. Jul 28 '19 at 16:51
  • 1
    you saved me dude – blu10 Jun 18 '21 at 09:31
  • This worked for me. Default config created by VS Code did not work. Removing those two lines fixed it. – Matt Welke Oct 29 '21 at 23:40
  • @MazBeye the --ui (or -u) option specifies the user interface to use with mocha. It defaults to 'bdd', which matches the kind of syntax the OP used for the test, not tdd. You can find more information about this command line option here: https://www.w3resource.com/mocha/interfaces.php – fleebness Nov 05 '22 at 10:03
  • 1
    @fleebness i've found the answer to my question. You basically repeating the point I mentioned in my answer I submitted 3 years ago. But the link you provided is wrong. I think it supposed to be here: https://www.w3resource.com/mocha/command-line-usage.php – MazBeye Nov 07 '22 at 04:05
  • @MazBeye You're right... I focused on this and missed your answer. Also, your provided answer gives a better link than the w3resource site, as it links directly to the documentation I couldn't find from Mocha itself (preferable to an ancillary site, IMO). It's a pity it isn't the accepted answer. – fleebness Nov 07 '22 at 10:00
38

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.

MazBeye
  • 666
  • 7
  • 12
0

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

Rolly
  • 3,205
  • 3
  • 26
  • 35