简体   繁体   English

如果之前或它在摩卡测试中阻止失败,则之后块是否仍然执行

[英]Does after block still excecute if before or it block fail in mocha test

If a mocha test looks like this如果 mocha 测试看起来像这样

describe("create a user") {
    before("login to system") {}
    it("execute assertion") {}
    after("delete the user") {}
}

If either before or it block failed, does the after block still execute or no?如果 before 或 it 块失败,after 块是否仍然执行或不执行?

Yes, when test fails it will run after hook.是的,当测试失败时,它会after钩子after运行。

This code:这段代码:

before("login to system", () => {
  console.log("BEFORE")
  throw new Error('error');
})

it('testing...', (done) => {
  console.log("IT")
  done();
});

after("delete the user", () => {
  console.log("AFTER")
})

Output this:输出这个:

BEFORE
    1) "before all" hook: login to system for "testing..."
AFTER


  0 passing (14ms)
  1 failing

  1) States Get
       "before all" hook: login to system for "testing...":
     Error: error
      at Context.<anonymous> (test\index.js:22:11)
      at processImmediate (node:internal/timers:463:21)

Also you can check this github discussion: #1612 where here says:你也可以查看这个 github 讨论: #1612这里说:

Please see the example below, showing that after is correctly invoked after the beforeEach failure请看下面的例子,显示after在beforeEach失败后被正确调用

The code编码

// example.js
before(function() {
  console.log('before');
});

after(function() {
  console.log('after');
});

describe('suite', function() {
  beforeEach(function() {
    throw new Error('In beforeEach');
  });

  afterEach(function() {
    console.log('afterEach');
  });

  it('test', function() {
    // example
  });
});

And the output.和输出。

// Test run
$ mocha example.js

  before

  ․afterEach
after


  0 passing (7ms)
  1 failing

  1) suite "before each" hook:
     Error: In beforeEach
      at Context.<anonymous> (/Users/dstjules/git/mocha/example.js:11:11)
      at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:251:21)
      at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:259:10)
      at Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:358:17)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM