未定义单元测试摩卡Visual Studio Code描述 [英] unit test mocha Visual Studio Code describe is not defined

查看:80
本文介绍了未定义单元测试摩卡Visual Studio Code描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If i run in the console the test runs fine

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

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

Note: I installed mocha and mocha -g

我想从Visual Studio代码运行单元测试

I want to run unit test from Visual Studio Code

launcgh.js文件

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

非常简单的测试文件

            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调试控制台中

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)                

推荐答案

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

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

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.

这篇关于未定义单元测试摩卡Visual Studio Code描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