问题
const expect = require("chai").expect;
class Test
{
constructor(){ this.x= 10;}
run() {
describe("test goes here", function() {
it("sample test", function() {
expect(this.x).to.be.eq(10);
});
});
}
}
new Test().run();
getting x is undefined.
Issue : this inside describe points to complete different context, how to make x available to this inside mocha test
回答1:
Use arrow functions () => this...
or .bind
on your functions.
describe("test goes here", () => {
it("sample test", () => {
expect(this.x).to.be.eq(10);
});
});
来源:https://stackoverflow.com/questions/60212359/how-to-make-sure-this-inside-mocha-test-have-access-to-class-properties