Javascript mocha:如何运行依赖于测试结果的清理

Javascript mocha:如何运行依赖于测试结果的清理,javascript,unit-testing,mocha.js,chai,Javascript,Unit Testing,Mocha.js,Chai,如何在摩卡开始一些测试结果相关的清理? e、 g 一种可能的解决办法是: describe('some nice tests', () => { //cleanup functions let cleanupAfterFail = () => 'nothing'; let cleanupAfterSuccess = () => 'nothing'; afterEach(function() { if (this.currentTest.state ==

如何在摩卡开始一些测试结果相关的清理?
e、 g

一种可能的解决办法是:

describe('some nice tests', () => {
  //cleanup functions
  let cleanupAfterFail = () => 'nothing';
  let cleanupAfterSuccess = () => 'nothing';

  afterEach(function() {
    if (this.currentTest.state === 'failed') {
      cleanupAfterFail();
    } else {
      cleanupAfterSuccess();
    }

  });

  it('test1', () => {
    cleanupAfterFail = () => 'what i need in fail case 1';
    cleanupAfterSuccess = () => 'what i need in success case 1';
  });

  it('test2', () => {
    cleanupAfterFail = () => 'what i need in fail case 2';
    cleanupAfterSuccess = () => 'what i need in success case 2';
  });

});
但这似乎不是一种合适的方式

有什么合适的方法吗?如果是,情况如何

我用摩卡咖啡2.4.5
柴2.2.0

测试是在现实生活中使用量角器e2e+柴承诺。但我认为在这种情况下这并不重要。

我想知道你想出了什么?@EvgenyTimoshenko提出了一个糟糕的“可能的解决方案”。即使没有currentTest.state,因为我不再需要它。
describe('some nice tests', () => {
  //cleanup functions
  let cleanupAfterFail = () => 'nothing';
  let cleanupAfterSuccess = () => 'nothing';

  afterEach(function() {
    if (this.currentTest.state === 'failed') {
      cleanupAfterFail();
    } else {
      cleanupAfterSuccess();
    }

  });

  it('test1', () => {
    cleanupAfterFail = () => 'what i need in fail case 1';
    cleanupAfterSuccess = () => 'what i need in success case 1';
  });

  it('test2', () => {
    cleanupAfterFail = () => 'what i need in fail case 2';
    cleanupAfterSuccess = () => 'what i need in success case 2';
  });

});