Skip to content

Commit 270a538

Browse files
committed
Genric message
1 parent a9a8e1a commit 270a538

11 files changed

+854
-362
lines changed

README.md

+458-277
Large diffs are not rendered by default.

example-application/entry-points/api.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const startWebServer = (): Promise<AddressInfo> => {
1616
expressApp.use(
1717
bodyParser.urlencoded({
1818
extended: true,
19-
})
19+
}),
2020
);
2121
expressApp.use(bodyParser.json());
2222
expressApp.use(authenticationMiddleware);
@@ -50,18 +50,18 @@ const defineRoutes = (expressApp: express.Application) => {
5050
async (
5151
req: express.Request,
5252
res: express.Response,
53-
next: express.NextFunction
53+
next: express.NextFunction,
5454
) => {
5555
try {
5656
console.log(
57-
`Order API was called to add new Order ${util.inspect(req.body)}`
57+
`Order API was called to add new Order ${util.inspect(req.body)}`,
5858
);
5959
const addOrderResponse = await orderService.addOrder(req.body);
6060
return res.json(addOrderResponse);
6161
} catch (error) {
6262
next(error);
6363
}
64-
}
64+
},
6565
);
6666

6767
// get existing order by id
@@ -92,7 +92,7 @@ const defineRoutes = (expressApp: express.Application) => {
9292
error: unknown,
9393
_req: express.Request,
9494
res: express.Response,
95-
_next: express.NextFunction
95+
_next: express.NextFunction,
9696
) => {
9797
if (!error || typeof error !== 'object') {
9898
await errorHandler.handleError(error);
@@ -106,7 +106,7 @@ const defineRoutes = (expressApp: express.Application) => {
106106
}
107107
await errorHandler.handleError(richError);
108108
res.status(status as number).end();
109-
}
109+
},
110110
);
111111
};
112112

example-application/test/createOrder.observabilityCheck.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,18 @@ describe('Error Handling', () => {
125125
});
126126
});
127127
describe('Various Throwing Scenarios And Locations', () => {
128-
test('When unhandled exception is throw, Then the logger reports correctly', async () => {
128+
test('When unhandled exception is throw, Then the logger+process exit reports correctly', async () => {
129129
//Arrange
130130
const loggerDouble = sinon.stub(logger, 'error');
131131
const errorToThrow = new Error('An error that wont be caught 😳');
132+
const processExitListener = sinon.stub(process, 'exit');
132133

133134
//Act
134135
process.emit('uncaughtException', errorToThrow);
135136

136137
// Assert
137138
expect(loggerDouble.lastCall.firstArg).toMatchObject(errorToThrow);
139+
expect(processExitListener.called).toBe(true);
138140
});
139141

140142
test.todo(

example-application/test/createOrder.responseCheck.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sinon from 'sinon';
12
import { buildOrder } from './order-data-factory';
23
import { testSetup } from './setup/test-file-setup';
34

@@ -87,6 +88,7 @@ describe('POST /orders', () => {
8788
expect(orderAddResult.status).toBe(401);
8889
});
8990

91+
9092
test('When ordered by a premium user, Then 10% discount is applied', async () => {
9193
//Arrange
9294
// ️️️✅ Best Practice: Use a dynamic factory to craft big payloads and still clarify which specific details

example-application/test/fooo.test.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { randomBytes } from 'crypto';
2+
import 'jest-extended';
3+
4+
// Test helper
5+
function buildWorkflowPreviewResponse(
6+
...workflows: { workflowType: string; status: string }[]
7+
) {
8+
const idsMap: Record<string, any> = {};
9+
10+
for (let index = 0; index < workflows.length; index++) {
11+
const workflow = workflows[index];
12+
const randomId = randomBytes(16).toString('hex');
13+
idsMap[randomId] = {
14+
WorkflowExcution: {
15+
WorkflowType: workflow.workflowType,
16+
status: workflow.status,
17+
},
18+
originKey: null,
19+
originWorkflowType: null,
20+
};
21+
}
22+
23+
return {
24+
rootId: expect.any(String),
25+
idsMap,
26+
};
27+
}
28+
29+
function buildWorkflowExecution(workflowExecution: {
30+
workflowType: string;
31+
status: string;
32+
}) {
33+
return {
34+
WorkflowExcution: {
35+
WorkflowType: workflowExecution.workflowType,
36+
status: workflowExecution.status,
37+
},
38+
originKey: null,
39+
originWorkflowType: null,
40+
};
41+
}
42+
43+
const codeUnderTest = () => {
44+
return {
45+
rootId: '123e4567-e89b-12d3-a456-426614174000',
46+
idsMap: {
47+
// Note: this is random uuid
48+
'123e4567-e89b-12d3-a456-426614172000': {
49+
WorkflowExcution: {
50+
WorkflowType: 'trustee',
51+
status: 'Draft',
52+
},
53+
originKey: null,
54+
originWorkflowType: null,
55+
},
56+
// Note: this is random uuid
57+
'762e4567-e89b-12d3-a456-426614172000': {
58+
WorkflowExcution: {
59+
WorkflowType: 'award-letter',
60+
status: 'Draft',
61+
},
62+
originKey: null,
63+
originWorkflowType: null,
64+
},
65+
},
66+
};
67+
};
68+
69+
test('foo', () => {
70+
// Arrange
71+
72+
//Act
73+
const receivedResult = codeUnderTest();
74+
75+
// Assert
76+
const workflowExecutions = Object.values(receivedResult.idsMap);
77+
expect(workflowExecutions).toIncludeAllPartialMembers([
78+
buildWorkflowExecution({ workflowType: 'award-letter', status: 'Draft' }),
79+
buildWorkflowExecution({ workflowType: 'trustee', status: 'Draft' }),
80+
81+
]);
82+
});

example-application/test/pool.ts

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ test('When order failed, send mail to admin', async () => {
5959
});
6060
});
6161

62-
6362
// ️️️✅ Best Practice: Check external calls
6463
test('When adding a new valid order, Then an email should be send to admin', async () => {
6564
//Arrange

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
coverageReporters: ['text-summary', 'lcov'],
1313
collectCoverageFrom: ['**/*.js', '!**/node_modules/**', '!**/test/**'],
1414
forceExit: true,
15+
setupFilesAfterEnv: ['jest-extended/all'],
1516
testEnvironment: 'node',
1617
notify: true,
1718
globalSetup: './example-application/test/setup/global-setup.js',

0 commit comments

Comments
 (0)