簡體   English   中英

在Jest Supertest中開始集成測試之前清除測試數據庫

[英]Cleaning out test database before starting integration test in jest supertest

我的user.test.js中有以下代碼:

const request = require('supertest');
let server ;
const {User} = require('./../../models/user');





describe('/users', () => {

    beforeEach(() => server =  require('./../../index'));
    afterEach(() => server.close());

    describe('POST /users', () => {

        it('should signup successfully', async () => {




           const res = await 
           request(server).
           post('/users').
           send({ email : 'abc@gmail.com' , password: 'abcassasa'})
           expect(res.status).toBe(200);
        });
    });


})

我想在進行“應該成功注冊”測試之前清除數據庫,以免創建重復記錄,因為電子郵件已設置為唯一驗證,因此它將失敗

我明白了,我只需要這樣做:

describe('POST /users', () => {

        beforeEach(async () => {
              await User.remove({});
          });

        it('should signup successfully', async () => {




           const res = await 
           request(server).
           post('/users').
           send({ email : 'sriramhegde@gmail.com' , password: 'Na321tional'})
           expect(res.status).toBe(200);
        });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM