Skip to content

Commit a88475b

Browse files
authored
Fix tests (#78)
1 parent 43384ca commit a88475b

23 files changed

+227
-56
lines changed

.eslintrc.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ module.exports = {
55
tsconfigRootDir: __dirname,
66
sourceType: 'module',
77
},
8-
plugins: ['@typescript-eslint/eslint-plugin'],
8+
plugins: ['@typescript-eslint/eslint-plugin', 'jest'],
99
extends: [
1010
'plugin:@typescript-eslint/recommended',
1111
'plugin:prettier/recommended',
12+
'plugin:jest/recommended'
1213
],
1314
root: true,
1415
env: {
@@ -21,10 +22,16 @@ module.exports = {
2122
'@typescript-eslint/explicit-function-return-type': 'off',
2223
'@typescript-eslint/explicit-module-boundary-types': 'off',
2324
'@typescript-eslint/no-explicit-any': 'off',
24-
"prettier/prettier": [
25-
"error",
25+
'jest/expect-expect': [
26+
'warn',
2627
{
27-
"endOfLine": "auto"
28+
'assertFunctionNames': ['expect', 'request.**.expect'],
29+
}
30+
],
31+
'prettier/prettier': [
32+
'error',
33+
{
34+
'endOfLine': 'auto'
2835
}
2936
]
3037
},

.github/workflows/ci.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
name: CI
22

33
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
tags:
9+
- "*"
410
pull_request:
511
branches:
6-
- master
12+
- main
713
- develop
814

915
jobs:
@@ -29,4 +35,7 @@ jobs:
2935
run: npm run lint:ci
3036
# Build App
3137
- name: Build App
32-
run: npm run build
38+
run: npm run build
39+
40+
- name: Test
41+
run: npm test

jest.config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Config } from 'jest';
2+
3+
const config: Config = {
4+
moduleFileExtensions: ['js', 'json', 'ts'],
5+
rootDir: 'src',
6+
testRegex: '.*\\.spec\\.ts$',
7+
transform: {
8+
'^.+\\.(t|j)s$': 'ts-jest',
9+
},
10+
collectCoverageFrom: ['**/*.(t|j)s'],
11+
coverageDirectory: '../coverage',
12+
moduleNameMapper: {
13+
'^src/(.*)$': '<rootDir>/$1',
14+
'^@/(.*)$': '<rootDir>/$1',
15+
'^test/(.*)$': '<rootDir>/../$1',
16+
},
17+
testEnvironment: 'node',
18+
};
19+
20+
export default config;

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"test:watch": "jest --watch",
1919
"test:cov": "jest --coverage",
2020
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
21-
"test:e2e": "jest --config ./test/jest-e2e.json",
21+
"test:e2e": "jest --config ./test/jest.e2e.config.ts",
2222
"migrations:run": "npx prisma migrate deploy",
2323
"migrations:dev": "npx prisma migrate dev",
2424
"docker:compose": "docker-compose -f docker-compose.dev.yml up"
@@ -53,6 +53,7 @@
5353
"@typescript-eslint/parser": "^6.0.0",
5454
"eslint": "^8.42.0",
5555
"eslint-config-prettier": "^9.0.0",
56+
"eslint-plugin-jest": "^28.5.0",
5657
"eslint-plugin-prettier": "^5.0.0",
5758
"jest": "^29.5.0",
5859
"prettier": "^3.0.0",
@@ -64,22 +65,5 @@
6465
"ts-node": "^10.9.1",
6566
"tsconfig-paths": "^4.2.0",
6667
"typescript": "^5.1.3"
67-
},
68-
"jest": {
69-
"moduleFileExtensions": [
70-
"js",
71-
"json",
72-
"ts"
73-
],
74-
"rootDir": "src",
75-
"testRegex": ".*\\.spec\\.ts$",
76-
"transform": {
77-
"^.+\\.(t|j)s$": "ts-jest"
78-
},
79-
"collectCoverageFrom": [
80-
"**/*.(t|j)s"
81-
],
82-
"coverageDirectory": "../coverage",
83-
"testEnvironment": "node"
8468
}
8569
}

src/sessions/sessions.controller.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { SessionsController } from './sessions.controller';
3+
import { SessionsService } from './sessions.service';
4+
import { PrismaService } from 'src/prisma/prisma.service';
5+
import { JwtService } from '@nestjs/jwt';
36

47
describe('SessionsController', () => {
58
let controller: SessionsController;
69

710
beforeEach(async () => {
811
const module: TestingModule = await Test.createTestingModule({
912
controllers: [SessionsController],
10-
}).compile();
13+
providers: [SessionsService, JwtService],
14+
})
15+
.useMocker((token) => {
16+
if (token === PrismaService) {
17+
return {};
18+
}
19+
})
20+
.compile();
1121

1222
controller = module.get<SessionsController>(SessionsController);
1323
});

src/sessions/sessions.service.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { SessionsService } from './sessions.service';
3+
import { JwtService } from '@nestjs/jwt';
4+
import { PrismaService } from 'src/prisma/prisma.service';
35

46
describe('SessionsService', () => {
57
let service: SessionsService;
68

79
beforeEach(async () => {
810
const module: TestingModule = await Test.createTestingModule({
9-
providers: [SessionsService],
10-
}).compile();
11+
providers: [SessionsService, JwtService],
12+
})
13+
.useMocker((token) => {
14+
if (token === PrismaService) {
15+
return {};
16+
}
17+
})
18+
.compile();
1119

1220
service = module.get<SessionsService>(SessionsService);
1321
});

src/shelter-managers/shelter-managers.controller.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { ShelterManagersController } from './shelter-managers.controller';
3+
import { PrismaService } from 'src/prisma/prisma.service';
4+
import { ShelterManagersService } from './shelter-managers.service';
35

