express-generator-typescript 2.5.14 → 2.5.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,7 @@
27
27
  "jet-logger": "^2.0.1",
28
28
  "jet-paths": "^1.1.0",
29
29
  "jet-schema": "^1.4.2",
30
- "jet-validators": "^1.0.9",
30
+ "jet-validators": "^1.0.17",
31
31
  "jsonfile": "^6.1.0",
32
32
  "module-alias": "^2.2.2",
33
33
  "morgan": "^1.10.0"
@@ -1,6 +1,3 @@
1
- import { isValidDate } from 'jet-validators';
2
- import { traverseObject } from 'jet-validators/utils';
3
-
4
1
  import { IReqPropErr } from '@src/routes/common';
5
2
  import UserRepo from '@src/repos/UserRepo';
6
3
 
@@ -27,14 +24,3 @@ export async function cleanDatabase(): Promise<void> {
27
24
  UserRepo.deleteAllUsers(),
28
25
  ]);
29
26
  }
30
-
31
- /**
32
- * Put things you need to do to modify the resp body object here.
33
- */
34
- export function parseResBody(body: unknown) {
35
- return traverseObject((key, val, parentObj) => {
36
- if (key === 'created' && isValidDate(val)) {
37
- parentObj[key] = new Date(val);
38
- }
39
- })(body);
40
- }
@@ -1,18 +1,18 @@
1
1
  import supertest from 'supertest';
2
2
  import insertUrlParams from 'inserturlparams';
3
- import { parseJson } from 'jet-validators/utils';
3
+ import { parseJson, customDeepCompare } from 'jet-validators/utils';
4
4
 
5
5
  import app from '@src/server';
6
6
 
7
7
  import UserRepo from '@src/repos/UserRepo';
8
- import User from '@src/models/User';
8
+ import User, { IUser } from '@src/models/User';
9
9
  import { USER_NOT_FOUND_ERR } from '@src/services/UserService';
10
10
 
11
11
  import HttpStatusCodes from '@src/common/HttpStatusCodes';
12
12
  import { ValidationErr } from '@src/common/route-errors';
13
13
 
14
14
  import Paths from 'spec/support/Paths';
15
- import { cleanDatabase, IValidationErr, parseResBody } from 'spec/support';
15
+ import { cleanDatabase, IValidationErr } from 'spec/support';
16
16
 
17
17
 
18
18
  /******************************************************************************
@@ -21,24 +21,34 @@ import { cleanDatabase, IValidationErr, parseResBody } from 'spec/support';
21
21
 
22
22
  // Dummy users for GET req
23
23
  const DB_USERS = [
24
- User.new({ id: 1, name: 'Sean Maxwell', email: 'sean.maxwell@gmail.com' }),
25
- User.new({ id: 2, name: 'John Smith', email: 'john.smith@gmail.com' }),
26
- User.new({ id: 3, name: 'Gordan Freeman', email: 'gordan.freeman@gmail.com' }),
24
+ User.new({ name: 'Sean Maxwell', email: 'sean.maxwell@gmail.com' }),
25
+ User.new({ name: 'John Smith', email: 'john.smith@gmail.com' }),
26
+ User.new({ name: 'Gordan Freeman', email: 'gordan.freeman@gmail.com' }),
27
27
  ] as const;
28
28
 
29
+ // Don't compare "id" and "created" cause those are set dynamically by the
30
+ // database
31
+ const compareUserArrays = customDeepCompare({
32
+ onlyCompareProps: ['name', 'email'],
33
+ })
34
+
29
35
 
30
36
  /******************************************************************************
31
37
  Tests
38
+ IMPORTANT: Following TypeScript best practices, we test all scenarios that
39
+ can be triggered by a user under normal circumstances. Not all theoretically
40
+ scenarios (i.e. a failed database connection).
32
41
  ******************************************************************************/
33
42
 
