-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Cophr/ci/enhance-lint-rules
ci/enhance lint rules
- Loading branch information
Showing
29 changed files
with
488 additions
and
264 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"project": "./tsconfig.json", | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint/eslint-plugin", | ||
"simple-import-sort", | ||
"import" | ||
], | ||
"extends": [ | ||
"eslint:all", | ||
"plugin:@typescript-eslint/all", | ||
"plugin:typescript-sort-keys/recommended", | ||
"prettier" | ||
], | ||
"root": true, | ||
"env": { | ||
"node": true, | ||
"jest": true | ||
}, | ||
"ignorePatterns": [".eslintrc.js"], | ||
"rules": { | ||
// disabled rules (required) | ||
"no-void": "off", // To call promise functions without other actions | ||
"no-magic-numbers": "off", // This will wrongly detect a number passed to a function as a magic number | ||
"@typescript-eslint/no-magic-numbers": "off", | ||
"new-cap": "off", // The decorators will be wrongly detected | ||
"class-methods-use-this": "off", // some methods are not using this | ||
"require-await": "off", // it will wrongly detect async methods as not having await | ||
"@typescript-eslint/require-await": "off", | ||
"max-params": "off", // injection constructors need many parameters | ||
|
||
// disabled rules (optional, in personal preference) | ||
"no-console": "off", // This will disable console.log, console.error, etc. This could be removed if we have our own logger | ||
"one-var": "off", // in favor to not merge variables to one | ||
"no-continue": "off", // in favor to use continue | ||
|
||
// disabled typescript-eslint rules (too strict) | ||
"@typescript-eslint/promise-function-async": "off", | ||
"@typescript-eslint/explicit-member-accessibility": "off", | ||
"@typescript-eslint/prefer-readonly-parameter-types": "off", | ||
"@typescript-eslint/parameter-properties": "off", | ||
"@typescript-eslint/no-extraneous-class": "off", | ||
"@typescript-eslint/no-misused-promises": "off", | ||
"@typescript-eslint/member-ordering": "off", | ||
"@typescript-eslint/no-type-alias": "off", | ||
"@typescript-eslint/unbound-method": "off", | ||
"@typescript-eslint/strict-boolean-expressions": "off", | ||
|
||
// modified rules | ||
"func-style": ["error", "declaration", { "allowArrowFunctions": true }], | ||
|
||
// replaced rules | ||
"no-useless-constructor": "off", | ||
"@typescript-eslint/no-useless-constructor": "error", | ||
|
||
"no-shadow": "off", | ||
"@typescript-eslint/no-shadow": "error", | ||
|
||
"sort-imports": "off", | ||
"simple-import-sort/imports": "error", | ||
"simple-import-sort/exports": "error", | ||
"import/first": "error", | ||
"import/newline-after-import": "error", | ||
"import/no-duplicates": "error", | ||
|
||
// additional rules | ||
"@typescript-eslint/consistent-type-exports": "error", | ||
"@typescript-eslint/consistent-type-imports": [ | ||
"error", | ||
{ | ||
"fixStyle": "inline-type-imports" | ||
} | ||
], | ||
|
||
"padding-line-between-statements": [ | ||
"error", | ||
{ "blankLine": "always", "prev": "*", "next": "return" }, | ||
{ | ||
"blankLine": "always", | ||
"prev": ["const", "let", "var", "multiline-expression"], | ||
"next": "*" | ||
}, | ||
{ | ||
"blankLine": "any", | ||
"prev": ["const", "let", "var"], | ||
"next": ["const", "let", "var"] | ||
} | ||
], | ||
"@typescript-eslint/interface-name-prefix": "off", | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
"@typescript-eslint/explicit-module-boundary-types": "off", | ||
"@typescript-eslint/no-unused-vars": "error" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.spec.ts", "*.e2e-spec.ts"], | ||
"rules": { | ||
"@typescript-eslint/init-declarations": "off", | ||
"max-lines-per-function": "off" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
build | ||
dist | ||
coverage | ||
e2e | ||
node_modules | ||
pnpm-lock.yaml | ||
*.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"arrowParens": "avoid", | ||
"endOfLine": "auto", | ||
"semi": true, | ||
"tabWidth": 2, | ||
"singleQuote": false, | ||
"trailingComma": "all", | ||
"arrowParens": "avoid", | ||
"endOfLine": "auto" | ||
"tabWidth": 2, | ||
"trailingComma": "all" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
import { ConflictException } from "@nestjs/common"; | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
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"; | ||
import { UserEntity } from "src/user/entities/user.entity"; | ||
import { CreateUserRespose } from "src/user/resposes/create-user-respose"; | ||
import type { CreateUserRespose } from "src/user/resposes/create-user-respose"; | ||
import { UserService } from "src/user/user.service"; | ||
import { Repository } from "typeorm"; | ||
import type { Repository } from "typeorm"; | ||
|
||
import { CreateUserDto } from "../user/dto/create-user.dto"; | ||
import type { CreateUserDto } from "../user/dto/create-user.dto"; | ||
import { AuthController } from "./auth.controller"; | ||
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({ | ||
imports: [TypeOrmModule.forRoot(dataSourceJest)], | ||
controllers: [AuthController], | ||
imports: [TypeOrmModule.forRoot(dataSourceJest)], | ||
providers: [ | ||
AuthService, | ||
UserService, | ||
{ | ||
provide: getRepositoryToken(UserEntity), | ||
useValue: UserEntity, // 使用測試資料庫的 Repository | ||
// 使用測試資料庫的 Repository | ||
useValue: UserEntity, | ||
}, | ||
], | ||
}).compile(); | ||
|
@@ -35,45 +37,49 @@ describe("AuthController", () => { | |
getRepositoryToken(UserEntity), | ||
); | ||
}); | ||
|
||
describe("create", () => { | ||
it("應該會創建一個使用者,並返回 201 狀態碼", async () => { | ||
const createUserDto: CreateUserDto = { | ||
account: "account", | ||
email: "[email protected]", | ||
name: "displayname", | ||
account: "account", | ||
password: "Password@123", | ||
}; | ||
const expectedResponse: CreateUserRespose = { | ||
statusCode: 201, | ||
message: "創建成功", | ||
statusCode: 201, | ||
}; | ||
|
||
jest.spyOn(authService, "register").mockResolvedValue(expectedResponse); | ||
const result = await authController.register(createUserDto); | ||
|
||
expect(result).toEqual(expectedResponse); | ||
}); | ||
|
||
it("應該會發生資料使用者重覆,並返回 409 狀態碼", async () => { | ||
const createUserDto1: CreateUserDto = { | ||
account: "account1", | ||
email: "[email protected]", | ||
name: "displayname", | ||
account: "account1", | ||
password: "Password@123", | ||
}; | ||
try { | ||
await authService.register(createUserDto1); | ||
await authService.register(createUserDto1); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(ConflictException); | ||
expect(error.response).toEqual({ | ||
statusCode: 409, | ||
message: ["email 已被註冊。", "account 已被註冊。"], | ||
error: "Conflict", | ||
|
||
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.