46
describe('ShelterManagersController', () => {
57
let controller: ShelterManagersController;
68

79
beforeEach(async () => {
810
const module: TestingModule = await Test.createTestingModule({
911
controllers: [ShelterManagersController],
10-
}).compile();
12+
providers: [ShelterManagersService],
13+
})
14+
.useMocker((token) => {
15+
if (token === PrismaService) {
16+
return {};
17+
}
18+
})
19+
.compile();
1120

1221
controller = module.get<ShelterManagersController>(
1322
ShelterManagersController,

src/shelter-managers/shelter-managers.service.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { ShelterManagersService } from './shelter-managers.service';
3+
import { PrismaService } from 'src/prisma/prisma.service';
34

45
describe('ShelterManagersService', () => {
56
let service: ShelterManagersService;
67

78
beforeEach(async () => {
89
const module: TestingModule = await Test.createTestingModule({
910
providers: [ShelterManagersService],
10-
}).compile();
11+
})
12+
.useMocker((token) => {
13+
if (token === PrismaService) {
14+
return {};
15+
}
16+
})
17+
.compile();
1118

1219
service = module.get<ShelterManagersService>(ShelterManagersService);
1320
});

src/shelter-supply/shelter-supply.controller.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { ShelterSupplyController } from './shelter-supply.controller';
3+
import { PrismaService } from 'src/prisma/prisma.service';
4+
import { ShelterSupplyService } from './shelter-supply.service';
35

46
describe('ShelterSupplyController', () => {
57
let controller: ShelterSupplyController;
68

79
beforeEach(async () => {
810
const module: TestingModule = await Test.createTestingModule({
911
controllers: [ShelterSupplyController],
10-
}).compile();
12+
providers: [ShelterSupplyService],
13+
})
14+
.useMocker((token) => {
15+
if (token === PrismaService) {
16+
return {};
17+
}
18+
})
19+
.compile();
1120

1221
controller = module.get<ShelterSupplyController>(ShelterSupplyController);
1322
});

src/shelter-supply/shelter-supply.service.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { ShelterSupplyService } from './shelter-supply.service';
3+
import { PrismaService } from 'src/prisma/prisma.service';
34

45
describe('ShelterSupplyService', () => {
56
let service: ShelterSupplyService;
67

78
beforeEach(async () => {
89
const module: TestingModule = await Test.createTestingModule({
910
providers: [ShelterSupplyService],
10-
}).compile();
11+
})
12+
.useMocker((token) => {
13+
if (token === PrismaService) {
14+
return {};
15+
}
16+
})
17+
.compile();
1118

1219
service = module.get<ShelterSupplyService>(ShelterSupplyService);
1320
});

src/shelter/shelter.controller.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2+
import { PrismaService } from 'src/prisma/prisma.service';
23
import { ShelterController } from './shelter.controller';
4+
import { ShelterService } from './shelter.service';
35

46
describe('ShelterController', () => {
57
let controller: ShelterController;
68

79
beforeEach(async () => {
810
const module: TestingModule = await Test.createTestingModule({
911
controllers: [ShelterController],
10-
}).compile();
12+
providers: [ShelterService],
13+
})
14+
.useMocker((token) => {
15+
if (token === PrismaService) {
16+
return {
17+
supplyCategory: { findMany: jest.fn().mockResolvedValue(0) },
18+
};
19+
}
20+
})
21+
.compile();
1122

1223
controller = module.get<ShelterController>(ShelterController);
1324
});

src/shelter/shelter.service.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { ShelterService } from './shelter.service';
3+
import { PrismaService } from 'src/prisma/prisma.service';
34

45
describe('ShelterService', () => {
56
let service: ShelterService;
67

78
beforeEach(async () => {
89
const module: TestingModule = await Test.createTestingModule({
910
providers: [ShelterService],
10-
}).compile();
11+
})
12+
.useMocker((token) => {
13+
if (token === PrismaService) {
14+
return {
15+
supplyCategory: {
16+
findMany: jest.fn().mockResolvedValue([]),
17+
},
18+
};
19+
}
20+
})
21+
.compile();
1122

1223
service = module.get<ShelterService>(ShelterService);
1324
});

src/shelter/shelter.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Injectable, OnModuleInit } from '@nestjs/common';
22
import { Prisma } from '@prisma/client';
33
import { DefaultArgs } from '@prisma/client/runtime/library';
44
import * as qs from 'qs';
@@ -17,10 +17,12 @@ import {
1717
import { subDays } from 'date-fns';
1818

1919
@Injectable()
20-
export class ShelterService {
20+
export class ShelterService implements OnModuleInit {
2121
private voluntaryIds: string[] = [];
2222

23-
constructor(private readonly prismaService: PrismaService) {
23+
constructor(private readonly prismaService: PrismaService) {}
24+
25+
onModuleInit() {
2426
this.loadVoluntaryIds();
2527
}
2628

src/supply-categories/supply-categories.controller.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { SupplyCategoriesController } from './supply-categories.controller';
3+
import { PrismaService } from 'src/prisma/prisma.service';
4+
import { SupplyCategoriesService } from './supply-categories.service';
35

46
describe('SupplyCategoriesController', () => {
57
let controller: SupplyCategoriesController;
68

79
beforeEach(async () => {
810
const module: TestingModule = await Test.createTestingModule({
911
controllers: [SupplyCategoriesController],
10-
}).compile();
12+
providers: [SupplyCategoriesService],
13+
})
14+
.useMocker((token) => {
15+
if (token === PrismaService) {
16+
return {};
17+
}
18+
})
19+
.compile();
1120

1221
controller = module.get<SupplyCategoriesController>(
1322
SupplyCategoriesController,

0 commit comments

Comments
 (0)