I have a nodejs project in Visual Studio 2017 and am trying to unit test using mocha. For some reason Test Explorer can't find the unit tests if it requires any file not in node_modules.
I have a simple test index.spec.js and Test Explorer is able to find the unit tests:
var assert = require('assert');
describe('Index 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");
});
});
I have an identical test heroes.spec.js with the addition of "var heroes = require('./heroes');" and it does not find the unit test. I assume that it can't find heroes. The file heroes.js resides in the same folder as heroes.spec.js. I also tried it with ./heroes.js. In both files, TestFramework is set to Mocha
This is the file heroes.spec.js
var assert = require('assert');
var heroes = require('./heroes');
describe('Heroes Test', function () {
it('Test 1', function () {
assert.ok(true, "This shouldn't fail");
});
it('Test 4', function () {
assert.ok(1 === 1, "This shouldn't fail");
assert.ok(false, "This should fail");
});
});
I narrowed the problem. heroes.js references other modules and one of the modules references db.js, which uses config to set dbConfig. If I replace the code and hardcode the values, the unit tests are found. I have a work around, but I would like to do it correctly. I tried setting the environment variable, NODE_ENV=test, in the project properties and creating a test.config. Still the problem exists. Any idea of how to do this correctly?
Here is my work around
//var dbConfig = config.get('dbConfig');
var dbConfig = {
"user": "test",
"password": "password",
"server": "xxxx",
"database": "Sandbox"
};
Thanks