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

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

我在名为 "example.js" 的文件中编写了以下代码:

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

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

i 运行 "mocha example.js" 时的代码输出为:

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

为什么 "ok" 没有打印出来?我认为 before() 钩子 运行s 在 describe() 块中的所有代码之前?

它没有被打印出来,因为 before 是 运行 在测试之前,而你没有。

尝试添加一个测试然后它应该 运行

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
  })
})