Skip to content

Commit

Permalink
test: finish monitoring bay test
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedMattr committed Mar 26, 2024
1 parent 087ad57 commit 814081d
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions test/mission-monitoring-bay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ describe('Sensors test', () => {
Make the DAL throw this error: new AppError('db-is-unaccessible', false, 500)
*/
sinon
.stub(SensorsService.prototype, 'addEvent')
.rejects(new AppError('db-is-unaccessible', false, 500));
.stub(SensorsService.prototype, 'addEvent')
.rejects(new AppError('db-is-unaccessible', false, 500));

// 💡 TIP: Listen here to the process.exit method to check later whether it was called
if (process.exit.restore) {
Expand All @@ -180,8 +180,8 @@ describe('Sensors test', () => {

// Act
const receivedResult = await request(expressApp)
.post('/sensor-events')
.send(eventToAdd);
.post('/sensor-events')
.send(eventToAdd);

// Assert
// 💡 TIP: Check here whether process.exit was called
Expand All @@ -199,20 +199,53 @@ describe('Sensors test', () => {
if (process.exit.restore) {
process.exit.restore();
}
const spyOnLogger = sinon.spy(console, 'error');
const listenToProcessExit = sinon.stub(process, 'exit');

sinon
.stub(SensorsService.prototype, 'addEvent')
.rejects(new AppError('db-is-unaccessible', false, 500));

// Act
// 💡 TIP: Explicitly make the process object throw an uncaught exception:
// process.emit(
// 'uncaughtException', define an error object here)
//
process.emit(
'uncaughtException', new Error("random error"))


// Assert
expect(listenToProcessExit.called).toBe(true);
expect(spyOnLogger.calledWithMatch({
message: 'random error'
})).toBe(true)
});

// ✅🚀 TASK: Check the same like above, but for unhandled rejections (throw unhandledRejection, ensure the process and logger behaves as expected)
// 💡 TIP: The event process.on('unhandledRejection' , yourCallBack) fires when a rejected promise is not caught error is not caught and will lead to
// non-documented crash!
test('When unhandledRejection is thrown, then logger writes the mandatory fields and the process exits', async () => {
// Arrange
if (process.exit.restore) {
process.exit.restore();
}
const spyOnLogger = sinon.spy(console, 'error');
const listenToProcessExit = sinon.stub(process, 'exit');

sinon
.stub(SensorsService.prototype, 'addEvent')
.rejects(new AppError('db-is-unaccessible', false, 500));

// Act
// 💡 TIP: Explicitly make the process object throw an uncaught exception:
process.emit(
'unhandledRejection', new Error("random error"))


// Assert
expect(listenToProcessExit.called).toBe(true);
expect(spyOnLogger.calledWithMatch({
message: 'random error'
})).toBe(true)
});

// ✅🚀 TASK: Check that for any type of error that is being thrown, whether a valid error object or number or anything else - Our
// error handler is capable of handling it
Expand All @@ -238,16 +271,24 @@ describe('Sensors test', () => {
const eventToAdd = getSensorEvent();

// 💡 TIP: make here some code throw the 'errorInstance' variable
sinon
.stub(SensorsService.prototype, 'addEvent')
.rejects(errorInstance);

// 💡 TIP: We should listen here to the logger and metrics exporter - This is how we know that errors were handled
const metricsExporterDouble = sinon.stub(metricsExporter, 'fireMetric');
const consoleErrorDouble = sinon.stub(console, 'error');

//Act
// 💡 TIP: Approach the API like in any other test
const receivedResult = await request(expressApp)
.post('/sensor-events')
.send(eventToAdd);

//Assert
// 💡 TIP: Check that the consoleErrorDouble, metricsExporterDouble were indeed called
expect(metricsExporterDouble.called).toBe(true);
expect(consoleErrorDouble.called).toBe(true);
},
);
});
Expand Down

0 comments on commit 814081d

Please sign in to comment.