1

I'm trying to get Mocha unit tests written in TypeScript to work in Visual Studio 2015 Community Edition in Node Tools for Visual Studio. I'm getting this error (in the Output window, Tests section):

------ 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) ==========

So it's listing .js files instead of ts, those are already transpiled out, but there is absolutely no replace function in the generated code in those functions. So it's a very strange error. I'm using Typescript 1.7.

The tests are working when run from the command prompt (npm test ...). But I want to be able to set (note I'm testing NodeJS code, e.g. serverside CommonJS).

Note: During analysis I've already simplified down one test file to the default typescript example file, but it raises the same error, so that should NOT be the issue:

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");
});
});
Bart
  • 5,065
  • 1
  • 35
  • 43

1 Answers1

2

It managed to fix this in the end; thought I'd share:

This issue was caused in my case by NTVS having some weird scoping requirement.

There was some Javascript code in the test file that was not nested inside a function. It's code was then executed within the NTVS context, and the stack trace initiated there. So the mention of replace in the error message was nowhere in my testing code, but elsewhere.

This issue can be solved by moving such code into a function. In this case into a (Mocha) before function. I actually later had the exact same issue again, this time coming from different code. Below is an example of moving some code to fix this issue. Hopefully then NTVS will detect your unit tests correctly again.

Before (error)

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");
}

After

import ...

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

    it("now works");
}

Note: I'm using ES6 lambda's - () => { ... } - here instead of regular ES5 functions - function() { ... } - , but that is not important for the error.

Bart
  • 5,065
  • 1
  • 35
  • 43