I am attempting to npm mocha test a class that extends another class in another js file.
The problem I'm running into is that the code works fine when running in production but when running npm test ClassTwo cannot see class one regardless that I'm requiring it or not. If I add the definition for class one in the file for class two then it works fine.
I can't put a require in ClassTwo.js because it causes the browser code to explode since require isn't a concept there.
ClassOne.js
class ClassOne{
}
if(typeof module !== 'undefined'){
module.exports = ClassOne;
}
ClassTwo.js
class ClassTwo extends ClassOne{
}
if(typeof module !== 'undefined'){
module.exports = ClassTwo;
}
test.js
const ClassOne = require('../js/ClassOne.js');
const ClassTwo = require('../js/ClassTwo.js');
... unit tests ...
The answer was to create a global variable in the test.js file that was named the same as my extended class and then hydrate it with the require command.
test.js
const { global } = require("@sinonjs/commons");
global.ClassOne = require('../js/ClassOne.js');
const ClassTwo = require('../js/ClassTwo.js');