未定义单元测试摩卡Visual Studio Code描述

rol*_*oll 14 unit-testing mocha.js typescript visual-studio-code

如果我在控制台中运行,则测试运行正常

        mocha --require ts-node/register tests/**/*.spec.ts
Run Code Online (Sandbox Code Playgroud)

注意:我安装了mocha和mocha -g

我想从Visual Studio Code运行单元测试

launcgh.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",
                        "tdd",
                        "--timeout",
                        "999999",
                        "--colors",
                        "${workspaceFolder}/tests/**/*.spec.ts"
                    ],
                    "internalConsoleOptions": "openOnSessionStart"
                },
Run Code Online (Sandbox Code Playgroud)

非常简单的测试文件

            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!');
                });
            });
Run Code Online (Sandbox Code Playgroud)

但在Visual Studio Code调试控制台中

            /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)                
Run Code Online (Sandbox Code Playgroud)

rol*_*oll 34

终于!!! 经过长时间的搜索,红色的一些教程和评论我找到了解决方案。问题出在配置

打开测试配置文件并删除以下行

            "-u", <<<< delete this line
            "tdd", <<<< delete this line
Run Code Online (Sandbox Code Playgroud)

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 Code Online (Sandbox Code Playgroud)

再次运行测试,它将起作用。

  • 关于这些开关为何失败的任何解释? (2认同)
  • 伙计,您节省了我很多时间。谢谢! (2认同)

Maz*_*eye 5

我在这里偶然发现了摩卡文档:

界面UI开关

TLDR;

--ui, -u开关有两个选项:bddtdd。然而,它也指出,将被默认为bdd--ui, -u不提供开关。

因此,当您使用--ui tdd交换机时,应该使用TDD接口,该接口suite(), test(), suiteSetup(), suiteTeardown(), setup(), and teardown()与BDD的describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach()方法相比具有优势。

这就解释了为什么它describe没有定义尖叫功能。