如何使用NPM测试进行调试 [英] How to debug with npm test

查看:899
本文介绍了如何使用NPM测试进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS Code.当我尝试在调试模式下运行测试时,它说describe不是函数.因此,我能够运行测试的唯一方法是通过npm Note.

I am using VS Code. When I try to run the test in debug mode, it says describe is not a function. Thus, only way I am able to run the test is through npm Note.

注意:我正在使用摩卡咖啡和柴.

Note: I am using mocha and chai.

var { describe,it, before, after } = require('mocha');
var assert = require('chai').assert;
var AuthAPI = require('../api/controllers/API.js');
     describe('getItem tests', function() {
    it('getItem ', function(done) {
      var API = new AuthAPI(clientId, PASS, List);

      api_jwt = API.getItem();
      assert.isNotEmpty(api_jwt);
    });
    )}

推荐答案

我认为您仅需要另一种用于Mocha测试的启动配置即可.

I think you just need another launch configuration for your mocha tests.

转到Visual Studio代码中的调试"部分.

Go to Debug section in your Visual Studio Code.

单击选择控件,然后选择添加配置..."(或单击齿轮图标,然后单击添加配置..."按钮);

Click the select control, then select "Add Configuration..." (or just click the gear icon and then click the "Add Configuration..." button);

您应该可以在此处选择"Node.js:Mocha测试",它将生成如下启动配置:

You should be able to select "Node.js: Mocha Tests" there, it would generate a launch config like that:

{
    "type": "node",
    "request": "launch",
    "name": "Mocha Tests",
    "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
    "args": [
        "-u",
        "tdd",
        "--timeout",
        "999999",
        "--colors",
        "${workspaceFolder}/test"
    ],
    "internalConsoleOptions": "openOnSessionStart"
},

!注意:从mocha 6开始,它需要正确选择接口( https://mochajs.org/#interfaces ),如果要使用describe函数,请将tdd更改为bdd.

确保您的Mocha测试位于./test文件夹中,并且已在本地安装了Mocha或自定义启动程序配置.

!Note: starting from mocha 6 it requires a proper selection of the interface (https://mochajs.org/#interfaces), change tdd to bdd if you want to use describe functions.

Make sure your mocha tests are in ./test folder and you have installed mocha locally or customise the launcher config.

假设您有一个测试./test/it_should_work.js(请注意,它没有任何要求,因为mocha二进制文件本身知道该功能)

Let's say you have a test ./test/it_should_work.js (note, it does not have any require, since mocha binary knows that functions by itself)

describe('test', () => {
  it('should work', () => {
  });
})

启动新创建的摩卡测试"配置,它应该返回成功的结果.

Launch a newly created "Mocha Test" configuration, it should return a successful result.

test
    ✓ should work
  1 passing (9ms)


但是,如果您真的需要以编程方式启动Mocha测试,请查看此官方Wiki页面 查看全文

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