Node.js 如何为POST和GET请求同步运行mocha测试

Node.js 如何为POST和GET请求同步运行mocha测试,node.js,express,mocha.js,couchbase,chai,Node.js,Express,Mocha.js,Couchbase,Chai,我为couchbase数据库构建了一个API,目前正在使用mocha编写HTTP请求的测试。我遇到的问题是,我的GET请求测试找不到与POST请求测试一起添加的数据。如果在运行测试之前数据库中存储了数据,则GET请求测试将通过。但是它返回的数据不包括来自请求后测试的新添加数据。我需要能够在干净的数据库上运行测试,并通过所有测试。我相信这个问题是由mocha的异步特性引起的,但我不确定如何解决这个问题。这是我的测试用例 依赖关系 const expect = require('chai').exp

我为couchbase数据库构建了一个API,目前正在使用mocha编写HTTP请求的测试。我遇到的问题是,我的GET请求测试找不到与POST请求测试一起添加的数据。如果在运行测试之前数据库中存储了数据,则GET请求测试将通过。但是它返回的数据不包括来自请求后测试的新添加数据。我需要能够在干净的数据库上运行测试,并通过所有测试。我相信这个问题是由mocha的异步特性引起的,但我不确定如何解决这个问题。这是我的测试用例

依赖关系

const expect = require('chai').expect;
const request = require('supertest');
const app = require('../src/app');
请求后测试

describe(`POST requests`, ()=>{
  it('Add new data', function() {
    return request(app)
      .post('/someendpoint')
      .set('Accept', 'application/json')
      .set('Authorization', auth_token)
      .send(newData)
      .expect(200)
      .then( function(res) {
        expect(res.body).be.a('Object');
        expect(res.body.id).be.a('string');
        id = res.body.id;
      })
    });
  });
describe('GET requests', function() {
  it('Get data', function() {
    return request(app)
      .get('/someendpoint')
      .set('Accept', 'application/json')
      .set('Authorization', auth_token)
      .expect(200)
      .then((res) => {
        expect(res.body).be.a('Array');
      })
    });
  });
    before('timer for get data', function(){
        for(let i = 0; i < 1000000000; i++){
        }
    })
    
    it('get data', function() {
        return request(app)
        .get('/someendpoint')
        .set('Accept', 'application/json')
        .set('Authorization', auth_token)
        .expect(200)
        .then((res) => {
            expect(res.body).be.a('Array');
        });
    });
获取请求测试

describe(`POST requests`, ()=>{
  it('Add new data', function() {
    return request(app)
      .post('/someendpoint')
      .set('Accept', 'application/json')
      .set('Authorization', auth_token)
      .send(newData)
      .expect(200)
      .then( function(res) {
        expect(res.body).be.a('Object');
        expect(res.body.id).be.a('string');
        id = res.body.id;
      })
    });
  });
describe('GET requests', function() {
  it('Get data', function() {
    return request(app)
      .get('/someendpoint')
      .set('Accept', 'application/json')
      .set('Authorization', auth_token)
      .expect(200)
      .then((res) => {
        expect(res.body).be.a('Array');
      })
    });
  });
    before('timer for get data', function(){
        for(let i = 0; i < 1000000000; i++){
        }
    })
    
    it('get data', function() {
        return request(app)
        .get('/someendpoint')
        .set('Accept', 'application/json')
        .set('Authorization', auth_token)
        .expect(200)
        .then((res) => {
            expect(res.body).be.a('Array');
        });
    });
还有,如果这有区别的话。每次发出POST请求时,都会在couchbase数据库中创建一个新文档

我发现了一个糟糕的解决方案(编辑)

before('获取数据的计时器',函数(){
对于(设i=0;i<100000000;i++){
}
})
它('get data',function(){
退货申请(app)
.get(“/someendpoint”)
.set('Accept','application/json')
.set('授权',身份验证令牌)
.expect(200)
。然后((res)=>{
expect(res.body).be.a('Array');
});
});

我认为您需要在“POST”测试中完成所有工作

我使用此方法测试post API:

//Pseudocode
request(app)
  .post()
  .expect().then({
    //do assertions
    request(app)
      .get()
      .expect().then({
        //do assertions
      })
  })
我知道您必须将每个测试划分到它的套件中,但这并不是将“get测试”划分到“POST套件”中。这是一个后测试,您可以使用GETAPI断言数据已插入。这完全是一个“后测试”

因此,代码是这样的(我喜欢使用done()):

it('should post',(done)=>{
请求(应用程序)
.post(“/endpoint”)
.expect(200)。然后(postResponse=>{
//断言
请求(应用程序)
.get(“/endpoint”)
.expect(200)。然后(getResponse=>{
//断言
完成()
}).catch(e=>{
完成(e)
})
}).catch(e=>{
完成(e)
})
})

那你为什么要把它分成两个测试呢?这是一个测试,有多个步骤。因为我有我所有的测试,基于请求的类型:POST、GET、PUT、DELETE、分组在一起,在不同的“描述”中。将它们结合在一起是解决此问题的唯一方法吗?您是否使用标记
--parallel
-p
运行测试?检查您的npm任务中的mocha命令,如果没有这个命令,mocha应该按照您编写测试的顺序运行测试。。