【问题标题】:unit test mocha Visual Studio Code describe is not defined单元测试 mocha Visual Studio 代码描述未定义
【发布时间】:2019-08-12 12:03:25
【问题描述】:

如果我在控制台中运行,测试运行良好

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

注意:我安装了 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"
                },

非常简单的测试文件

            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!');
                });
            });

但在 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)                

【问题讨论】:

  • 你安装@types/mocha了吗?
  • 是的。我也在使用打字稿。

标签: typescript unit-testing visual-studio-code mocha.js


【解决方案1】:

终于!!!经过长时间的搜索并阅读了一些教程和 cmets,我找到了解决方案:问题出在配置上。

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

            "-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"
            },

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

【讨论】:

  • 对这些开关为什么会失败有任何解释吗?
  • 你节省了我很多时间。谢谢!
  • 添加“--recursive”
  • 你救了我,伙计
  • 这对我有用。 VS Code 创建的默认配置不起作用。删除这两行修复了它。
【解决方案2】:

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

InterfacesUI Switch

TLDR;

--ui, -u 开关有两个选项:bddtdd。但是,它还指出,当未提供 --ui, -u 开关时,它将默认为 bdd

因此,当您使用 --ui tdd 开关时,与 BDD 的 describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach() 方法相比,您应该使用提供 suite(), test(), suiteSetup(), suiteTeardown(), setup(), and teardown() 的 TDD 接口。

这就解释了为什么它会尖叫describe function is not defined。

【讨论】:

    【解决方案3】:

    如果有人要,这是我在 2020 年 6 月的配置。

    {
        // 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"
            }   
        ]
    }
    
    

    "src", 更改为您的自定义位置,将"${workspaceFolder}/src/**/*.spec.ts" 更改为您的自定义测试文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2019-08-10
      相关资源
      最近更新 更多