如何在 mocha-chai 测试中的 describe 块的两个 it 块之间插入时间延迟?

How to insert a time delay in between two it blocks of a describe block in mocha-chai test?

我想在我的 describe 块的两个 it() 之间插入一个时间延迟。第二个 it() 将获取在一段时间内推送的数据。在执行第一个 it() 之前,我将时间保存在 time1 变量中,然后使用下面的 setTimeout 函数通过发送 time1 和 time2(结束时间)来执行下一个 it()。

但是第二个 it() 似乎没有按照我的要求运行。我需要如何更改它或延迟调用第二个 it() 的方法是什么?

        var chai = require('chai');
        var chaiHttp = require('chai-http');    
        var should = chai.should();
        var expect = chai.expect;
        var http = require('http');
        chai.use(chaiHttp);
        var server;
        var mongodb;

        before(function (done) {
            server = require('../../../app.js'); // same as "node app.js"
            done();
        })

        after(function (done) {
            server.close();
        })

        describe('POST call to insert data into project', ()=> {
            var time1= new Date();
            time1 = time1.getTime();
            it('Creating project', (done) => {
                chai.request(server)
                .post('/create/myproject')
                .send()
                .end((err, res) => {
                    expect(res.statusCode).to.equal(200);
                    chai.request(server)
                    .post('/data/myproject')
                    .send(json_obj)
                    .end((err, res) => {
                         expect(res.statusCode).to.equal(200);
                         chai.request(server)
                         .get('/data/myproject')        
                         .end((err, res) => {
                             expect(res.statusCode).to.equal(200);
                         });
                    });
                    done();
                });
            });  //it    


            /* Below it() block should be executed after 30s with the time1 and 
               time2 variable */
            it("Doing total counting", (done) => {

             // this.timeout(30000);
             setTimeout(function () {
               var time2= new Date();
               time2 = time2.getTime();
               var url  = 'total?start_time=' + time1 + '&end_time=' + time2;
            chai.request(server)
                .get(url)
                .send()
                .end((err, res) => {
                    expect(res.statusCode).to.equal(200);

                    done();
                }
                )
        }, 30000)

    })     
});// describe

看起来您可以受益于 built in this.timeout() 功能

        this.timeout(30000);

        it("Doing total counting", (done) => {             
           var time2= new Date();
           time2 = time2.getTime();
           var url  = 'total?start_time=' + time1 + '&end_time=' + time2;
        chai.request(server)
        ...    
        setTimeout(3000, done)