首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mocha.js:即使测试套件失败也要运行"after“钩子

Mocha.js:即使测试套件失败也要运行"after“钩子
EN

Stack Overflow用户
提问于 2013-05-25 15:15:54
回答 1查看 11.3K关注 0票数 14

即使其中一个测试(套件)失败,也可以运行"after“钩子吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-02 23:53:37

是的,当测试失败时,afterafterEach钩子都应该运行。

请参阅以下github问题以了解相关讨论和更改:#94#125#143#690

下面是一个例子来证明我的说法:

代码语言:javascript
复制
describe('test', function() {
  after(function() { console.log('after'); });
  afterEach(function() { console.log('afterEach'); });

  it('fails sync', function(done) {
    after(function() { console.log('inner after 1'); });
    throw new Error('failed');
  });

  it('fails async', function(done) {
    after(function() { console.log('inner after 2'); });
    process.nextTick(function() {
      throw new Error('failed');
    });
  });
});

它使用mocha 1.1.12生成以下输出:

代码语言:javascript
复制
  ․afterEach
․afterEach
after
inner after 1
inner after 2


  0 passing (5 ms)
  2 failing

1) test fails sync:
 Error: failed
  at Context.<anonymous> (/private/tmp/so/test/main.js:7:11)
  at Test.Runnable.run (/private/tmp/so/node_modules/mocha/lib/runnable.js:194:15)
  at Runner.runTest (/private/tmp/so/node_modules/mocha/lib/runner.js:355:10)
  at /private/tmp/so/node_modules/mocha/lib/runner.js:401:12
  at next (/private/tmp/so/node_modules/mocha/lib/runner.js:281:14)
  at /private/tmp/so/node_modules/mocha/lib/runner.js:290:7
  at next (/private/tmp/so/node_modules/mocha/lib/runner.js:234:23)
  at Object._onImmediate (/private/tmp/so/node_modules/mocha/lib/runner.js:258:5)
  at processImmediate [as _immediateCallback] (timers.js:330:15)

2) test fails async:
 Error: failed
  at /private/tmp/so/test/main.js:13:12
  at process._tickCallback (node.js:415:13)
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16747367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档