I’m writing unit test for a JS class via sinon and here is the sample. I would like to know whether what ever I wrote is appropriate. Reason, I am not using stub/mock as what mentioned in different tutorial on the Internet.
//let someGlobalVariable
class Topics {
constructor() {}
isValidTopic(topics, requiredTopic) {
let topicFound = false;
requiredTopic.filter((topic) => {
if (topics.includes(requiredTopic.topicId)) {
topicFound = true;
}
});
return topicFound;
}
}
module.exports = Topics;
it('isValidTopic should return false', () => {
const topics = [
{ topicId: 'topic 1' },
{ topicId: 'topic 2' }
];
const requiredTopic = ['topic 9'];
const topicsInstance = new Topics();
const isValid = topicsInstance.isValidTopic(topics, requiredTopic);
sinon.assert.match(isValid, false);
});
Also, there is a global variable ‘someGlobalVariable’ and we are using across this class (I did not mention in the above code) and I know its not good to use global variables and should be part of constructor as ‘this.someGlobalVariable’ but the code base is written in that way unfortunately and have little control to refactor.
So question, how to mock/stub this global variable ?