无论测试失败,Mocha中的挂钩都会运行吗?

时间:2013-07-25 21:09:42

标签: node.js mocha

就像标题所说的那样。即使测试失败,Mocha中的before / after / beforeEach / afterEach挂钩也能正常工作吗?

1 个答案:

答案 0 :(得分:1)

是的,即使测试失败,它们也会运行。 但是,如果使用bail选项运行Mocha,则拆卸挂钩将不会运行。

这里只是对现有的测试套件进行了快速检查 代码:

setup(function() {
    console.log("1");
});
teardown(function() {
    console.log("2");
});

执行:

D:\Dev\JS\toposort>mocha


  Toposort
    ◦ should sort correctly: 1
    √ should sort correctly
2
    ◦ should find cyclic dependencies: 1
    1) should find cyclic dependencies
2
    ◦ #2 - should add the item if an empty array of dependencies is passed: 1
    √ #2 - should add the item if an empty array of dependencies is passed
2


  2 passing (15 ms)
  1 failing

  1) Toposort should find cyclic dependencies:
     AssertionError: expected [Function] to not throw 'Error' but [Error: Cyclic dependency found. '3' is dependent of itself
.] was thrown

D:\Dev\JS\toposort>mocha --bail


  Toposort
    ◦ should sort correctly: 1
    √ should sort correctly
2
    ◦ should find cyclic dependencies: 1
    1) should find cyclic dependencies


  1 passing (14 ms)
  1 failing

  1) Toposort should find cyclic dependencies:
     AssertionError: expected [Function] to not throw 'Error' but [Error: Cyclic dependency found. '3' is dependent of itself
.] was thrown