Wednesday, 17 May 2017

Node + test: how to mock api using nock

Am trying to mock the api inside the unit test like the following:

const userResponse = {
    user: {
      _id: '58828157ce4e140820e23648',
      info: {
        email: 'fake@test.io',
        password: '1',
        name: 'test',
      },
};
  it('should register new user', (done) => {
    nock('http://localhost:5000')
      .post('/auth/register')
      .reply(200, userResponse);



    agent.post('/auth/register')
      .send({
        name: 'test',
        email: 'fake@test.io',
        password: '1',
      })
      .expect(200)
      .end((error, response) => {
        expect(response.body.user.info.email).to.equal('fake@test.io');
        expect(response.body.user.info.name).to.equal('test');
        done();
      });
  }).timeout(5000);

But what acutely happen is the request goes to the real internal api and not getting interrupted but nock.



via user3462064

No comments:

Post a Comment