chore: update deps and fixes tests

This commit is contained in:
Hantsy Bai 2023-04-15 21:35:02 +08:00 committed by hantsy
parent a15b351cae
commit 5ff00a1e36
10 changed files with 2430 additions and 12107 deletions

View File

@ -9,7 +9,6 @@ module.exports = {
'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended', 'plugin:@typescript-eslint/recommended',
'prettier', 'prettier',
'prettier/@typescript-eslint',
], ],
root: true, root: true,
env: { env: {

View File

@ -22,7 +22,7 @@ jobs:
- name: Setup NodeJS - name: Setup NodeJS
uses: actions/setup-node@v3 uses: actions/setup-node@v3
with: with:
node-version: "16" node-version: "18"
- name: Cache Node.js modules - name: Cache Node.js modules
uses: actions/cache@v3 uses: actions/cache@v3

View File

@ -17,7 +17,7 @@ jobs:
- name: Setup NodeJS - name: Setup NodeJS
uses: actions/setup-node@v3 uses: actions/setup-node@v3
with: with:
node-version: "16" node-version: "18"
- name: Cache Node.js modules - name: Cache Node.js modules
uses: actions/cache@v3 uses: actions/cache@v3

View File

@ -22,7 +22,7 @@ jobs:
- name: Setup NodeJS - name: Setup NodeJS
uses: actions/setup-node@v3 uses: actions/setup-node@v3
with: with:
node-version: "16" node-version: "18"
- name: Cache Node.js modules - name: Cache Node.js modules
uses: actions/cache@v3 uses: actions/cache@v3

View File

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
. "$(dirname "$0")/_/husky.sh" . "$(dirname "$0")/_/husky.sh"
#npm test npm run lint

View File

@ -1,5 +1,5 @@
# Set nginx base image # Set nginx base image
FROM node:16 FROM node:18
LABEL maintainer="Hantsy Bai" LABEL maintainer="Hantsy Bai"
WORKDIR /app WORKDIR /app
COPY ./dist ./dist COPY ./dist ./dist

14320
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -22,54 +22,54 @@
"prepare": "husky install" "prepare": "husky install"
}, },
"dependencies": { "dependencies": {
"@nestjs/common": "^8.0.0", "@nestjs/common": "^9.4.0",
"@nestjs/config": "^2.0.0", "@nestjs/config": "^2.3.1",
"@nestjs/core": "^9.0.5", "@nestjs/core": "^9.4.0",
"@nestjs/jwt": "^10.0.1", "@nestjs/jwt": "^10.0.3",
"@nestjs/passport": "^9.0.0", "@nestjs/passport": "^9.0.3",
"@nestjs/platform-express": "^8.0.0", "@nestjs/platform-express": "^9.4.0",
"@sendgrid/mail": "^7.4.5", "@sendgrid/mail": "^7.7.0",
"bcrypt": "^5.0.1", "bcrypt": "^5.1.0",
"class-transformer": "^0.5.0", "class-transformer": "^0.5.1",
"class-validator": "^0.14.0", "class-validator": "^0.14.0",
"mongoose": "^7.0.0", "mongoose": "^7.0.3",
"passport": "^0.6.0", "passport": "^0.6.0",
"passport-jwt": "^4.0.0", "passport-jwt": "^4.0.1",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^5.0.0", "rimraf": "^5.0.0",
"rxjs": "^7.1.0" "rxjs": "^7.8.0"
}, },
"devDependencies": { "devDependencies": {
"@commitlint/cli": "^17.0.0", "@commitlint/cli": "^17.6.1",
"@commitlint/config-conventional": "^17.0.0", "@commitlint/config-conventional": "^17.6.1",
"@golevelup/ts-jest": "^0.3.1", "@golevelup/ts-jest": "^0.3.5",
"@nestjs/cli": "^9.0.0", "@nestjs/cli": "^9.4.0",
"@nestjs/schematics": "^9.0.1", "@nestjs/schematics": "^9.1.0",
"@nestjs/testing": "^8.0.0", "@nestjs/testing": "^9.4.0",
"@types/bcrypt": "^5.0.0", "@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.13", "@types/express": "^4.17.17",
"@types/jest": "^27.0.1", "@types/jest": "^29.5.0",
"@types/node": "^18.0.0", "@types/node": "^18.15.11",
"@types/passport-jwt": "^3.0.6", "@types/passport-jwt": "^3.0.8",
"@types/passport-local": "^1.0.34", "@types/passport-local": "^1.0.35",
"@types/supertest": "^2.0.11", "@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "5.57.1", "@typescript-eslint/eslint-plugin": "5.58.0",
"@typescript-eslint/parser": "5.57.1", "@typescript-eslint/parser": "5.58.0",
"eslint": "8.38.0", "eslint": "8.38.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.23.4", "eslint-plugin-import": "^2.27.5",
"husky": "^8.0.1", "husky": "^8.0.3",
"jest": "^27.0.6", "jest": "^29.5.0",
"jest-mock-extended": "^3.0.1", "jest-mock-extended": "^3.0.4",
"prettier": "^2.3.2", "prettier": "^2.8.7",
"supertest": "^6.1.3", "supertest": "^6.3.3",
"ts-jest": "^27.0.3", "ts-jest": "^29.1.0",
"ts-loader": "^9.2.3", "ts-loader": "^9.4.2",
"ts-mockito": "^2.6.1", "ts-mockito": "^2.6.1",
"ts-node": "^10.0.0", "ts-node": "^10.9.1",
"tsconfig-paths": "^4.0.0", "tsconfig-paths": "^4.2.0",
"typescript": "^4.3.5" "typescript": "^5.0.4"
}, },
"jest": { "jest": {
"moduleFileExtensions": [ "moduleFileExtensions": [

View File

@ -1,7 +1,8 @@
import { REQUEST } from '@nestjs/core'; import { REQUEST } from '@nestjs/core';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { FilterQuery, Model } from 'mongoose'; import { Model } from 'mongoose';
import { lastValueFrom } from 'rxjs'; import { lastValueFrom } from 'rxjs';
import { Comment } from '../database/comment.model'; import { Comment } from '../database/comment.model';
import { COMMENT_MODEL, POST_MODEL } from '../database/database.constants'; import { COMMENT_MODEL, POST_MODEL } from '../database/database.constants';
import { Post } from '../database/post.model'; import { Post } from '../database/post.model';
@ -99,22 +100,15 @@ describe('PostService', () => {
expect(data.length).toBe(3); expect(data.length).toBe(3);
expect(model.find).toHaveBeenCalled(); expect(model.find).toHaveBeenCalled();
jest jest.spyOn(model, 'find').mockImplementation(() => {
.spyOn(model, 'find') return {
.mockImplementation( skip: jest.fn().mockReturnValue({
( limit: jest.fn().mockReturnValue({
conditions: FilterQuery<Post>, exec: jest.fn().mockResolvedValueOnce([posts[0]]),
callback?: (err: any, res: Post[]) => void, }),
) => { }),
return { } as any;
skip: jest.fn().mockReturnValue({ });
limit: jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValueOnce([posts[0]]),
}),
}),
} as any;
},
);
const result = await lastValueFrom(service.findAll('Generate', 0, 10)); const result = await lastValueFrom(service.findAll('Generate', 0, 10));
expect(result.length).toBe(1); expect(result.length).toBe(1);
@ -171,14 +165,14 @@ describe('PostService', () => {
const toReturned = { const toReturned = {
_id: '5ee49c3115a4e75254bb732e', _id: '5ee49c3115a4e75254bb732e',
...toCreated, ...toCreated,
} as Post; } as any;
jest jest
.spyOn(model, 'create') .spyOn(model, 'create')
.mockImplementation(() => Promise.resolve(toReturned)); .mockImplementation(() => Promise.resolve([toReturned]));
const data = await lastValueFrom(service.save(toCreated)); const data = await lastValueFrom(service.save(toCreated));
expect(data._id).toBe('5ee49c3115a4e75254bb732e'); expect(data[0]._id).toBe('5ee49c3115a4e75254bb732e');
expect(model.create).toBeCalledWith({ expect(model.create).toBeCalledWith({
...toCreated, ...toCreated,
createdBy: { createdBy: {
@ -300,26 +294,19 @@ describe('PostService', () => {
}); });
it('should get comments of post ', async () => { it('should get comments of post ', async () => {
jest jest.spyOn(commentModel, 'find').mockImplementation(() => {
.spyOn(commentModel, 'find') return {
.mockImplementation( select: jest.fn().mockReturnValue({
( exec: jest.fn().mockResolvedValue([
conditions: FilterQuery<Comment>, {
callback?: (err: any, res: Comment[]) => void, _id: 'test',
) => { content: 'content',
return { post: { _id: '_test_id' },
select: jest.fn().mockReturnValue({ },
exec: jest.fn().mockResolvedValue([ ] as any),
{ }),
_id: 'test', } as any;
content: 'content', });
post: { _id: '_test_id' },
},
] as any),
}),
} as any;
},
);
const result = await lastValueFrom(service.commentsOf('test')); const result = await lastValueFrom(service.commentsOf('test'));
expect(result.length).toBe(1); expect(result.length).toBe(1);

View File

@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { Model } from 'mongoose'; import { Model, FilterQuery } from 'mongoose';
import { lastValueFrom, of } from 'rxjs'; import { lastValueFrom, of } from 'rxjs';
import { USER_MODEL } from '../database/database.constants'; import { USER_MODEL } from '../database/database.constants';
@ -91,16 +91,15 @@ describe('UserService', () => {
}); });
it('findByUsername should return user', async () => { it('findByUsername should return user', async () => {
jest jest.spyOn(model, 'findOne').mockImplementation(
.spyOn(model, 'findOne') (filter?: FilterQuery<User>) =>
.mockImplementation((filter: any, callback: any) => { ({
return {
exec: jest.fn().mockResolvedValue({ exec: jest.fn().mockResolvedValue({
username: 'hantsy', username: 'hantsy',
email: 'hantsy@example.com', email: 'hantsy@example.com',
} as User), } as User),
} as any; } as any),
}); );
const foundUser = await lastValueFrom(service.findByUsername('hantsy')); const foundUser = await lastValueFrom(service.findByUsername('hantsy'));
expect(foundUser).toEqual({ expect(foundUser).toEqual({
@ -113,18 +112,16 @@ describe('UserService', () => {
describe('findById', () => { describe('findById', () => {
it('return one result', async () => { it('return one result', async () => {
jest jest.spyOn(model, 'findOne')
.spyOn(model, 'findOne') .mockImplementation(
.mockImplementation( (filter?: FilterQuery<User>) =>
(filter: any, callback: any) => { ({
return { exec: jest.fn().mockResolvedValue({
exec: jest.fn().mockResolvedValue({ username: 'hantsy',
username: 'hantsy', email: 'hantsy@example.com',
email: 'hantsy@example.com', } as User),
} as User), } as any),
} as any; );
},
);
const foundUser = await lastValueFrom(service.findById('hantsy')); const foundUser = await lastValueFrom(service.findById('hantsy'));
expect(foundUser).toEqual({ expect(foundUser).toEqual({
@ -138,13 +135,9 @@ describe('UserService', () => {
it('return a null result', async () => { it('return a null result', async () => {
jest jest
.spyOn(model, 'findOne') .spyOn(model, 'findOne')
.mockImplementation( .mockImplementation((filter?: FilterQuery<User>) => ({
(filter: any, callback: any) => { exec: jest.fn().mockResolvedValue(null) as any,
return { } as any));
exec: jest.fn().mockResolvedValue(null) as any,
} as any;
},
);
try { try {
const foundUser = await lastValueFrom(service.findById('hantsy')); const foundUser = await lastValueFrom(service.findById('hantsy'));
@ -156,17 +149,13 @@ describe('UserService', () => {
it('parameter withPosts=true', async () => { it('parameter withPosts=true', async () => {
jest jest
.spyOn(model, 'findOne') .spyOn(model, 'findOne')
.mockImplementation( .mockImplementation((filter?: FilterQuery<User>) => ({
(filter: any, callback: any) => { populate: jest.fn().mockReturnThis(),
return { exec: jest.fn().mockResolvedValue({
populate: jest.fn().mockReturnThis(), username: 'hantsy',
exec: jest.fn().mockResolvedValue({ email: 'hantsy@example.com',
username: 'hantsy', } as User),
email: 'hantsy@example.com', } as any));
} as User),
} as any;
},
);
const foundUser = await lastValueFrom(service.findById('hantsy', true)); const foundUser = await lastValueFrom(service.findById('hantsy', true));
expect(foundUser).toEqual({ expect(foundUser).toEqual({