Skip to content

Commit

Permalink
add example e2e tests in nestjs app
Browse files Browse the repository at this point in the history
  • Loading branch information
sitek94 committed Nov 25, 2023
1 parent 78b6e2d commit d260a6c
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 54 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
- [x] manually deploy to Fly.io
- [x] create CI deployment workflow to Fly.io
- [x] improve CI deployment workflow to trigger only for changed apps
- [ ] run typecheck, lint, test and build in parallel
- [ ] create shared ui lib
- [ ] setup Storybook in shared ui lib
- [x] set unified path aliases for all apps and shared libs (done for `apps/`, because `libs/` probably don't need them anyway)
- [ ] add unused imports plugin to eslint

## References

Expand Down
8 changes: 8 additions & 0 deletions apps/nestjs/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ module.exports = {
},
},
},
overrides: [
{
files: ['jest.config.js', 'jest-e2e.config.js'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
},
},
],
}
18 changes: 18 additions & 0 deletions apps/nestjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
preset: 'ts-jest',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
moduleFileExtensions: ['js', 'json', 'ts'],
moduleNameMapper: {
'^~/(.*)$': '../src/$1',
},
testEnvironment: 'node',
rootDir: './src',
testRegex: '.*\\.spec\\.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
collectCoverageFrom: ['**/*.(t|j)s'],
coverageDirectory: '../coverage',
}
20 changes: 2 additions & 18 deletions apps/nestjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.config.js"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand All @@ -33,28 +33,12 @@
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"jest": "^29.5.0",
"jest-mock": "^29.7.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
2 changes: 2 additions & 0 deletions apps/nestjs/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { appConfig } from '~/config/app.config'

import { AppController } from './app.controller'
import { AppService } from './app.service'
import { DogsModule } from './dogs/dogs.module'

@Module({
imports: [
CatsModule,
ConfigModule.forRoot({ isGlobal: true, load: [appConfig] }),
DogsModule,
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
Expand Down
19 changes: 19 additions & 0 deletions apps/nestjs/src/dogs/dogs.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Test, TestingModule } from '@nestjs/testing'

import { DogsController } from './dogs.controller'

describe('DogsController', () => {
let controller: DogsController

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DogsController],
}).compile()

controller = module.get<DogsController>(DogsController)
})

it('should be defined', () => {
expect(controller).toBeDefined()
})
})
14 changes: 14 additions & 0 deletions apps/nestjs/src/dogs/dogs.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Dog } from '@company/types'
import { Controller, Get } from '@nestjs/common'

import { DogsService } from './dogs.service'

@Controller('dogs')
export class DogsController {
constructor(private readonly dogsService: DogsService) {}

@Get()
async findAll(): Promise<Dog[]> {
return this.dogsService.findAll()
}
}
10 changes: 10 additions & 0 deletions apps/nestjs/src/dogs/dogs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common'

import { DogsController } from './dogs.controller'
import { DogsService } from './dogs.service'

@Module({
controllers: [DogsController],
providers: [DogsService],
})
export class DogsModule {}
19 changes: 19 additions & 0 deletions apps/nestjs/src/dogs/dogs.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Test, TestingModule } from '@nestjs/testing'

import { DogsService } from './dogs.service'

describe('DogsService', () => {
let service: DogsService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [DogsService],
}).compile()

service = module.get<DogsService>(DogsService)
})

it('should be defined', () => {
expect(service).toBeDefined()
})
})
22 changes: 22 additions & 0 deletions apps/nestjs/src/dogs/dogs.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Dog } from '@company/types'
import { Injectable } from '@nestjs/common'

@Injectable()
export class DogsService {
async findAll(): Promise<Dog[]> {
return [
{
_id: '1',
name: 'Barnaba',
},
{
_id: '2',
name: 'Homelander',
},
{
_id: '3',
name: 'Butcher',
},
]
}
}
25 changes: 0 additions & 25 deletions apps/nestjs/test/app.e2e-spec.ts

This file was deleted.

34 changes: 34 additions & 0 deletions apps/nestjs/test/dogs.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { INestApplication } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
import request from 'supertest'

import { DogsModule } from '~/dogs/dogs.module'
import { DogsService } from '~/dogs/dogs.service'

describe('UsersController (e2e)', () => {
let app: INestApplication

const dogsServiceMock = {
findAll: jest.fn().mockResolvedValue([]),
}

beforeEach(async () => {
const moduleRef: TestingModule = await Test.createTestingModule({
imports: [DogsModule],
})
.overrideProvider(DogsService)
.useValue(dogsServiceMock)
.compile()

app = moduleRef.createNestApplication()
await app.init()
})

it('[GET] /users', async () => {
await request(app.getHttpServer()).get('/dogs').expect(200).expect([])
})

afterAll(async () => {
await app.close()
})
})
32 changes: 32 additions & 0 deletions apps/nestjs/test/jest-e2e.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// const { pathsToModuleNameMapper } = require('ts-jest')

// // In the following statement, replace `./tsconfig` with the path to your `tsconfig` file
// // which contains the path mapping (ie the `compilerOptions.paths` option):
// const { compilerOptions } = require('./tsconfig.json')

module.exports = {
preset: 'ts-jest',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
moduleFileExtensions: ['js', 'json', 'ts'],
moduleNameMapper: {
'^~/(.*)$': '../src/$1',
},
testEnvironment: 'node',

rootDir: '.',
testRegex: '.e2e-spec.ts$',
collectCoverageFrom: ['**/*.(t|j)s'],
coverageDirectory: '../coverage',
}

// {
// // "moduleFileExtensions": ["js", "json", "ts"],
// "rootDir": ".",
// "testEnvironment": "node",

// "transform": {
// "^.+\\.(t|j)s$": "ts-jest"
// }
// }
9 changes: 0 additions & 9 deletions apps/nestjs/test/jest-e2e.json

This file was deleted.

6 changes: 4 additions & 2 deletions apps/nestjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../../tsconfig.json",
"include": ["**/*.ts"],
"include": ["**/*.ts", "jest.config.js"],
"compilerOptions": {
"module": "commonjs",
"declaration": true,
Expand All @@ -15,9 +15,11 @@
"noImplicitAny": false,
"strictBindCallApply": false,
"noFallthroughCasesInSwitch": false,
"resolveJsonModule": true,
"baseUrl": "./",
"paths": {
"~/*": ["./src/*"]
}
},
"types": ["@types/jest"]
}
}
5 changes: 5 additions & 0 deletions libs/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export type Cat = {
age: number
breed: string
}

export type Dog = {
_id: string
name: string
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit d260a6c

Please sign in to comment.