Skip to content

Commit 181555a

Browse files
committed
Various cleanup
1 parent 3124202 commit 181555a

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

example-application/data-access/order-repository.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = class OrderRepository {
4343

4444
async addOrder(orderDetails) {
4545
const addingResponse = await orderModel.create(orderDetails);
46+
4647
return addingResponse.dataValues
4748
}
4849

example-application/error-handling.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
const mailer = require('./libraries/mailer');
22
const logger = require('./libraries/logger');
3-
const { HttpStatus } = require('@nestjs/common');
4-
const { RuleTester } = require('eslint');
53

64
// This file simulates real-world error handler that makes this component observable
75
const errorHandler = {
86
handleError: async (errorToHandle) => {
97
try {
10-
logger.error(`Error occurred`);
118
logger.error(errorToHandle);
129
metricsExporter.fireMetric('error', {
1310
errorName: errorToHandle.name || 'generic-error',

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"test": "jest",
77
"test:dev": "jest --watch --silent --runInBand",
8-
"test:dev:debug": "node --inspect=9229 node_modules/.bin/jest --watch --runInBand --silent",
8+
"test:dev:debug": "node --async-stack-traces --inspect=9229 node_modules/.bin/jest --watch --runInBand",
99
"test:nestjs": "jest --config='./recipes/nestjs/ts-jest.config.js'",
1010
"test:dev:verbose": "jest --watch --verbose",
1111
"test:mocha": "mocha recipes/mocha/basic-tests.test.js --exit --require recipes/mocha/hooks.js",

recipes/error-handling/test/error-handling.tests.js

+43-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const express = require('express');
12
const axios = require('axios');
23
const sinon = require('sinon');
34
const nock = require('nock');
@@ -61,10 +62,12 @@ describe('Error Handling', () => {
6162
productId: 2,
6263
mode: 'approved',
6364
};
64-
65+
6566
sinon
6667
.stub(OrderRepository.prototype, 'addOrder')
67-
.rejects(new AppError('saving-failed', 'Order could not be saved', 500));
68+
.rejects(
69+
new AppError('saving-failed', 'Order could not be saved', 500)
70+
);
6871
const loggerDouble = sinon.stub(logger, 'error');
6972

7073
//Act
@@ -87,7 +90,11 @@ describe('Error Handling', () => {
8790
mode: 'approved',
8891
};
8992

90-
const errorToThrow = new AppError('example-error', 'some example message', 500);
93+
const errorToThrow = new AppError(
94+
'example-error',
95+
'some example message',
96+
500
97+
);
9198
sinon.stub(OrderRepository.prototype, 'addOrder').throws(errorToThrow);
9299
const metricsExporterDouble = sinon.stub(metricsExporter, 'fireMetric');
93100

@@ -126,7 +133,7 @@ describe('Error Handling', () => {
126133
expect(processExitListener.called).toBe(true);
127134
});
128135

129-
test('When unknown exception is throw during request, Then its treated as trusted error and the process stays alive', async () => {
136+
test('When unknown exception is throw during request, Then the process stays alive', async () => {
130137
//Arrange
131138
const orderToAdd = {
132139
userId: 1,
@@ -146,11 +153,37 @@ describe('Error Handling', () => {
146153
expect(processExitListener.called).toBe(false);
147154
});
148155
});
149-
150156
describe('Various Throwing Scenarios And Locations', () => {
151-
test.todo(
152-
"When an error is thrown during startup, then it's handled correctly"
153-
);
157+
test('When unhandled exception is throw, Then the logger reports correctly', async () => {
158+
//Arrange
159+
const loggerDouble = sinon.stub(logger, 'error');
160+
const errorToThrow = new Error('An error that wont be caught 😳');
161+
162+
//Act
163+
process.emit('uncaughtException', errorToThrow);
164+
165+
// Assert
166+
expect(loggerDouble.lastCall.firstArg).toMatchObject(errorToThrow);
167+
});
168+
169+
test.skip('When an error is thrown during startup for more than 3 times, then the process exits', async () => {
170+
// Arrange
171+
const noneExistingPort = 80;
172+
sinon.restore();
173+
//const app = express();
174+
const http = require('http');
175+
sinon.stub(http, 'createServer').throws(new Error('Can not start'));
176+
//sinon.stub(app, 'listen').throws(new Error());
177+
//process.env.PORT = noneExistingPort;
178+
const processExitListener = sinon.stub(process, 'exit');
179+
180+
// Act
181+
await initializeWebServer();
182+
183+
// Assert
184+
expect(processExitListener.called).toBe(true);
185+
});
186+
154187
test.todo(
155188
"When an error is thrown during web request, then it's handled correctly"
156189
);
@@ -187,14 +220,14 @@ describe('Error Handling', () => {
187220

188221
sinon.stub(OrderRepository.prototype, 'addOrder').throws(errorInstance);
189222
const metricsExporterDouble = sinon.stub(metricsExporter, 'fireMetric');
190-
const consoleErrorDouble = sinon.stub(console, 'error');
223+
const loggerDouble = sinon.stub(logger, 'error');
191224

192225
//Act
193226
await axiosAPIClient.post('/order', orderToAdd);
194227

195228
//Assert
196229
expect(metricsExporterDouble.called).toBe(true);
197-
expect(consoleErrorDouble.called).toBe(true);
230+
expect(loggerDouble.called).toBe(true);
198231
}
199232
);
200233
});

0 commit comments

Comments
 (0)