I have below test which hits an endpoint. The service method stub throws an error.
Any error throws in app is handled by global error handler, and the handler return an error with status 500 like:
{
success: false,
message: "Something went wrong!"
}
/* eslint-disable no-param-reassign */
const test = require('ava');
const sinon = require('sinon');
const request = require('supertest');
require('dotenv').config();
// dependency to be stubbed
const { CountryService } = require('../../src/components/country/country.component');
// stubs
const error = new Error('Some Error');
const doGetCountries = sinon.stub(CountryService, 'doGetCountries').throws(error);
const app = require('../../src/app');
test.before(async (t) => {
t.context.stubs = {
doGetCountries,
};
t.context.apiUrl = '/api/v1/countries';
t.context.server = request(app);
});
test.after.always((t) => {
delete require.cache[require.resolve('../../src/app')]; // kills server
});
test('Throws Internal Server', async (t) => {
const { server, apiUrl, stubs } = t.context;
const res = await server
.get(apiUrl)
.set('accept', 'application/json');
t.true(res.status === 500);
t.true(stubs.doGetCountries.calledOnce);
});
When I run the test it fails with no error message but
npm ERR! Test failed. See above for more details.
The test passes if do studb.resolve({}) though.