I’m unable to user supertest with sinon to stub the same function twice in the same test file. How can I do this?
Here is the code I have currently:
const { app } = require('../../app');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const testConfig = require('../testConfig/testConfig');
const utils = require('../../helpers/utils');
const token = testConfig.token;
const header = { 'Authorization': `Bearer ${token}` };
const mockOrder = {
accountNumber: '1234',
orderSourceId: 1
}
const mockOrders = [mockOrder];
describe('Vendors routes', () => {
afterEach(() => {
sinon.restore();
})
describe('GET /orders/:accountNumber', () => {
it('should get an order record for provided Account Number', async () => {
sinon.stub(utils, 'getOrder').resolves(mockOrders);
const accountNumber = '1234';
const route = testConfig.createRoute(`/vendors/orders/${accountNumber}`);
const res = await request(app)
.get(route)
.set(header);
expect(res.status).to.be.equal(200);
})
it('should return 404 if an order is not found', async () => {
sinon.stub(utils, 'getOrder').resolves(undefined);
const accountNumber = '1234';
const route = testConfig.createRoute(`/vendors/orders/${accountNumber}`);
const res = await request(app)
.get(route)
.set(header);
expect(res.status).to.be.equal(404);
sinon.restore();
})
})
})
As you can see from above, I simply want to adjust what the function getOrder
resolves to. However I get the following error when doing this with supertest:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
The first test will pass, but the second one fails. Why is sinon not able to overwrite the stubbed function more than once? What’s going on exactly?