Skip to content

Commit

Permalink
Merge pull request #169 from victorsoares96/fix/fix-tests
Browse files Browse the repository at this point in the history
🩹 fix: fix tests
  • Loading branch information
victorsoares96 authored Dec 29, 2023
2 parents 40beb60 + 63bb76c commit c616196
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 137 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
{
"devDependencies": [
"**/*.spec.ts",
"**/scripts/*.ts"
"**/scripts/*.ts",
"**/*.mock.ts"
]
}
],
Expand Down
51 changes: 44 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
}
},
"engines": {
"node": ">=18.x.x <=19.x.x",
"node": ">=20.x.x <=21.x.x",
"yarn": "not allowed in this project",
"npm": ">=8.0.0"
"npm": ">=10.0.0"
},
"scripts": {
"prepare": "husky install",
Expand Down Expand Up @@ -53,7 +53,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-s3": "^3.328.0",
"@aws-sdk/client-s3": "3.328.0",
"@vendia/serverless-express": "4.10.1",
"axios": "1.4.0",
"bcryptjs": "2.4.3",
Expand All @@ -63,15 +63,16 @@
"compression": "1.7.4",
"config": "3.3.9",
"cors": "2.8.5",
"dayjs": "^1.11.7",
"dayjs": "1.11.7",
"dotenv": "16.0.3",
"ejs": "3.1.9",
"escape-html": "1.0.3",
"express": "4.18.2",
"express-async-errors": "3.1.1",
"express-fileupload": "^1.4.0",
"express-fileupload": "1.4.0",
"jsonwebtoken": "9.0.0",
"mysql2": "2.3.3",
"node-cache": "5.1.2",
"reflect-metadata": "0.1.13",
"source-map-support": "0.5.21",
"tslib": "2.6.2",
Expand All @@ -80,6 +81,7 @@
"winston-loki": "6.0.6"
},
"devDependencies": {
"@faker-js/faker": "8.3.1",
"@types/aws-lambda": "8.10.115",
"@types/bcryptjs": "2.4.6",
"@types/compression": "1.7.2",
Expand All @@ -88,7 +90,7 @@
"@types/ejs": "3.1.2",
"@types/escape-html": "1.0.2",
"@types/express": "4.17.17",
"@types/express-fileupload": "^1.4.1",
"@types/express-fileupload": "1.4.1",
"@types/jest": "29.5.1",
"@types/jsonwebtoken": "9.0.2",
"@types/node": "18.16.4",
Expand All @@ -114,7 +116,7 @@
"jest": "29.5.0",
"lint-staged": "13.2.2",
"prettier": "2.8.8",
"sqlite3": "^5.1.4",
"sqlite3": "5.1.4",
"supertest": "6.3.3",
"ts-jest": "29.1.0",
"ts-node": "10.9.1",
Expand Down
39 changes: 39 additions & 0 deletions src/database/database.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createFakeUser } from '@/mocks/user.mock';
import Database from './database';
import { User } from '@/entities/user.entity';
import { Session } from '@/entities/session.entity';

export const DEFAULT_USERS = [
createFakeUser({
id: '1',
name: 'John Doe',
email: '[email protected]',
}),
];

class MockedDatabase extends Database {
public async clear(): Promise<void> {
await this.dataSource.manager.delete(User, {});
await this.dataSource.manager.delete(Session, {});
}

public async seed(): Promise<void> {
await this.dataSource.manager.insert(User, DEFAULT_USERS);
}

public async findUser(id: string) {
return this.dataSource.manager.findBy(User, { id });
}

public async createUsers(count?: number, elements?: User[]): Promise<void> {
const documents: User[] = [
...Array(count || 0)
.fill(0)
.map(() => createFakeUser()),
];

await this.dataSource.manager.insert(User, elements || documents);
}
}

export default MockedDatabase;
35 changes: 35 additions & 0 deletions src/database/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { DataSource } from 'typeorm';
import productionDataSource from './sources/production.source';
import stagingDataSource from './sources/staging.source';
import developmentDataSource from './sources/development.source';

class Database {
public dataSource: DataSource;

constructor() {
this.dataSource = this.getDataSource();
}

private getDataSource(): DataSource {
const environment = process.env.NODE_ENV;

if (environment === 'production') {
return productionDataSource;
}

if (environment === 'staging') {
return stagingDataSource;
}
return developmentDataSource;
}

public async connect(): Promise<DataSource> {
return this.dataSource.initialize();
}

public async disconnect(): Promise<void> {
await this.dataSource.destroy();
}
}

export default Database;
3 changes: 2 additions & 1 deletion src/environments/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"AWS_SECRET_ACCESS_KEY": "",
"AWS_REGION": "us-east-1",
"AWS_S3_BUCKET": "serverless_express_template"
}
},
"port": 3333
}
3 changes: 2 additions & 1 deletion src/environments/production.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"AWS_SECRET_ACCESS_KEY": "",
"AWS_REGION": "us-east-1",
"AWS_S3_BUCKET": "serverless_express_template"
}
},
"port": 3333
}
5 changes: 3 additions & 2 deletions src/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'source-map-support/register';
import serverlessExpress from '@vendia/serverless-express';
import app from './app';
import Server from './server';

export const handler = serverlessExpress({ app: app.express });
const server = new Server();
export const handler = serverlessExpress({ app: server.express });
18 changes: 18 additions & 0 deletions src/mocks/user.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { faker } from '@faker-js/faker';
import { User } from '@/entities/user.entity';

export function createFakeUser(data?: Partial<User>): User {
const createdAt = faker.date.past();
return {
id: faker.string.numeric(5),
name: faker.internet.displayName(),
avatar: faker.internet.avatar(),
createdAt,
updatedAt: createdAt,
deletionDate: null as never,
username: faker.internet.userName(),
email: faker.internet.email(),
password: faker.internet.password(),
...data,
};
}
35 changes: 25 additions & 10 deletions src/multithread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import log from './utils/log.util';
const runPrimaryProcess = () => {
const processesCount = os.cpus().length;

log.info(`Primary ${process.pid} is running`);
log.info(`Forking Server with ${processesCount} processes\n`);
log.info(
`🚀 Primary process running with id: ${process.pid}, forking server with ${processesCount} processes!`,
);

for (let index = 0; index < processesCount; index++) {
cluster.fork();

cluster.on('exit', (worker, code, signal) => {
if (code !== 0 && !worker.exitedAfterDisconnect) {
log.info(
`Worker ${worker.process.pid} died with signal ${signal}, scheduling another one`,
`💀 worker ${worker.process.pid} died with signal ${signal}, scheduling another one...`,
);
cluster.fork();
}
Expand All @@ -23,14 +24,28 @@ const runPrimaryProcess = () => {
};

const runWorkerProcess = async () => {
const { default: server } = await import('./app');
await server.startServer();
const { default: Database } = await import('./database/database');
const { default: Server } = await import('./server');

const database = new Database();
const server = new Server();

const connection = await database.connect();
await server.startServer(connection);

log.info(`ℹ️ Worker process running with id: ${process.pid}!`);
};

(() => {
if (cluster.isPrimary) {
runPrimaryProcess();
} else {
runWorkerProcess();
(async () => {
try {
if (cluster.isPrimary) {
runPrimaryProcess();
} else {
await runWorkerProcess();
}
} catch (error) {
if (error instanceof Error) {
log.error(`${error.message} ${error.stack}`);
} else log.error(error);
}
})();
Loading

0 comments on commit c616196

Please sign in to comment.