Skip to content

Commit

Permalink
feat: move faker to devdependencies
Browse files Browse the repository at this point in the history
Move faker.js to devDependencies as it's not used in production app. Also add node args to pm2
config to support es6
  • Loading branch information
saisilinus committed Apr 27, 2022
1 parent 11dde60 commit e09711a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 67 deletions.
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
### Testing Tasks

- [ ] Improve coverage
- [ ] Add paginate tests
- [ ] Add toJSON tests
- [x] Add paginate tests
- [x] Add toJSON tests

### Other Tasks

- [ ] Have faker.js as a dev dependency
- [x] Have faker.js as a dev dependency
- [ ] Add Cookie Support
1 change: 1 addition & 0 deletions ecosystem.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"name": "app",
"script": "dist/index.js",
"node_args": "--experimental-modules --es-module-specifier-resolution=node",
"instances": 1,
"autorestart": true,
"watch": false,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"node": ">=14.0.0"
},
"scripts": {
"start": "pm2 start ecosystem.config.json --no-daemon",
"start": "tsc --build && eslint . --fix && pm2 start ecosystem.config.json --no-daemon",
"compile": "tsc --build && eslint . --fix",
"compile:watch": "tsc --build --watch",
"pre:dev": "cross-env NODE_ENV=development nodemon --experimental-modules --es-module-specifier-resolution=node dist/index.js",
Expand Down Expand Up @@ -58,6 +58,7 @@
"devDependencies": {
"@commitlint/cli": "^16.2.3",
"@commitlint/config-conventional": "^16.2.1",
"@faker-js/faker": "^6.2.0",
"@jest/globals": "^27.5.1",
"@types/bcryptjs": "^2.4.2",
"@types/compression": "^1.7.2",
Expand Down Expand Up @@ -100,7 +101,6 @@
"typescript": "^4.5.4"
},
"dependencies": {
"@faker-js/faker": "^6.0.0-alpha.7",
"bcryptjs": "^2.4.3",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand Down
23 changes: 21 additions & 2 deletions src/modules/auth/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable jest/no-commented-out-tests */
import { faker } from '@faker-js/faker';
import mongoose from 'mongoose';
import request from 'supertest';
import httpStatus from 'http-status';
import httpMocks from 'node-mocks-http';
Expand All @@ -10,17 +11,35 @@ import app from '../../app';
import setupTestDB from '../jest/setupTestDB';
import User from '../user/user.model';
import config from '../../config/config';
import { userOne, insertUsers } from '../user/user.fixture';
import { NewRegisteredUser } from '../user/user.interfaces';
import * as tokenService from '../token/token.service';
import tokenTypes from '../token/token.types';
import Token from '../token/token.model';
import { userOneAccessToken } from '../token/token.fixture';
import authMiddleware from './auth.middleware';
import ApiError from '../errors/ApiError';

setupTestDB();

const password = 'password1';
const salt = bcrypt.genSaltSync(8);
const hashedPassword = bcrypt.hashSync(password, salt);
const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');

const userOne = {
_id: new mongoose.Types.ObjectId(),
name: faker.name.findName(),
email: faker.internet.email().toLowerCase(),
password,
role: 'user',
isEmailVerified: false,
};

const userOneAccessToken = tokenService.generateToken(userOne._id, accessTokenExpires, tokenTypes.ACCESS);

const insertUsers = async (users: Record<string, any>[]) => {
await User.insertMany(users.map((user) => ({ ...user, password: hashedPassword })));
};

describe('Auth routes', () => {
describe('POST /v1/auth/register', () => {
let newUser: NewRegisteredUser;
Expand Down
3 changes: 1 addition & 2 deletions src/modules/token/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as tokenFixture from './token.fixture';
import * as tokenService from './token.service';
import Token from './token.model';
import * as tokenInterfaces from './token.interfaces';
import tokenTypes from './token.types';

export { tokenFixture, tokenService, Token, tokenInterfaces, tokenTypes };
export { tokenService, Token, tokenInterfaces, tokenTypes };
9 changes: 0 additions & 9 deletions src/modules/token/token.fixture.ts

This file was deleted.

19 changes: 17 additions & 2 deletions src/modules/token/token.model.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import moment from 'moment';
import mongoose from 'mongoose';
import { faker } from '@faker-js/faker';
import config from '../../config/config';
import { NewToken } from './token.interfaces';
import { userOne } from '../user/user.fixture';
import { userOneAccessToken } from './token.fixture';
import tokenTypes from './token.types';
import Token from './token.model';
import * as tokenService from './token.service';

const password = 'password1';
const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');

const userOne = {
_id: new mongoose.Types.ObjectId(),
name: faker.name.findName(),
email: faker.internet.email().toLowerCase(),
password,
role: 'user',
isEmailVerified: false,
};

const userOneAccessToken = tokenService.generateToken(userOne._id, accessTokenExpires, tokenTypes.ACCESS);

describe('Token Model', () => {
const refreshTokenExpires = moment().add(config.jwt.refreshExpirationDays, 'days');
Expand Down
3 changes: 1 addition & 2 deletions src/modules/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as userController from './user.controller';
import * as userFixture from './user.fixture';
import * as userInterfaces from './user.interfaces';
import User from './user.model';
import * as userService from './user.service';
import * as userValidation from './user.validation';

export { userController, userFixture, userInterfaces, User, userService, userValidation };
export { userController, userInterfaces, User, userService, userValidation };
39 changes: 0 additions & 39 deletions src/modules/user/user.fixture.ts

This file was deleted.

47 changes: 45 additions & 2 deletions src/modules/user/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
import mongoose from 'mongoose';
import bcrypt from 'bcryptjs';
import request from 'supertest';
import { faker } from '@faker-js/faker';
import httpStatus from 'http-status';
import moment from 'moment';
import config from '../../config/config';
import tokenTypes from '../token/token.types';
import * as tokenService from '../token/token.service';
import app from '../../app';
import setupTestDB from '../jest/setupTestDB';
import User from './user.model';
import { userOne, userTwo, admin, insertUsers } from './user.fixture';
import { userOneAccessToken, adminAccessToken } from '../token/token.fixture';
import { NewCreatedUser } from './user.interfaces';

setupTestDB();

const password = 'password1';
const salt = bcrypt.genSaltSync(8);
const hashedPassword = bcrypt.hashSync(password, salt);
const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');

const userOne = {
_id: new mongoose.Types.ObjectId(),
name: faker.name.findName(),
email: faker.internet.email().toLowerCase(),
password,
role: 'user',
isEmailVerified: false,
};

const userTwo = {
_id: new mongoose.Types.ObjectId(),
name: faker.name.findName(),
email: faker.internet.email().toLowerCase(),
password,
role: 'user',
isEmailVerified: false,
};

const admin = {
_id: new mongoose.Types.ObjectId(),
name: faker.name.findName(),
email: faker.internet.email().toLowerCase(),
password,
role: 'admin',
isEmailVerified: false,
};

const userOneAccessToken = tokenService.generateToken(userOne._id, accessTokenExpires, tokenTypes.ACCESS);
const adminAccessToken = tokenService.generateToken(admin._id, accessTokenExpires, tokenTypes.ACCESS);

const insertUsers = async (users: Record<string, any>[]) => {
await User.insertMany(users.map((user) => ({ ...user, password: hashedPassword })));
};

describe('User routes', () => {
describe('POST /v1/users', () => {
let newUser: NewCreatedUser;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,10 @@
minimatch "^3.0.4"
strip-json-comments "^3.1.1"

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

"@hapi/hoek@^9.0.0":
version "9.2.1"
Expand Down

0 comments on commit e09711a

Please sign in to comment.