在NTVS中找不到摩卡单元测试

时间:2016-01-10 21:53:36

标签: visual-studio-2015 mocha typescript1.7 ntvs

我正在尝试使用TypeScript编写的Mocha单元测试在 Visual Studio的节点工具中的Visual Studio 2015社区版中工作。我收到此错误(在“输出”窗口中,Tests部分):

------ Discover test started ------
Processing:  <lot of *.js** files>...
Test discovery error: [TypeError: Cannot read property 'replace' of undefined] in C:\Code\ov\BuyCo\test\sellers\testPersistance.js
Test discovery error: [TypeError: Cannot read property 'replace' of undefined] in C:\Code\ov\BuyCo\test\sellers\testUserPersistance.js
...<andsoon>
Processing finished for framework of Mocha
Discovered 0 testcases.
========== Discover test finished: 0 found (0:00:01.4378126) ==========

所以它列出了.js文件而不是ts,这些已经被编译出来了,但是在这些函数中生成的代码中有绝对没有replace函数。所以这是一个非常奇怪的错误。我正在使用Typescript 1.7。

从命令提示符(npm test ...)运行时,测试正在运行。但我希望能够设置(注意我正在测试NodeJS代码,例如服务器端CommonJS)。

注意:在分析期间,我已经将一个测试文件简化为默认的typescript示例文件,但它引发了相同的错误,因此不应该是问题:

import assert = require('assert');

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

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

1 个答案:

答案 0 :(得分:2)

最终设法解决了这个问题;以为我会分享:

这个问题是由我的案例引起的,NTVS有一些奇怪的范围要求。

测试文件中有一些未嵌套在函数内的Javascript代码。然后在NTVS上下文中执行代码,并在那里启动堆栈跟踪。因此,错误消息中提及replace在我的测试代码中没有出现,但在其他地方。

可以通过将此类代码移动到函数中来解决此问题。在这种情况下进入(Mochabefore函数。我实际上后来又遇到了完全相同的问题,这次来自不同的代码。下面是移动一些代码来解决此问题的示例。希望NTVS能够再次正确检测您的单元测试。

之前(错误)

import ...

var clearDb = require("mocha-mongoose")(dbUri);     <--- NTVS don't like it

describe("Example unit test", () => {
    before(done => {
        var testSubject = { name: "John Doe" };
    }

    it("throws strange error");
}

import ...

describe("Example unit test", () => {
    before(done => {
        var clearDb = require("mocha-mongoose")(dbUri);  <--- Move to here
        var testSubject = { name: "John Doe" };
    }

    it("now works");
}

注意:我正在使用ES6 lambda's - () => { ... } - 而不是常规的ES5函数 - function() { ... } - ,但这对错误并不重要。

相关问题