javascript - 如何确保 mocha 测试中的 'this' 可以访问类属性

标签 javascript node.js mocha.js

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来完成不同的上下文,如何使x可用于mocha测试中的this

最佳答案

在函数上使用箭头函数 () => this....bind

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

关于javascript - 如何确保 mocha 测试中的 'this' 可以访问类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60212359/

相关文章:

javascript - 通过npm安装自创作库时导入始终未定义

node.js - Foundation 使用 Libsass 导入,node-sass-middleware 未编译

mysql - 如何用node-mysql查询并写出结果?

node.js - 使用带有 TDD 接口(interface)的 mocha,ReferenceError

node.js - 运行 mocha 测试后如何终止 nodemon 进程?

javascript - Ruby on Rails - 集成现有元素 - 在哪里放置 css/js 文件并调用它们?

javascript - Browserify 和 Gulp 中的 require()

javascript - 一旦某个东西离开屏幕 'slid' 如何杀死它?

r - 在 R 中解析 SVG 路径

javascript - 为 Mongoose 运行多个 Mocha 测试文件被打破