0

I have this code in my 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);
        });
    });


})

I want to clear my database before conducting 'should signup successfully ' test so that I dont create duplicate records as it will fail since email is set to be unique validation

skyboyer
  • 22,209
  • 7
  • 57
  • 64
sriram hegde
  • 2,301
  • 5
  • 29
  • 43

1 Answers1

0

I got it I just have to do this:

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);
        });
    });
sriram hegde
  • 2,301
  • 5
  • 29
  • 43