Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(domain): write tests for services in the domain's package #9

Merged
merged 2 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', 'plugin:typescript-sort-keys/recommended'],
plugins: ['sort-destructure-keys', 'sort-keys-fix', 'typescript-sort-keys', 'import'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
'plugin:typescript-sort-keys/recommended'
],
plugins: [
'sort-destructure-keys',
'sort-keys-fix',
'typescript-sort-keys',
'import'
],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
Expand Down Expand Up @@ -68,7 +77,6 @@ module.exports = {
'no-console': 'off',
'no-else-return': 'error',
'no-eval': 'error',
'no-undef': 'error',
'no-mixed-operators': 'off',
'no-nested-ternary': 'error',
'no-param-reassign': 'error',
Expand Down
11 changes: 11 additions & 0 deletions packages/database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ yarn db:migrate:dev
yarn db:seed
```

### Generate a database migration
To generate a database migration, we run the command below:
```shell
yarn db:migrate:dev --name <migration-name>
```
Once done, make a copy of the file `prisma/schema.prisma` to `prisma/schema.test.prisma`.

Open the copied file and remove the line `shadowDatabaseUrl = env("SHADOW_DATABASE_URL")`.

This action is required to make the test executions easier in the package **domain (packages/domain)**.

### Others Prisma commands
- Reset the database without seeding: `db:reset:dev`
- Reset the database with seeding: `db:reset:dev:seed`
Expand Down
1 change: 1 addition & 0 deletions packages/database/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type EnvironmentVariables = {
DATABASE_URL: string;
NODE_ENV: string;
};

declare global {
Expand Down
1 change: 1 addition & 0 deletions packages/database/src/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if (process.env.NODE_ENV === 'production') {
if (!global.prisma) {
global.prisma = new PrismaClient();
}
// eslint-disable-next-line prefer-destructuring
prisma = global.prisma;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/repositories/interfaces/role.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BaseRepository from './_base';
import Role, { RoleName } from '../../entities/role';
import BaseRepository from './_base';

type RoleRepositoryInterface = {
findByName: (name: RoleName) => Promise<Role | null>;
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/repositories/interfaces/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BaseRepository from './_base';
import User from '../../entities/user';
import BaseRepository from './_base';

type UserRepositoryInterface = {
findByEmail: (email: string) => Promise<User | null>;
Expand Down
4 changes: 2 additions & 2 deletions packages/domain/.env.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DATABASE_URL="mysql:https://root:@127.0.0.1:3311/sharingan"
SHADOW_DATABASE_URL="mysql:https://root:@127.0.0.1:3312/sharingan"
DATABASE_URL="mysql:https://root:[email protected]:3313/sharingan"
MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=sharingan
MYSQL_PORT=3313
NODE_ENV=test
ADMIN_PASSWORD=admin_password
38 changes: 34 additions & 4 deletions packages/domain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This package contains all the business logic of the project
Make sure you have this tools installed before running the project
* Node.js 14+
* NPM or Yarn
* Docker

## Set up the project
Delete the existing folders output from build commands
Expand All @@ -20,6 +21,10 @@ Install node modules
````shell
yarn install
````
Create the .env file from the template. This file is useful for test executions, so you don't have to edit any properties in the file.
```shell
cp .env.template .env
```

Build the package to generate types declaration required to provide autocompletion while using the functions in the core or web applications
```bash
Expand All @@ -28,13 +33,38 @@ yarn build
A `dist` folder will be generated.

## Running tests
Run the command below to run all the tests
If you are running tests that require a database, you must start one with Docker.

Start the Docker database
```shell
yarn db:test
```
Stop the Docker database
```shell
yarn db:test:stop
```
Run the migration to load the database schema
```shell
yarn test
yarn db:test:migration
```
To run a specific test file, append the filename after the command

There are three command to run tests where each command has a specific purpose:
- `yarn test:unit`: use this command when you want to test unit functions


- `yarn test`: use this command to run functions that require database or not. Everytime you run a test, the migration will be executed against the database
which add 5-10 seconds on test execution time. This command is appropriate inside a CI Pipeline


- `yarn test:db:local`: This command run functions that require database or not but, it doesn't execute the migration, so it is faster than the former.

To run a specific test file, append the filename after the command:
```shell
yarn test index.test.ts
yarn test user.service.test.ts
# or
yarn test:unit user.service.test.ts
# or
yarn test:db:local user.service.test.ts
```

## Lint the project
Expand Down
1 change: 1 addition & 0 deletions packages/domain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint": "eslint src index.ts",
"db:test": "docker run -d --rm -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=sharingan --name sharingandb -p 3313:3306 mysql:8.0",
"db:test:stop": "docker container kill sharingandb",
"db:test:migration": "npx prisma migrate dev --schema=../database/prisma/schema.test.prisma",
"test": "TEST_WITH_DB=true IS_LOCAL=false jest",
"test:unit": "jest",
"test:db:local": "TEST_WITH_DB=true IS_LOCAL=true jest"
Expand Down
8 changes: 6 additions & 2 deletions packages/logger/src/base-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ class BaseLogger {
}

private logMessage(message: any): string {
// @ts-ignore
return isObject(message) ? (message.stack ? message.stack : JSON.stringify(message, null, 2)) : message.toString();
if (isObject(message)) {
// @ts-expect-error Property 'stack' does not exist on type 'object'.
return 'stack' in message ? message.stack : JSON.stringify(message, undefined, '2');
}

return message.toString();
}

private customFormat() {
Expand Down
5 changes: 5 additions & 0 deletions packages/logger/tests/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('Test logger', () => {
test('basic test', () => {
expect(true).toEqual(true);
});
});