How to make sure 'this' inside mocha test have access to class properties

≯℡__Kan透↙ 提交于 2020-04-16 06:09:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!