Skip to content

Commit

Permalink
Improve error handling test cases for service routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndpnt committed Jun 13, 2024
1 parent 69b535c commit 2786517
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions src/routes/services.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,32 +254,67 @@ describe('Routes: Services', () => {
});

context('when an error occurs in one of the underlying collections', () => {
before(async () => {
nock.cleanAll();
nock('http://collection-1.example').persist().get('/collection-api/v1/services').reply(200, COLLECTION_1_SERVICES_RESULT);
nock('https://2.collection.example').get('/collection-api/v1/services').replyWithError({
message: 'something went wrong',
code: 'ERROR',
context('with an error in the JSON response', () => {
const ERROR_MESSAGE = 'something went wrong';

before(async () => {
nock.cleanAll();
nock('http://collection-1.example').persist().get('/collection-api/v1/services').reply(200, COLLECTION_1_SERVICES_RESULT);
nock('https://2.collection.example').get('/collection-api/v1/services').replyWithError({
message: ERROR_MESSAGE,
code: 'ERROR',
});
response = await request(app).get(`${BASE_PATH}/services`);
});
response = await request(app).get(`${BASE_PATH}/services`);
});

it('returns a non empty results array', () => {
expect(response.body.results).to.not.be.empty;
});
it('returns a non empty results array', () => {
expect(response.body.results).to.not.be.empty;
});

it('returns a non empty failures array', () => {
expect(response.body.failures).to.not.be.empty;
});
it('returns a non empty failures array', () => {
expect(response.body.failures).to.not.be.empty;
});

describe('failure entries', () => {
it('have a collection name', () => {
expect(response.body.failures.map(failure => failure.collection)).to.have.members(['collection-2']);
});

describe('failure entries', () => {
it('have a collection name', () => {
expect(response.body.failures.map(failure => failure.collection)).to.have.members(['collection-2']);
it('have a detailed error message', () => {
response.body.failures.forEach(failure => {
expect(failure).to.have.property('message').that.has.string(ERROR_MESSAGE);
});
});
});
});

it('have a detailed error message', () => {
response.body.failures.forEach(failure => {
expect(failure).to.have.property('message').that.has.string('something went wrong');
[ 403, 404, 500, 502 ].forEach(errorCode => {
context(`with a HTTP ${errorCode} error`, () => {
before(async () => {
nock.cleanAll();
nock('http://collection-1.example').persist().get('/collection-api/v1/services').reply(200, COLLECTION_1_SERVICES_RESULT);
nock('https://2.collection.example').get('/collection-api/v1/services').reply(errorCode);
response = await request(app).get(`${BASE_PATH}/services`);
});

it('returns a non empty results array', () => {
expect(response.body.results).to.not.be.empty;
});

it('returns a non empty failures array', () => {
expect(response.body.failures).to.not.be.empty;
});

describe('failure entries', () => {
it('have a collection name', () => {
expect(response.body.failures.map(failure => failure.collection)).to.have.members(['collection-2']);
});

it('have a detailed error message', () => {
response.body.failures.forEach(failure => {
expect(failure).to.have.property('message').that.has.string(errorCode);
});
});
});
});
});
Expand Down

0 comments on commit 2786517

Please sign in to comment.