简体   繁体   English

引用或需要其他文件时,Visual Studio 2019 中的节点 js 应用程序的 Mocha 未在测试资源管理器中显示测试

[英]Mocha for node js app in Visual Studio 2019 is not showing Tests in Test Explorer when reference or require another file

I am using node js in VS 2019. I want to try Mocha as the test platform so I have gone through and installed that and got it working in a basic sense.我在 VS 2019 中使用节点 js。我想尝试将 Mocha 作为测试平台,所以我已经完成并安装了它并让它在基本意义上工作。 I have a simple calculator node js file (calc.js).我有一个简单的计算器节点 js 文件(calc.js)。

I have another file that has basic hello world tests (basictest.js).我有另一个包含基本 hello world 测试的文件(basictest.js)。 If I do NOT put in the "require" line for Calc.js, then these tests (Test 1 and Test 2) are discovered by VS 2019 test explorer and are listed.如果我没有为 Calc.js 添加“require”行,那么这些测试(测试 1 和测试 2)会被 VS 2019 测试资源管理器发现并列出。

However, I need the require line to access the calculator app, so when I put the require line var mycalc = require('./Scripts/Calc.js');但是,我需要 require 行来访问计算器应用程序,所以当我放置 require 行时var mycalc = require('./Scripts/Calc.js'); in there, the tests are NOT discovered.在那里,没有发现测试。 The same thing happens if I try a reference path=.. to it.如果我尝试引用path=.. ,也会发生同样的事情。 I am new to node js so I could be missing something seemingly obvious as well when referencing functions in another js file.我是节点 js 的新手,所以在引用另一个 js 文件中的函数时,我也可能会遗漏一些看似显而易见的东西。

basictest.js:基本测试.js:

'use strict';

var assert = require('assert');
var mycalc = require('./Scripts/Calc.js');

describe('Test Suite 1', function () {
it('Test 1', function () {
    assert.ok(true, "This shouldn't fail");
});

it('Test 2', function () {
    assert.ok(1 === 1, "This shouldn't fail");
    assert.ok(false, "This should fail");
});
});

Ok... I figured it out after hours of searching online and had no success.好的...经过数小时的在线搜索,我想通了,但没有成功。

This line here wasn't completely correct: var mycalc = require('./Scripts/Calc.js');这里的这一行并不完全正确: var mycalc = require('./Scripts/Calc.js');

For some reason js finds the calc.js file and test file.由于某种原因,js 找到了 calc.js 文件和测试文件。 However, the test explorer wasn't able to discover the tests.但是,测试资源管理器无法发现测试。 The reason is a simple "."原因很简单的一个“。” My guess is that./Scripts/ puts current working directory in the Scripts directory and VS is unable to find the tests.我的猜测是 ./Scripts/ 将当前工作目录放在 Scripts 目录中,VS 无法找到测试。 Just adding a second "."只需添加第二个“。” moves it back a directory so the Tests can be found.将其移回一个目录,以便可以找到测试。

Correct code: var mycalc = require('../Scripts/Calc.js');正确代码: var mycalc = require('../Scripts/Calc.js');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM