Skip to content

Commit e09711a

Browse files
committed
feat: move faker to devdependencies
Move faker.js to devDependencies as it's not used in production app. Also add node args to pm2 config to support es6
1 parent 11dde60 commit e09711a

File tree

11 files changed

+95
-67
lines changed

11 files changed

+95
-67
lines changed

TODO.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
### Testing Tasks
22

33
- [ ] Improve coverage
4-
- [ ] Add paginate tests
5-
- [ ] Add toJSON tests
4+
- [x] Add paginate tests
5+
- [x] Add toJSON tests
66

77
### Other Tasks
88

9-
- [ ] Have faker.js as a dev dependency
9+
- [x] Have faker.js as a dev dependency
1010
- [ ] Add Cookie Support

ecosystem.config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{
44
"name": "app",
55
"script": "dist/index.js",
6+
"node_args": "--experimental-modules --es-module-specifier-resolution=node",
67
"instances": 1,
78
"autorestart": true,
89
"watch": false,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"node": ">=14.0.0"
1212
},
1313
"scripts": {
14-
"start": "pm2 start ecosystem.config.json --no-daemon",
14+
"start": "tsc --build && eslint . --fix && pm2 start ecosystem.config.json --no-daemon",
1515
"compile": "tsc --build && eslint . --fix",
1616
"compile:watch": "tsc --build --watch",
1717
"pre:dev": "cross-env NODE_ENV=development nodemon --experimental-modules --es-module-specifier-resolution=node dist/index.js",
@@ -58,6 +58,7 @@
5858
"devDependencies": {
5959
"@commitlint/cli": "^16.2.3",
6060
"@commitlint/config-conventional": "^16.2.1",
61+
"@faker-js/faker": "^6.2.0",
6162
"@jest/globals": "^27.5.1",
6263
"@types/bcryptjs": "^2.4.2",
6364
"@types/compression": "^1.7.2",
@@ -100,7 +101,6 @@
100101
"typescript": "^4.5.4"
101102
},
102103
"dependencies": {
103-
"@faker-js/faker": "^6.0.0-alpha.7",
104104
"bcryptjs": "^2.4.3",
105105
"compression": "^1.7.4",
106106
"cors": "^2.8.5",

src/modules/auth/auth.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable jest/no-commented-out-tests */
22
import { faker } from '@faker-js/faker';
3+
import mongoose from 'mongoose';
34
import request from 'supertest';
45
import httpStatus from 'http-status';
56
import httpMocks from 'node-mocks-http';
@@ -10,17 +11,35 @@ import app from '../../app';
1011
import setupTestDB from '../jest/setupTestDB';
1112
import User from '../user/user.model';
1213
import config from '../../config/config';
13-
import { userOne, insertUsers } from '../user/user.fixture';
1414
import { NewRegisteredUser } from '../user/user.interfaces';
1515
import * as tokenService from '../token/token.service';
1616
import tokenTypes from '../token/token.types';
1717
import Token from '../token/token.model';
18-
import { userOneAccessToken } from '../token/token.fixture';
1918
import authMiddleware from './auth.middleware';
2019
import ApiError from '../errors/ApiError';
2120

2221
setupTestDB();
2322

23+
const password = 'password1';
24+
const salt = bcrypt.genSaltSync(8);
25+
const hashedPassword = bcrypt.hashSync(password, salt);
26+
const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');
27+
28+
const userOne = {
29+
_id: new mongoose.Types.ObjectId(),
30+
name: faker.name.findName(),
31+
email: faker.internet.email().toLowerCase(),
32+
password,
33+
role: 'user',
34+
isEmailVerified: false,
35+
};
36+
37+
const userOneAccessToken = tokenService.generateToken(userOne._id, accessTokenExpires, tokenTypes.ACCESS);
38+
39+
const insertUsers = async (users: Record<string, any>[]) => {
40+
await User.insertMany(users.map((user) => ({ ...user, password: hashedPassword })));
41+
};
42+
2443
describe('Auth routes', () => {
2544
describe('POST /v1/auth/register', () => {
2645
let newUser: NewRegisteredUser;

src/modules/token/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as tokenFixture from './token.fixture';
21
import * as tokenService from './token.service';
32
import Token from './token.model';
43
import * as tokenInterfaces from './token.interfaces';
54
import tokenTypes from './token.types';
65

7-
export { tokenFixture, tokenService, Token, tokenInterfaces, tokenTypes };
6+
export { tokenService, Token, tokenInterfaces, tokenTypes };

src/modules/token/token.fixture.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/modules/token/token.model.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import moment from 'moment';
2+
import mongoose from 'mongoose';
3+
import { faker } from '@faker-js/faker';
24
import config from '../../config/config';
35
import { NewToken } from './token.interfaces';
4-
import { userOne } from '../user/user.fixture';
5-
import { userOneAccessToken } from './token.fixture';
66
import tokenTypes from './token.types';
77
import Token from './token.model';
8+
import * as tokenService from './token.service';
9+
10+
const password = 'password1';
11+
const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');
12+
13+
const userOne = {
14+
_id: new mongoose.Types.ObjectId(),
15+
name: faker.name.findName(),
16+
email: faker.internet.email().toLowerCase(),
17+
password,
18+
role: 'user',
19+
isEmailVerified: false,
20+
};
21+
22+
const userOneAccessToken = tokenService.generateToken(userOne._id, accessTokenExpires, tokenTypes.ACCESS);
823

924
describe('Token Model', () => {
1025
const refreshTokenExpires = moment().add(config.jwt.refreshExpirationDays, 'days');

src/modules/user/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as userController from './user.controller';
2-
import * as userFixture from './user.fixture';
32
import * as userInterfaces from './user.interfaces';
43
import User from './user.model';
54
import * as userService from './user.service';
65
import * as userValidation from './user.validation';
76

8-
export { userController, userFixture, userInterfaces, User, userService, userValidation };
7+
export { userController, userInterfaces, User, userService, userValidation };

src/modules/user/user.fixture.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/modules/user/user.test.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
1+
import mongoose from 'mongoose';
2+
import bcrypt from 'bcryptjs';
13
import request from 'supertest';
24
import { faker } from '@faker-js/faker';
35
import httpStatus from 'http-status';
6+
import moment from 'moment';
7+
import config from '../../config/config';
8+
import tokenTypes from '../token/token.types';
9+
import * as tokenService from '../token/token.service';
410
import app from '../../app';
511
import setupTestDB from '../jest/setupTestDB';
612
import User from './user.model';
7-
import { userOne, userTwo, admin, insertUsers } from './user.fixture';
8-
import { userOneAccessToken, adminAccessToken } from '../token/token.fixture';
913
import { NewCreatedUser } from './user.interfaces';
1014

1115
setupTestDB();
1216

17+
const password = 'password1';
18+
const salt = bcrypt.genSaltSync(8);
19+
const hashedPassword = bcrypt.hashSync(password, salt);
20+
const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');
21+
22+
const userOne = {
23+
_id: new mongoose.Types.ObjectId(),
24+
name: faker.name.findName(),
25+
email: faker.internet.email().toLowerCase(),
26+
password,
27+
role: 'user',
28+
isEmailVerified: false,
29+
};
30+
31+
const userTwo = {
32+
_id: new mongoose.Types.ObjectId(),
33+
name: faker.name.findName(),
34+
email: faker.internet.email().toLowerCase(),
35+
password,
36+
role: 'user',
37+
isEmailVerified: false,
38+
};
39+
40+
const admin = {
41+
_id: new mongoose.Types.ObjectId(),
42+
name: faker.name.findName(),
43+
email: faker.internet.email().toLowerCase(),
44+
password,
45+
role: 'admin',
46+
isEmailVerified: false,
47+
};
48+
49+
const userOneAccessToken = tokenService.generateToken(userOne._id, accessTokenExpires, tokenTypes.ACCESS);
50+
const adminAccessToken = tokenService.generateToken(admin._id, accessTokenExpires, tokenTypes.ACCESS);
51+
52+
const insertUsers = async (users: Record<string, any>[]) => {
53+
await User.insertMany(users.map((user) => ({ ...user, password: hashedPassword })));
54+
};
55+
1356
describe('User routes', () => {
1457
describe('POST /v1/users', () => {
1558
let newUser: NewCreatedUser;

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,10 @@
543543
minimatch "^3.0.4"
544544
strip-json-comments "^3.1.1"
545545

546-
"@faker-js/faker@^6.0.0-alpha.7":
547-
version "6.1.2"
548-
resolved "https://registry.npmjs.org/@faker-js/faker/-/faker-6.1.2.tgz"
549-
integrity sha512-QSvmexHCxeRUk1/yKmoEDaWB5Hohjvtim5g2JJwy8S/l0L4b3y/GxSpE6vN4SBoVGGahEQW21uqyRr7EofI35A==
546+
"@faker-js/faker@^6.2.0":
547+
version "6.2.0"
548+
resolved "https://registry.yarnpkg.com/@faker-js/faker/-/faker-6.2.0.tgz#6d836ee8a580589b60c9088a3bdb0b669a468825"
549+
integrity sha512-3kIcQ+aTr3I+LqDbJwbINFk5oA+a63LH57GPJt9PM8AWqN7nCwnubuSiWosHYQlyf2NicrvpzXQxllyLeEdpyQ==
550550

551551
"@hapi/hoek@^9.0.0":
552552
version "9.2.1"

0 commit comments

Comments
 (0)