简体   繁体   English

为什么我在Mocha中的before()钩子根本不运行?

[英]Why doesn't my before() hook in Mocha run at all?

I have written this following code in a file named "example.js": 我已经在名为“ example.js”的文件中编写了以下代码:

console.log('HI HI HI HI');

describe('hooks', function() {
  console.log('before before');
  before(function() {
    console.log('ok');
  });
  console.log('after before');  
})

Output of the code when i run "mocha example.js" is: 当我运行“ mocha example.js”时,代码的输出为:

HI HI HI HI
before before
after before
  0 passing (1ms)

Why didn't the "ok" get printed? 为什么没有打印出“ ok”? I thought the before() hook runs before all the code in the describe() block? 我以为before()挂钩先于describe()块中的所有代码运行?

It is not printed because before is run before a test, and you don't have any. 它不会被打印,因为before是在测试before运行的,并且您没有任何内容。

Try add a test then it should run 尝试添加测试,然后它应该运行

console.log('1');

describe('hooks', function() {
  console.log('2');
  before(function() {
    console.log('4');
  });

  console.log('3');
  it('description', function() {
    console.log('5');
    // nothing more here but still a test
  })
})

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

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