【问题标题】:Order of Operations in `before` and `describe` blocks in Mocha/Chai Node TestsMocha/Chai 节点测试中的“之前”和“描述”块中的操作顺序
【发布时间】:2019-10-22 18:11:36
【问题描述】:

为了尝试了解我的一个 Mocha 测试的执行顺序(我看到函数执行乱序),我创建了以下简单的测试用例:

describe("methods", function () {
  before(async function () {
    console.log('---------- BEFORE ------------');
    let sum = 1 + 2;
    console.log('sum: ', sum);
  });
  describe("test case", async function () {
    console.log('---------- AFTER ------------');
  });
});

再次,我想在这里检查的是操作顺序。果然,在这种情况下,打印到控制台的所有内容都是------------ AFTER ------------before 块中的日志记录根本不会显示在控制台中。

这是为什么?我在这里错过了什么?

在我的实际测试用例中,我使用before 块来创建和保存文档。然后在下一个describe 块中,我在数据库中查找它。当我遇到在创建之前查找文档的问题时,我决定做一些更简单的测试。因此,您在上面看到了什么。

【问题讨论】:

    标签: node.js mongodb mocha.js chai


    【解决方案1】:

    这是因为“之前”块仅在 一个 IT

    之前执行

    尝试执行此操作:

    describe("methods", function () {
      before(async function () {
        console.log('---------- BEFORE ------------');
        let sum = 1 + 2;
        console.log('sum: ', sum);
      });
      it('should be a test', function(){
    
        console.log('test');
    
      });
      describe("test case", async function () {
        console.log('---------- AFTER ------------');
      });
    });
    

    查看这篇文章了解更多信息: https://gist.github.com/samwize/8877226

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 2014-11-14
      • 2019-05-17
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多