My JavaScript/TypeScript class (AAA
) extends another class (BBB
). The API of class BBB
is stable, but the implementation is not yet finalized. I just want to unit test some functions in class AAA
. However, I'm facing an issue in creating an instance of class AAA
due to the constructor call of class BBB
. You can find an example here:
BBB.ts:
class BBB {
constructor() {
throw new Error("BBB");
}
public say(msg: string): string {
return msg;
}
}
module.exports = BBB;
AAA.ts:
const BB = require("./BBB");
class AAA
extends BB {
public hello(): string {
return super.say("Hello!");
}
}
module.exports = AAA;
Test Script:
const AA = require("../src/AAA");
import sinon from "sinon";
describe("Hello Sinon", () => {
describe("#hello", () => {
it("#hello", async () => {
const stub = sinon.stub().callsFake(() => { });
Object.setPrototypeOf(AA, stub);
let a = new AA();
sinon.spy(a, "hello");
a.hello();
sinon.assert.calledOnce(a.hello);
sinon.assert.calledOnce(stub);
// how to verify that super.say has been called once with string "Hello!"?
});
});
});
I'm using sinonjs. However, I'm struggling to create an instance of AAA
in this case. If I can create one, I'm unsure how to verify the call to super.say
.
Thank you!
UPDATE: I have managed to create an instance of AAA
now, but I need guidance on how to verify the call to super.say
.