Skip to content

Commit 7843204

Browse files
authored
Merge pull request #171 from testjavascript/refactor/typescript
TypeScript support, mocking chapter, a generic test file setup helper and more
2 parents d39e40a + abd1897 commit 7843204

File tree

57 files changed

+14135
-18802
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+14135
-18802
lines changed

.github/workflows/nodejs.yml

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,27 @@ on:
88
branches:
99
- '*'
1010
pull_request:
11-
branches: [ master ]
11+
branches: [master]
1212

1313
jobs:
1414
build:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v2
18+
- uses: actions/checkout@v4
19+
- name: Setup Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version-file: '.nvmrc'
23+
- name: Cache node_modules
24+
uses: actions/cache@v4
25+
with:
26+
path: |
27+
node_modules
28+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
29+
restore-keys: |
30+
${{ runner.os }}-node-
1931
20-
- name: Read .nvmrc
21-
run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
22-
id: nvm
23-
24-
- name: Use Node.js (.nvmrc)
25-
uses: actions/setup-node@v2
26-
with:
27-
node-version: "${{ steps.nvm.outputs.NVMRC }}"
28-
29-
- run: npm ci
30-
- run: npm test
31-
- run: npm run test:nestjs
32-
- run: npm run test:mocha
32+
- run: npm ci
33+
- run: npm test
34+
- run: npm run test:nestjs

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18
1+
22.11

README.md

Lines changed: 502 additions & 322 deletions
Large diffs are not rendered by default.

recipes/message-queue/anti-pattern.message-queue.test.js renamed to anti-patterns/anti-pattern.message-queue.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const MessageQueueClient = require('../../example-application/libraries/message-queue-client');
1+
const MessageQueueClient = require('../example-application/libraries/message-queue-client');
22

33
beforeEach(async () => {
44
const messageQueueClient = new MessageQueueClient();

example-application/business-logic/order-service.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const util = require('util');
2-
const axios = require('axios');
2+
const axios = require('axios').default;
33
const mailer = require('../libraries/mailer');
4-
const axiosRetry = require('axios-retry');
4+
const axiosRetry = require('axios-retry').default;
55
const OrderRepository = require('../data-access/order-repository');
66
const { AppError } = require('../error-handling');
77
const MessageQueueClient = require('../libraries/message-queue-client');
8+
const { AxiosError } = require('axios');
89

9-
const axiosHTTPClient = axios.create();
10-
axiosRetry(axiosHTTPClient, { retries: 3 });
10+
const axiosHTTPClient = axios.create({});
1111

1212
module.exports.addOrder = async function (newOrder) {
1313
// validation
@@ -17,31 +17,38 @@ module.exports.addOrder = async function (newOrder) {
1717

1818
// verify user existence by calling external Microservice
1919
const userWhoOrdered = await getUserFromUsersService(newOrder.userId);
20-
20+
console.log('userWhoOrdered', userWhoOrdered);
2121
if (!userWhoOrdered) {
22+
console.log('The user was not found');
2223
throw new AppError(
2324
'user-doesnt-exist',
2425
`The user ${newOrder.userId} doesnt exist`,
25-
404
26+
404,
2627
);
2728
}
2829

30+
// A little logic to calculate the total price
31+
if (newOrder.isPremiumUser) {
32+
newOrder.totalPrice = Math.ceil(newOrder.totalPrice * 0.9);
33+
}
34+
2935
// save to DB (Caution: simplistic code without layers and validation)
3036
const DBResponse = await new OrderRepository().addOrder(newOrder);
3137

38+
console.log('DBResponse', DBResponse);
3239
if (process.env.SEND_MAILS === 'true') {
3340
await mailer.send(
3441
'New order was placed',
3542
`user ${DBResponse.userId} ordered ${DBResponse.productId}`,
36-
43+
3744
);
3845
}
3946

4047
// We should notify others that a new order was added - Let's put a message in a queue
4148
await new MessageQueueClient().publish(
4249
'order.events',
4350
'order.events.new',
44-
newOrder
51+
newOrder,
4552
);
4653

4754
return DBResponse;
@@ -60,19 +67,21 @@ async function getUserFromUsersService(userId) {
6067
const getUserResponse = await axiosHTTPClient.get(
6168
`http://localhost/user/${userId}`,
6269
{
63-
timeout: 2000,
70+
timeout: process.env.HTTP_TIMEOUT
71+
? parseInt(process.env.HTTP_TIMEOUT)
72+
: 2000,
6473
validateStatus: (status) => {
6574
return status < 500;
6675
},
67-
}
76+
},
6877
);
6978
return getUserResponse.data;
7079
} catch (error) {
7180
if (error?.code === 'ECONNABORTED') {
7281
throw new AppError(
7382
'user-verification-failed',
7483
`Request to user service failed so user cant be verified`,
75-
503
84+
503,
7685
);
7786
}
7887

example-application/data-access/migrations/20191229152126-entire-schema.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ module.exports = {
1515
mode: {
1616
type: Sequelize.STRING,
1717
},
18+
contactEmail: {
19+
type: Sequelize.STRING,
20+
},
1821
userId: {
1922
type: Sequelize.INTEGER,
2023
},
2124
productId: {
2225
type: Sequelize.INTEGER,
2326
},
27+
totalPrice: {
28+
type: Sequelize.INTEGER,
29+
},
30+
isPremiumUser: {
31+
type: Sequelize.BOOLEAN,
32+
},
2433
createdAt: {
2534
allowNull: false,
2635
type: Sequelize.DATE,
@@ -40,9 +49,9 @@ module.exports = {
4049
},
4150
name: {
4251
type: Sequelize.STRING,
43-
}
52+
},
4453
});
4554
},
4655

47-
down: (queryInterface, Sequelize) => queryInterface.dropTable("Orders"),
56+
down: (queryInterface, Sequelize) => queryInterface.dropTable('Orders'),
4857
};

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = class OrderRepository {
1111
'shop',
1212
'myuser',
1313
'myuserpassword',
14-
sequelizeConfig
14+
sequelizeConfig,
1515
);
1616
orderModel = repository.define('Order', {
1717
id: {
@@ -30,16 +30,24 @@ module.exports = class OrderRepository {
3030
userId: {
3131
type: Sequelize.INTEGER,
3232
},
33+
contactEmail: {
34+
type: Sequelize.STRING,
35+
},
3336
productId: {
3437
type: Sequelize.INTEGER,
3538
},
39+
totalPrice: {
40+
type: Sequelize.INTEGER,
41+
},
42+
isPremiumUser: {
43+
type: Sequelize.BOOLEAN,
44+
},
3645
});
3746
}
3847
}
3948

4049
async getOrderById(id) {
4150
return await orderModel.findOne({ where: { id } });
42-
4351
}
4452

4553
async addOrder(orderDetails) {

0 commit comments

Comments
 (0)