34
43
  describe('UserRouter', () => {
35
44
 
36
45
  const agent = supertest.agent(app);
46
+ let dbUsers: IUser[] = [];
37
47
 
38
48
  // Run before all tests
39
49
  beforeEach(async () => {
40
50
  await cleanDatabase();
41
- await UserRepo.insertMult(DB_USERS);
51
+ dbUsers = await UserRepo.insertMult(DB_USERS);
42
52
  });
43
53
 
44
54
  // Get all users
@@ -50,9 +60,8 @@ describe('UserRouter', () => {
50
60
  agent
51
61
  .get(Paths.Users.Get)
52
62
  .end((_, res) => {
53
- parseResBody(res.body);
54
63
  expect(res.status).toBe(HttpStatusCodes.OK);
55
- expect(res.body).toEqual({ users: DB_USERS });
64
+ expect(compareUserArrays(res.body.users, DB_USERS)).toBeTruthy();
56
65
  done();
57
66
  });
58
67
  });
@@ -143,11 +152,14 @@ describe('UserRouter', () => {
143
152
  // Delete User
144
153
  describe(`"DELETE:${Paths.Users.Delete}"`, () => {
145
154
 
155
+ const getPath = (id: number) => insertUrlParams(Paths.Users.Delete, { id });
156
+
146
157
  // Success
147
158
  it(`should return a status code of "${HttpStatusCodes.OK}" if the ` +
148
159
  'request was successful.', done => {
160
+ const id = dbUsers[0].id;
149
161
  agent
150
- .delete(insertUrlParams(Paths.Users.Delete, { id: 3 }))
162
+ .delete(getPath(id))
151
163
  .end((_, res) => {
152
164
  expect(res.status).toBe(HttpStatusCodes.OK);
153
165
  done();
@@ -159,7 +171,7 @@ describe('UserRouter', () => {
159
171
  `"${USER_NOT_FOUND_ERR}" and a status code of ` +
160
172
  `"${HttpStatusCodes.NOT_FOUND}" if the id was not found.`, done => {
161
173
  agent
162
- .delete(insertUrlParams(Paths.Users.Delete, { id: -1 }))
174
+ .delete(getPath(-1))
163
175
  .end((_, res) => {
164
176
  expect(res.status).toBe(HttpStatusCodes.NOT_FOUND);
165
177
  expect(res.body.error).toBe(USER_NOT_FOUND_ERR);
@@ -1,5 +1,6 @@
1
1
  import { IUser } from '@src/models/User';
2
2
  import { getRandomInt } from '@src/util/misc';
3
+
3
4
  import orm from './MockOrm';
4
5
 
5
6
 
@@ -98,14 +99,21 @@ async function deleteAllUsers(): Promise<void> {
98
99
  * Insert multiple users. Can't do multiple at once cause using a plain file
99
100
  * for nmow.
100
101
  */
101
- async function insertMult(users: IUser[] | readonly IUser[]): Promise<void> {
102
- const db = await orm.openDb();
103
- db.users = [ ...db.users, ...users];
104
- return orm.saveDb(db);
102
+ async function insertMult(
103
+ users: IUser[] | readonly IUser[],
104
+ ): Promise<IUser[]> {
105
+ const db = await orm.openDb(),
106
+ usersF = [ ...users ];
107
+ for (const user of usersF) {
108
+ user.id = getRandomInt();
109
+ user.created = new Date();
110
+ }
111
+ db.users = [ ...db.users, ...users ];
112
+ await orm.saveDb(db);
113
+ return usersF;
105
114
  }
106
115
 
107
116
 
108
-
109
117
  /******************************************************************************
110
118
  Export default
111
119
  ******************************************************************************/
@@ -1 +1 @@
1
- {"users":[{"id":1,"name":"Bill","created":"2025-02-10T05:36:18.360Z","email":"sean.maxwell@gmail.com"},{"id":2,"name":"John Smith","created":"2025-02-10T05:36:18.360Z","email":"john.smith@gmail.com"},{"id":3,"name":"Gordan Freeman","created":"2025-02-10T05:36:18.360Z","email":"gordan.freeman@gmail.com"}]}
1
+ {"users":[{"id":697609403164,"name":"Bill","created":"2025-02-13T19:25:15.288Z","email":"sean.maxwell@gmail.com"},{"id":387041929717,"name":"John Smith","created":"2025-02-13T19:25:15.288Z","email":"john.smith@gmail.com"},{"id":465077000925,"name":"Gordan Freeman","created":"2025-02-13T19:25:15.288Z","email":"gordan.freeman@gmail.com"}]}
@@ -1,3 +1,4 @@
1
+
1
2
  /**
2
3
  * Get a random number between 1 and 1,000,000,000,000
3
4
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-generator-typescript",
3
- "version": "2.5.14",
3
+ "version": "2.5.15",
4
4
  "description": "Generate new Express applications similar to express-generate which but sets it up to use TypeScript instead",
5
5
  "scripts": {
6
6
  "express-generator-typescript": "node bin/cli.js",