簡體   English   中英

如何確保 mocha 測試中的“this”可以訪問類屬性

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

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();

得到 x 是未定義的。

問題: this inside describe 指向完成不同的上下文,如何使 x 可用於this inside mocha 測試

在您的函數上使用箭頭函數() => this....bind

describe("test goes here", () => {
  it("sample test", () => {
    expect(this.x).to.be.eq(10);
  });
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM