mocha测试中的上下文未定义

时间:2018-04-25 14:26:11

标签: node.js mocha

所以我使用mocha和node来测试一些api。我有一个测试

import { describe, before, it, xit } from 'mocha';

describe('test my scenarios dude', () => {
   before('do all my pre-test stuff', () => {
     const blah = blah;
   });

   it('tests my really useful test', () => {
     const testName = this.test.ctx.currentTest.fullTitle();
   });
});

但是'this'是未定义的。我怎样才能获得测试名称?

1 个答案:

答案 0 :(得分:1)

https://mochajs.org/#arrow-functions

正如文档所说Passing arrow functions (“lambdas”) to Mocha is discouraged 使用function代替

describe('test my scenarios dude', function() {
   before('do all my pre-test stuff', function() {
     const blah = blah;
   });

   it('tests my really useful test', function() {
     const testName = this.test.ctx.currentTest.fullTitle();
   });
});

您还可以阅读有关箭头功能here的更多信息。他们没有this