Skip to content

Commit

Permalink
style: apply new lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
moontai0724 committed Jun 10, 2023
1 parent 377400c commit 3dd0d02
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 95 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.13",
"@types/jest": "28.1.8",
"@types/node": "^16.0.0",
Expand Down
28 changes: 13 additions & 15 deletions src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConflictException } from "@nestjs/common";
import { type HttpException, ConflictException } from "@nestjs/common";
import { type TestingModule, Test } from "@nestjs/testing";
import { getRepositoryToken, TypeOrmModule } from "@nestjs/typeorm";
import { dataSourceJest } from "src/config/data-source";
Expand All @@ -14,7 +14,7 @@ import { AuthService } from "./auth.service";
describe("AuthController", () => {
let authController: AuthController;
let authService: AuthService;
let userRepository: Repository<UserEntity>;
let userRepository: Repository<UserEntity> | undefined;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand Down Expand Up @@ -65,23 +65,21 @@ describe("AuthController", () => {
password: "Password@123",
};

try {
await authService.register(createUserDto1);
await authService.register(createUserDto1);
} catch (error) {
expect(error).toBeInstanceOf(ConflictException);
expect(error.response).toEqual({
error: "Conflict",
message: ["email 已被註冊。", "account 已被註冊。"],
statusCode: 409,
await authService.register(createUserDto1);
await authService
.register(createUserDto1)
.catch((error: HttpException) => {
expect(error).toBeInstanceOf(ConflictException);
expect(error.getResponse()).toEqual({
error: "Conflict",
message: ["email 已被註冊。", "account 已被註冊。"],
statusCode: 409,
});
});
}
});
});

afterEach(async () => {
if (userRepository && userRepository.clear) {
await userRepository.clear();
}
await userRepository?.clear();
});
});
2 changes: 1 addition & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AuthController {
description: "使用者格式不符",
type: CreateUserBadrequestError,
})
register(@Body() userDto: CreateUserDto) {
async register(@Body() userDto: CreateUserDto) {
return this.authService.register(userDto);
}
}
52 changes: 25 additions & 27 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ConflictException, HttpStatus } from "@nestjs/common";
import {
type HttpException,
ConflictException,
HttpStatus,
} from "@nestjs/common";
import { type TestingModule, Test } from "@nestjs/testing";
import { getRepositoryToken, TypeOrmModule } from "@nestjs/typeorm";
import { validate } from "class-validator";
Expand All @@ -12,7 +16,7 @@ import { AuthService } from "./auth.service";

