Skip to content

Commit

Permalink
feat: init nestjs (cuixueshe#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 authored Feb 10, 2023
1 parent b3ae78f commit 073a184
Show file tree
Hide file tree
Showing 19 changed files with 3,101 additions and 836 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
**/*.cy.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
.turbo
coverage
Empty file removed apps/backend/.gitkeep
Empty file.
27 changes: 27 additions & 0 deletions apps/backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Backend

## Running the app

```bash
# development
$ pnpm run start

# watch mode
$ pnpm run start:dev

# production mode
$ pnpm run start:prod
```

## Test

```bash
# unit tests
$ pnpm run test

# e2e tests
$ pnpm run test:e2e

# test coverage
$ pnpm run test:cov
```
8 changes: 8 additions & 0 deletions apps/backend/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
48 changes: 48 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "backend",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "nest build",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"test": "vitest run",
"test:watch": "vitest",
"test:cov": "vitest run --coverage",
"test:e2e": "vitest run --config test/vitest-e2e.config.ts"
},
"dependencies": {
"@nestjs/common": "^9.3.5",
"@nestjs/core": "^9.3.5",
"@nestjs/platform-express": "^9.3.5",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
},
"devDependencies": {
"@nestjs/cli": "^9.2.0",
"@nestjs/schematics": "^9.0.4",
"@nestjs/testing": "^9.3.5",
"@swc/core": "^1.3.32",
"@types/express": "^4.17.17",
"@types/node": "18.13.0",
"@types/superagent": "^4.1.15",
"@types/supertest": "^2.0.12",
"@vitest/coverage-c8": "^0.28.4",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"typescript": "^4.9.5",
"unplugin-swc": "^1.3.2",
"vite": "^4.0.4",
"vitest": "^0.27.0",
"webpack": "^5.75.0"
},
"eslintConfig": {
"extends": "@antfu",
"rules": {
"@typescript-eslint/consistent-type-imports": "off",
"@typescript-eslint/ban-ts-comment": "off"
}
}
}
21 changes: 21 additions & 0 deletions apps/backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { TestingModule } from '@nestjs/testing'
import { Test } from '@nestjs/testing'
import { AppController } from './app.controller'
import { AppService } from './app.service'

describe('AppController', () => {
let appController: AppController

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile()

appController = app.get<AppController>(AppController)
})

it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!')
})
})
12 changes: 12 additions & 0 deletions apps/backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common'
import { AppService } from './app.service'

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello()
}
}
10 changes: 10 additions & 0 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
8 changes: 8 additions & 0 deletions apps/backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common'

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!'
}
}
8 changes: 8 additions & 0 deletions apps/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'

async function bootstrap() {
const app = await NestFactory.create(AppModule)
await app.listen(3000)
}
bootstrap()
25 changes: 25 additions & 0 deletions apps/backend/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { TestingModule } from '@nestjs/testing'
import { Test } from '@nestjs/testing'
import type { INestApplication } from '@nestjs/common'
import request from 'supertest'
import { AppModule } from '@/app.module'

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

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile()

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

it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!')
})
})
9 changes: 9 additions & 0 deletions apps/backend/test/vitest-e2e.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config'
import { mergeConfig } from 'vite'
import defaultConfig from '../vitest.config'

export default mergeConfig(defaultConfig, defineConfig({
test: {
include: ['**/*.e2e-spec.ts'],
},
}))
4 changes: 4 additions & 0 deletions apps/backend/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "vitest.*"]
}
29 changes: 29 additions & 0 deletions apps/backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "esnext",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"types": [
"vitest/globals"
],
"paths": {
"@/*": [
"src/*"
]
}
}
}
16 changes: 16 additions & 0 deletions apps/backend/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'vitest/config'
import swc from 'unplugin-swc'

export default defineConfig({
plugins: [
swc.vite(),
],
test: {
globals: true,
},
resolve: {
alias: {
'@/': '/src/',
},
},
})
2 changes: 1 addition & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"cypress": "^12.3.0",
"cypress-promise": "^1.1.0",
"jsdom": "^21.0.0",
"typescript": "^4.9.4",
"typescript": "^4.9.5",
"unocss": "^0.48.3",
"vite": "^4.0.4",
"vitest": "^0.27.0",
Expand Down
24 changes: 11 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@
"scripts": {
"bootstrap": "cross-env CYPRESS_INSTALL_BINARY=0 pnpm i",
"dev:fe": "pnpm -F frontend dev",
"dev:be": "pnpm -F backend start",
"build:fe": "pnpm -F frontend build",
"build": "turbo build",
"test": "pnpm -F frontend test",
"test": "pnpm -F frontend test && pnpm -F backend test && pnpm -F backend test:e2e",
"prepare": "simple-git-hooks",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"dep:up": "taze -I major"
"dep:up": "taze -Ir major"
},
"devDependencies": {
"@antfu/eslint-config": "^0.34.1",
"@commitlint/cli": "^17.4.1",
"@commitlint/config-conventional": "^17.4.0",
"@types/node": "^18.11.18",
"eslint": "^8.31.0",
"lint-staged": "^13.1.0",
"@commitlint/cli": "^17.4.2",
"@commitlint/config-conventional": "^17.4.2",
"@types/node": "^18.13.0",
"eslint": "^8.33.0",
"lint-staged": "^13.1.1",
"simple-git-hooks": "^2.8.1",
"taze": "^0.8.5",
"turbo": "^1.7.0",
"typescript": "^4.9.4"
"turbo": "^1.7.4",
"typescript": "^4.9.5"
},
"simple-git-hooks": {
"pre-commit": "pnpm exec lint-staged",
Expand All @@ -40,10 +41,7 @@
]
},
"eslintConfig": {
"extends": "@antfu",
"ignorePatterns": [
"**/*.cy.js"
]
"extends": "@antfu"
},
"commitlint": {
"extends": [
Expand Down
Loading

0 comments on commit 073a184

Please sign in to comment.