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

Viewed 371

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. I have a simple calculator node js file (calc.js).

I have another file that has basic hello world tests (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.

However, I need the require line to access the calculator app, so when I put the require line 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. I am new to node js so I could be missing something seemingly obvious as well when referencing functions in another js file.

basictest.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");
});
});
1 Answers

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');

For some reason js finds the calc.js file and test file. 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. Just adding a second "." moves it back a directory so the Tests can be found.

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

Related