【问题标题】:Function Defined in Before Block Not Finishing Before Tests Run in Mocha在 Mocha 中的测试运行之前未完成之前块中定义的函数
【发布时间】:2019-10-25 04:01:48
【问题描述】:

我正在我的 Node 项目中使用 Mocha/Chai 创建一些测试。在一个特定的测试中,我需要先将document 保存到数据库中,然后检索它并对找到的文档进行一些检查。

我遇到的问题是 it 检查在文档完成保存到数据库之前运行。我不明白为什么会发生这种情况,因为我正在 before() 块中进行保存——根据 Mocha 文档,它应该在同一个描述块中的测试之前运行,请参阅下面的文档:

> With its default "BDD"-style interface, Mocha provides the hooks
> before(), after(), beforeEach(), and afterEach(). These should be used
> to set up preconditions and clean up after your tests.
> 
> describe('hooks', function() {   before(function() {
>     // runs before all tests in this block   });
> 
>   after(function() {
>     // runs after all tests in this block   });
> 
>   beforeEach(function() {
>     // runs before each test in this block   });
> 
>   afterEach(function() {
>     // runs after each test in this block   });
> 
>   // test cases });

澄清一下,当我第二次(以及所有后续时间)运行测试时,所有检查都通过了,因为到那时它可以在 db.xml 中找到文档。

我在这里错过了什么?

这是我的代码:

const assert = require("chai").assert;
const expect = require("chai").expect;
const chai = require("chai");
chai.use(require("chai-datetime"));
const Agenda = require('agenda');

const config = require('./../../configuration');
const url = config.get('MONGO_URL');
const dbName = config.get('MONGO_DATABASE');
const collection = config.get('MONGO_COLLECTION');
const createAgendaJob = require('./../../lib/agenda-jobs/contact-firstname-to-proper-case');

const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(url);

describe("Contact FirstName to Proper Case", async function () {
  const jobName = "Contact FirstName To Proper Case";
  const testDate = new Date(2019, 01, 01);
  let result;
  let agenda;
  this.timeout(2000);
  before(async function () {
    const connectionOpts = {
      db: {
        address: `${url}/${dbName}`,
        collection
      }
    };

    agenda = new Agenda(connectionOpts);
    await new Promise(resolve => agenda.once('ready', resolve));
    await createAgendaJob(agenda); // Here is where I save the job to the DB
  });
  client.connect(async function (err) {
    assert.equal(null, err);

    const db = await client.db(dbName);

    result = await db.collection("jobs").findOne({
      "name": jobName
    });

    client.close();
  });
  it("should have a property 'name'", function () {
    expect(result).to.have.property("name");
  });
  it("should have a 'name' of 'Contact FirstName To Proper Case'", function () {
    expect(result.name).to.equal("Contact FirstName To Proper Case");
  });
  it("should have a property 'type'", function () {
    expect(result).to.have.property("type");
  });
  it("should have a 'type' of 'normal'", function () {
    expect(result.type).to.equal("normal");
  });
  it("should have a property 'repeatTimezone'", function () {
    expect(result).to.have.property("repeatTimezone");
  });
  it("should have a property 'repeatInterval'", function () {
    expect(result).to.have.property("repeatInterval");
  });
  it("should have a property 'lastModifiedBy'", function () {
    expect(result).to.have.property("lastModifiedBy");
  });
  it("should have a property 'nextRunAt'", function () {
    expect(result).to.have.property("nextRunAt");
  });
  it("should return a date for the 'nextRunAt' property", function () {
    assert.typeOf(result.nextRunAt, "date");
  });
  it("should 'nextRunAt' to be a date after test date", function () {
    expect(result.nextRunAt).to.afterDate(testDate);
  });
});

我还尝试将it 检查放在后续的describe() 块中,但这也不起作用(相同的结果——仅在第二次运行测试文件时找到文档):

const assert = require("chai").assert;
const expect = require("chai").expect;
const chai = require("chai");
chai.use(require("chai-datetime"));
const Agenda = require('agenda');

const config = require('./../../configuration');
const url = config.get('MONGO_URL');
const dbName = config.get('MONGO_DATABASE');
const collection = config.get('MONGO_COLLECTION');
const createAgendaJob = require('./../../lib/agenda-jobs/contact-firstname-to-proper-case');

const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(url);

describe("Contact FirstName to Proper Case", async function () {
  const jobName = "Contact FirstName To Proper Case";
  const testDate = new Date(2019, 01, 01);
  let result;
  let agenda;
  this.timeout(2000);
  before(async function () {
    const connectionOpts = {
      db: {
        address: `${url}/${dbName}`,
        collection
      }
    };

    agenda = new Agenda(connectionOpts);
    await new Promise(resolve => agenda.once('ready', resolve));
    await createAgendaJob(agenda); // Here is where I save the job to the DB
  });
  client.connect(async function (err) {
    assert.equal(null, err);

    const db = await client.db(dbName);

    result = await db.collection("jobs").findOne({
      "name": jobName
    });

    client.close();
  });
  describe("Check Contact FirstName To ProperCase Found Job", function () {
    it("should have a property 'name'", function () {
      expect(result).to.have.property("name");
    });
    it("should have a 'name' of 'Contact FirstName To Proper Case'", function () {
      expect(result.name).to.equal("Contact FirstName To Proper Case");
    });
    it("should have a property 'type'", function () {
      expect(result).to.have.property("type");
    });
    it("should have a 'type' of 'normal'", function () {
      expect(result.type).to.equal("normal");
    });
    it("should have a property 'repeatTimezone'", function () {
      expect(result).to.have.property("repeatTimezone");
    });
    it("should have a property 'repeatInterval'", function () {
      expect(result).to.have.property("repeatInterval");
    });
    it("should have a property 'lastModifiedBy'", function () {
      expect(result).to.have.property("lastModifiedBy");
    });
    it("should have a property 'nextRunAt'", function () {
      expect(result).to.have.property("nextRunAt");
    });
    it("should return a date for the 'nextRunAt' property", function () {
      assert.typeOf(result.nextRunAt, "date");
    });
    it("should 'nextRunAt' to be a date after test date", function () {
      expect(result.nextRunAt).to.afterDate(testDate);
    });
  });
});

【问题讨论】:

    标签: node.js mocha.js chai


    【解决方案1】:

    它不是在before 完成之前运行it,而是在一切之前运行:

        client.connect(async function (err) {
         ...
        });
    

    Mocha,当创建 test suide 时运行代码如下:

    • 调用传递给describe的每个函数

      • 收集所有its 和before/after* 钩子
      • 那是你的client.connect sn-p 真正被调用的时候!
    • 然后,在创建整个套件/测试树之后,为每个套件

      • 在*钩子之前调用适当的
      • 调用所有its - 测试,可能被before/afterAll 钩子包围
      • 调用after钩子

    每个钩子/测试运行至少在您的情况下等待完成,因为您正确使用 async/await

    另外,请注意这是非常可疑的:

    await setTimeout(() => {
      console.log('finished pausing for 5 seconds...');
    }, 5000);
    

    这不会等待任何事情,因为setTimeout 返回计时器它(可能是数字)并且它没有很好地定义await 应该如何处理这些数据,因为这不是承诺。 当然,它不会等待 5 秒。

    正确的承诺等待如下所示:

    await new Promise(resolve => setTimeout(resolve, 5000))
    

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 2021-03-31
      相关资源
      最近更新 更多