describe("AuthService", () => {
let authService: AuthService;
let userRepository: Repository<UserEntity>;
let userRepository: Repository<UserEntity> | undefined;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand Down Expand Up @@ -57,17 +61,17 @@ describe("AuthService", () => {
password: "Password@123",
};

try {
await authService.register(createUserDto1);
await authService.register(createUserDto1);
} catch (error) {
expect(error).toBeInstanceOf(ConflictException);
expect(error.response).toEqual({
error: "Conflict",
message: ["email 已被註冊。", "account 已被註冊。"],
statusCode: 409,
await authService.register(createUserDto1);
await authService
.register(createUserDto1)
.catch((error: HttpException) => {
expect(error).toBeInstanceOf(ConflictException);
expect(error.getResponse()).toEqual({
error: "Conflict",
message: ["email 已被註冊。", "account 已被註冊。"],
statusCode: 409,
});
});
}
});

it("應該會發生 email 已被註冊衝突", async () => {
Expand All @@ -86,17 +90,15 @@ describe("AuthService", () => {
const errors = await validate(rawUser1);

expect(errors.length).toBe(0);
try {
await authService.register(rawUser1);
await authService.register(rawUser2);
} catch (error) {
await authService.register(rawUser1);
await authService.register(rawUser2).catch((error: HttpException) => {
expect(error).toBeInstanceOf(ConflictException);
expect(error.response).toEqual({
expect(error.getResponse()).toEqual({
error: "Conflict",
message: ["email 已被註冊。"],
statusCode: 409,
});
}
});
});

it("應該會發生 account 已被註冊衝突", async () => {
Expand All @@ -115,23 +117,19 @@ describe("AuthService", () => {
const errors = await validate(rawUser1);

expect(errors.length).toBe(0);
try {
await authService.register(rawUser1);
await authService.register(rawUser2);
} catch (error) {
await authService.register(rawUser1);
await authService.register(rawUser2).catch((error: HttpException) => {
expect(error).toBeInstanceOf(ConflictException);
expect(error.response).toEqual({
expect(error.getResponse()).toEqual({
error: "Conflict",
message: ["account 已被註冊。"],
statusCode: 409,
});
}
});
});
});

afterEach(async () => {
if (userRepository && userRepository.clear) {
await userRepository.clear();
}
await userRepository?.clear();
});
});
6 changes: 3 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Repository } from "typeorm";
@Injectable()
export class AuthService {
constructor(
private userService: UserService,
private readonly userService: UserService,
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
private readonly userRepository: Repository<UserEntity>,
) {}

async register(userDto: CreateUserDto) {
Expand All @@ -20,7 +20,7 @@ export class AuthService {

if (existingUser) {
const keys = ["email", "account"];
const conflictedAttributes = [];
const conflictedAttributes: string[] = [];

keys.forEach(key => {
if (existingUser[key] === userDto[key]) {
Expand Down
2 changes: 1 addition & 1 deletion src/config/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const dataSourceOptions: DataSourceOptions = {
logging: false,
migrations: [`${__dirname}/../database/migrations/*.js`],
password: process.env.DB_PASSWORD,
port: parseInt(process.env.DB_PORT, 10),
port: parseInt(process.env.DB_PORT ?? "", 10),
synchronize: false,
timezone: process.env.DB_TIMEZONE,
type: "mysql",
Expand Down
1 change: 1 addition & 0 deletions src/config/env.validation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { plainToInstance } from "class-transformer";
import {
IsEnum,
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { validationPipe } from "./pipes/validation-pipe";
function setupSwagger(app: INestApplication) {
const builder = new DocumentBuilder();
const config = builder
.setTitle(process.env.APP_SWAGGER_Title)
.setDescription(process.env.APP_SWAGGER_Description)
.setVersion(process.env.APP_SWAGGER_Version)
.setTitle(process.env.APP_SWAGGER_Title ?? "Cophr")
.setDescription(process.env.APP_SWAGGER_Description ?? "")
.setVersion(process.env.APP_SWAGGER_Version ?? "N/A")
.build();
const document = SwaggerModule.createDocument(app, config);

Expand All @@ -25,4 +25,4 @@ async function bootstrap() {
await app.listen(3000);
}

bootstrap();
void bootstrap();
99 changes: 57 additions & 42 deletions src/user/dto/create-user.dto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ describe("createUser-DTO", () => {
type: "body",
};

await validationPipe.transform(createUserDto, metadata).catch(error => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.response).toEqual({
error: "Bad Request",
message: ["email 為必填欄位。", "email 必須是信箱格式。"],
statusCode: 400,
await validationPipe
.transform(createUserDto, metadata)
.catch((error: BadRequestException) => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.getResponse()).toEqual({
error: "Bad Request",
message: ["email 為必填欄位。", "email 必須是信箱格式。"],
statusCode: 400,
});
});
});
});

it("應該會發生 email 欄位格式驗證失敗", async () => {
Expand All @@ -40,14 +42,16 @@ describe("createUser-DTO", () => {
type: "body",
};

await validationPipe.transform(createUserDto, metadata).catch(error => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.response).toEqual({
error: "Bad Request",
message: ["email 必須是信箱格式。"],
statusCode: 400,
await validationPipe
.transform(createUserDto, metadata)
.catch((error: BadRequestException) => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.getResponse()).toEqual({
error: "Bad Request",
message: ["email 必須是信箱格式。"],
statusCode: 400,
});
});
});
});

it("應該會發生 name 欄位未填驗證失敗", async () => {
Expand All @@ -63,14 +67,16 @@ describe("createUser-DTO", () => {
type: "body",
};

await validationPipe.transform(createUserDto, metadata).catch(error => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.response).toEqual({
error: "Bad Request",
message: ["name 為必填欄位。"],
statusCode: 400,
await validationPipe
.transform(createUserDto, metadata)
.catch((error: BadRequestException) => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.getResponse()).toEqual({
error: "Bad Request",
message: ["name 為必填欄位。"],
statusCode: 400,
});
});
});
});

it("應該會發生 account 欄位未填驗證失敗", async () => {
Expand All @@ -86,14 +92,16 @@ describe("createUser-DTO", () => {
type: "body",
};

await validationPipe.transform(createUserDto, metadata).catch(error => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.response).toEqual({
error: "Bad Request",
message: ["account 為必填欄位。"],
statusCode: 400,
await validationPipe
.transform(createUserDto, metadata)
.catch((error: BadRequestException) => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.getResponse()).toEqual({
error: "Bad Request",
message: ["account 為必填欄位。"],
statusCode: 400,
});
});
});
});

it("應該會發生 password 欄位未填驗證失敗", async () => {
Expand All @@ -109,14 +117,19 @@ describe("createUser-DTO", () => {
type: "body",
};

await validationPipe.transform(createUserDto, metadata).catch(error => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.response).toEqual({
error: "Bad Request",
message: ["password 必須長度大於等於8個字。", "password 為必填欄位。"],
statusCode: 400,
await validationPipe
.transform(createUserDto, metadata)
.catch((error: BadRequestException) => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.getResponse()).toEqual({
error: "Bad Request",
message: [
"password 必須長度大於等於8個字。",
"password 為必填欄位。",
],
statusCode: 400,
});
});
});
});

it("應該會發生 password 欄位長度驗證失敗", async () => {
Expand All @@ -132,13 +145,15 @@ describe("createUser-DTO", () => {
type: "body",
};

await validationPipe.transform(createUserDto, metadata).catch(error => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.response).toEqual({
error: "Bad Request",
message: ["password 必須長度大於等於8個字。"],
statusCode: 400,
await validationPipe
.transform(createUserDto, metadata)
.catch((error: BadRequestException) => {
expect(error).toBeInstanceOf(BadRequestException);
expect(error.getResponse()).toEqual({
error: "Bad Request",
message: ["password 必須長度大於等於8個字。"],
statusCode: 400,
});
});
});
});
});
2 changes: 1 addition & 1 deletion src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UserEntity } from "./entities/user.entity";
@Injectable()
export class UserService {
async create(userDto: CreateUserDto) {
const hash = await bcrypt.hashSync(userDto.password, 5);
const hash = bcrypt.hashSync(userDto.password, 5);
const user = new UserEntity();

user.name = userDto.name;
Expand Down
2 changes: 1 addition & 1 deletion test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ describe("AppController (e2e)", () => {
await app.init();
});

it("/ (GET)", () =>
it("/ (GET)", async () =>
request(app.getHttpServer()).get("/").expect(200).expect("Hello World!"));
});
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,13 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/bcrypt@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@types/bcrypt/-/bcrypt-5.0.0.tgz#a835afa2882d165aff5690893db314eaa98b9f20"
integrity sha512-agtcFKaruL8TmcvqbndlqHPSJgsolhf/qPWchFlgnW1gECTN/nKbFcoFnvKAQRFfKbh+BO6A3SWdJu9t+xF3Lw==
dependencies:
"@types/node" "*"

"@types/body-parser@*":
version "1.19.2"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.2.tgz#aea2059e28b7658639081347ac4fab3de166e6f0"
Expand Down

0 comments on commit 3dd0d02

Please sign in to comment.