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

Fix/jest memory leaks #20

Merged
merged 11 commits into from
Nov 28, 2021
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
101 changes: 92 additions & 9 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,84 @@
root: true

parser: '@typescript-eslint/parser'

parserOptions:
project: './tsconfig.json'
sourceType: 'module'
project: ./tsconfig.json
sourceType: module

plugins:
- '@typescript-eslint/eslint-plugin'
- prettier
- import

root: true
- jest

extends:
- eslint:recommended
- plugin:@typescript-eslint/recommended
- plugin:prettier/recommended
- plugin:import/typescript
- plugin:jest/recommended
- plugin:jest/style

env:
jest: true
node: true

rules:
'@typescript-eslint/no-explicit-any': off
'@typescript-eslint/interface-name-prefix': off
'@typescript-eslint/explicit-function-return-type': off
'@typescript-eslint/await-thenable': error
'@typescript-eslint/explicit-module-boundary-types': off
'@typescript-eslint/explicit-function-return-type': error
'@typescript-eslint/explicit-member-accessibility': off
'@typescript-eslint/indent': off
'@typescript-eslint/interface-name-prefix': off
'@typescript-eslint/member-delimiter-style': off
'@typescript-eslint/member-ordering': error
'@typescript-eslint/no-confusing-non-null-assertion': error
'@typescript-eslint/no-empty-interface': off
'@typescript-eslint/no-explicit-any': error
'@typescript-eslint/no-extra-non-null-assertion': error
'@typescript-eslint/no-floating-promises':
- error
- ignoreVoid: true
'@typescript-eslint/no-non-null-assertion': error
'@typescript-eslint/no-use-before-define': off
'@typescript-eslint/no-unused-vars':
- error
- argsIgnorePattern: '^_'
'@typescript-eslint/no-var-requires': off
'@typescript-eslint/switch-exhaustiveness-check': error
array-bracket-newline:
- error
- consistent
array-bracket-spacing:
- error
- never
array-callback-return: error
block-spacing:
- error
brace-style: error
camelcase:
- off
- properties: never
complexity: error
computed-property-spacing:
- error
- never
curly:
- error
- all
default-case: off
default-case-last: off
dot-location:
- error
- property
dot-notation: error
eqeqeq: error
eol-last: error
func-call-spacing:
- error
- never
func-style: error
import/no-unresolved: off
import/order:
- error
Expand All @@ -38,14 +91,44 @@ rules:
alphabetize:
order: 'asc'
pathGroups:
- pattern: '@nestjs/**'
group: external
- pattern: '{@tests/**,@type/**,@modules/**,@database/**,@decorators/**,@filters/**,@pipes/**}'
group: internal
position: before
pathGroupsExcludedImportTypes:
- builtin
jest/no-conditional-expect: off
key-spacing: error
keyword-spacing: error
lines-between-class-members: error
newline-per-chained-call: off
no-console:
- error
- allow: [warn, error]
- allow:
- warn
- error
no-dupe-keys: error
no-else-return: error
no-implicit-coercion: error
no-invalid-this: error
no-lonely-if: error
no-multi-spaces: error
no-return-await: error
no-trailing-spaces: error
no-useless-return: error
no-unneeded-ternary: error
prefer-arrow-callback:
- error
prefer-const: error
prettier/prettier:
- error
- endOfLine: auto
template-curly-spacing:
- error
- never
valid-typeof: error
jest/expect-expect:
- error
- assertFunctionNames:
- expect
- request.**.expect
17 changes: 14 additions & 3 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: checks
on: [push]
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
name: Main workflow
jobs:
install:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -27,7 +32,7 @@ jobs:
node-version: '16.x'

- name: Setting yarn version
run: yarn policies set-version 1.22.10
run: yarn policies set-version 1.22.17

- name: Install dependencies
run: yarn install --prefer-offline
Expand All @@ -43,3 +48,9 @@ jobs:

- name: Generating coverage badges
uses: jpb06/[email protected]

- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
43 changes: 27 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Sandbox

