在Promise.then函数内部调用时,Mochajs测试函数“ It”永远不会运行

时间:2019-01-04 12:54:26

标签: javascript mocha es6-promise async.js

我对mochajs测试有一个奇怪的问题。我不了解对异步和Promise函数的深入了解。

我在mytest.js中进行了跟踪测试

  describe(" store tests", () => {
  jsonfile.readFile(testDateFile).then(data => {
    describe("store is running", function() {
      it(`should show home page ${data}`, async function() {
        console.log("working");
      });
    });
  });
});

其中jsonfile.readFile返回一个承诺。

当我使用./node_modules/.bin/mocha test/verify-urls.js运行测试时,它永远不会进入“ it”。
但是当我添加另一个如下描述时

describe(" store tests", () => {
  describe("store is running", function() {
    it("should do nothing", () => {});
  });
  jsonfile.readFile(testDateFile).then(data => {
    describe("store is running", function() {
      it(`should show home page ${data}`, async function() {
        console.log("working");
      });
    });
  });
});

然后执行it(should show home page ${data}

任何了解该问题的建议将不胜感激。

编辑: 我想到了。问题出在jsonfile.readFile(testDateFile)中。由于jsonfile.readFile是异步的并且是不同的线程。 Mocha测试运行程序将处于当前线程中,而不会等待返回Promise。 我只是将readFile方法移到了测试之外,并在执行describe之前加载了它。

0 个答案:

没有答案
相关问题