Please check the test file comment below for the issue
//dependency.js
const function Dependency(property) {
this.property = property;
}
Dependency.prototype.doSomething = function () {
console.log('inside doSomething property: ', this.property);
}
export default Dependency;
Another File
//myModule.js
import Dependency from './Dependency.js';
const function MyModule () {
this.dependency = new Dependency('Some Property');
}
MyModule.protoype.init = function () {
this.dependency.doSomething();
}
export default MyModule;
Test File
//myModule-test.js
import MyModule from '../myModule';
import Dependency from '../dependency';
describe('MyModule', () => {
it('doSomething on the dependency called on init', () => {
//Here is the problem, How to make Dependency Spy so that new Dependency()
//inside the MyModule.js returns the fake object
//Right now there are two instances one in the next line and another in MyModule.js
const $DependencyFake = new Dependency('Testing');
// spyOn(Dependency, 'doSomething');
const myModule_ = new MyModule();
myModule_.init();
expect($DependencyFake.doSomething).toHaveBeenCalled();
});
});
How to make Dependency Spy so that new Dependency() inside the MyModule.js returns the fake object?
In short, how to mock a dependency which is used as a class exported by a module.