[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/jpb06/nestjs-prisma-postgres-sandbox)
![Code quality](https://img.shields.io/codefactor/grade/github/jpb06/nestjs-prisma-postgres-sandbox?logo=codefactor)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox&metric=security_rating)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox&metric=code_smells)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jpb06_nestjs-prisma-postgres-sandbox&metric=coverage)](https://sonarcloud.io/dashboard?id=jpb06_nestjs-prisma-postgres-sandbox)
![Coverage](./badges/coverage-global%20coverage.svg)
![Github workflow](https://img.shields.io/github/workflow/status/jpb06/nestjs-prisma-postgres-sandbox/checks?label=last%20workflow&logo=github-actions)
![Last deployment](https://img.shields.io/github/deployments/jpb06/nestjs-prisma-postgres-sandbox/nestjs-prisma-postgres?label=last%20deployment&logo=heroku)
Expand Down Expand Up @@ -30,71 +35,77 @@ So let's talk about the stack (pushing on an open door here, but hey):

## :zap: Guidelines

### :diamonds: Schema splitting
### 🔶 Schema splitting

We do not want a huge prisma schema. We want to isolate each model (table or set of tables) in its own file.

### :diamonds: Well documented routes
### 🔶 Well documented routes

Let's have a swagger documenting properly exposed routes, that is mainly for each route:

- a description.
- the list of possible responses.
- a definition of the inputs and outputs.

### :diamonds: Full testing coverage
### 🔶 Full testing coverage

We want to test everything to learn how to properly test, and to face every single difficulty that comes with testing. We will at very least do end to end using superagent, controllers testing, service testing.

### :diamonds: No testing against the database
### 🔶 No testing against the database

All tests should run without any interaction with a database.

## :zap: Usage

### :diamonds: run locally
### 🔶 run locally

:point_down: Since our prisma schemas are split within modules, we will have to merge them all in one file prisma can understand. Let's do just that:
#### 👇 Since our prisma schemas are split within modules, we will have to merge them all in one file prisma can understand. Let's do just that:

```bash
yarn prisma:merge
```

:point_down: Now, we need to tell prisma to generate in node_modules the code actually allowing us to interact with the database:
#### 👇 Now, we need to tell prisma to generate in node_modules the code actually allowing us to interact with the database:

```bash
yarn prisma:gen
```

:point_down: You will need docker and docker-compose to get the postgres database up and running. You can use this command to launch the database container:
#### 👇 You will need docker and docker-compose to get the postgres database up and running. You can use this command to launch the database container:

```bash
yarn dev:db
yarn docker
```

:point_down: Then, let's inject some data in our dev database using:
#### 👇 Then, let's inject some data in our dev database using:

```bash
yarn prisma:seed
```

:point_down: We can now launch the backend in dev:
#### 👇 We can now launch the backend in dev:

```bash
yarn dev
```

### :diamonds: test all the things
#### 😵 You can do the merge, gen & seed steps all at once using the following command:

```bash
yarn dev:db
```

### 🔶 test all the things

:point_down: We can run all the tests and get a coverage report using the following:
🔶 We can run all the tests and get a coverage report using the following:

```bash
yarn test:dev
```

## :zap: Subjects

### :diamonds: Authentication
### 🔶 Authentication

Let's use passport to setup jwt based authentication.

Expand All @@ -121,7 +132,7 @@ We have two users in database to play with the routes:
- :white_check_mark: services
- :white_check_mark: local passport strategy

### :diamonds: CRUD
### 🔶 CRUD

Let's create CRUD routes to manage a list of books.
We want to make sure to give a proper feedback when foreign keys violations do occur (when we try to delete an entry whose key is referenced in another table or when we try to update an entry with a foreign key that does not exist). Let's use filters for that!
Expand Down
10 changes: 8 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { pathsToModuleNameMapper } = require('ts-jest/utils');

const {
compilerOptions: { paths: tsconfigPaths },
} = require('./tsconfig');

module.exports = {
roots: ['<rootDir>/src/'],
logHeapUsage: true,
moduleFileExtensions: ['js', 'json', 'ts'],
moduleNameMapper: {
...pathsToModuleNameMapper(tsconfigPaths, { prefix: '<rootDir>/src' }),
},
testRegex: '\\.(test|spec)\\.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
'^.+\\.ts$': 'ts-jest',
},
collectCoverageFrom: ['**/*.ts'],
coveragePathIgnorePatterns: ['/tests-related/', '.dto\\.ts'],
coverageDirectory: './coverage',
testEnvironment: 'node',
coverageReporters: ['json-summary', 'text', 'lcov'],
globals: {
'ts-jest': {
isolatedModules: true,
},
},
};
Loading