I want to write a unit test that covers the line in a constructor that calls super
. How can I mock super
so that I can verify it was called?
I found related content like mocking other functions on the super class and a solution that only works for ES6 code transpiled by Babel but nothing seems to address mocking the .super
call in native ES6.
Here is a contrived example from https://jsfiddle.net/wuzkhfcx/16/ showing all my various attempts failing:
class Vehicle {
constructor(make, model) {
this.make = make;
this.model = model;
}
}
class Car extends Vehicle {
constructor(make, model, doorCount) {
super(make, model);
this.doorCount = doorCount;
}
}
function testCarConstructor() {
console.log('Running test: testCarConstructor');
let wereAnyMocksUsed = false;
const mock = () => { wereAnyMocksUsed = true; }
Vehicle.constructor = mock;
Vehicle.prototype.constructor = mock;
Car.prototype.super = mock;
Car.super = mock;
new Car('Nissan', 'Pulsar', 4);
console.log('Finished test. Were any mocks used?', wereAnyMocksUsed);
}
testCarConstructor();
EDIT: This is only example code, not the actual code I am working on. I think that the shorter example can keep it simple without needing backstory on the names and inheritance structure of the code I'm actually working on.