Commit e2ef9fd7 authored by nanahira's avatar nanahira

unit test and bump

parent eb83403f
Pipeline #7767 passed with stages
in 1 minute and 42 seconds
stages: stages:
- install
- build - build
- deploy - deploy
variables: variables:
GIT_DEPTH: "1" GIT_DEPTH: "1"
build: npm_ci:
stage: build stage: install
tags: tags:
- linux - linux
script: script:
- npm ci - npm ci
artifacts:
paths:
- node_modules
.build_base:
stage: build
tags:
- linux
dependencies:
- npm_ci
build:
extends:
- .build_base
script:
- npm run build - npm run build
artifacts: artifacts:
paths: paths:
- dist/ - dist/
unit-test:
extends:
- .build_base
script:
- npm run test
deploy_npm: deploy_npm:
stage: deploy stage: deploy
dependencies: dependencies:
......
...@@ -11,3 +11,5 @@ Dockerfile ...@@ -11,3 +11,5 @@ Dockerfile
.prettierrc .prettierrc
webpack.config.js webpack.config.js
tsconfig.json tsconfig.json
/tests
/dist/tests
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
"scripts": { "scripts": {
"lint": "eslint --fix .", "lint": "eslint --fix .",
"build": "tsc" "build": "tsc",
"test": "jest --passWithNoTests"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
...@@ -39,20 +40,27 @@ ...@@ -39,20 +40,27 @@
"devDependencies": { "devDependencies": {
"@nestjs/common": "^8.0.9", "@nestjs/common": "^8.0.9",
"@nestjs/core": "^8.0.9", "@nestjs/core": "^8.0.9",
"@nestjs/platform-express": "^8.2.3",
"@nestjs/testing": "^8.2.3",
"@types/jest": "^27.0.3",
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa-bodyparser": "^4.3.3", "@types/koa-bodyparser": "^4.3.3",
"@types/lodash": "^4.14.175", "@types/lodash": "^4.14.175",
"@types/node": "^16.10.2", "@types/node": "^16.10.2",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.32.0", "@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0", "@typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1", "eslint-plugin-prettier": "^3.4.1",
"express": "^4.17.1", "express": "^4.17.1",
"jest": "^27.4.4",
"koishi": "^4.0.0-beta.4", "koishi": "^4.0.0-beta.4",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rxjs": "^7.4.0", "rxjs": "^7.4.0",
"supertest": "^6.1.6",
"ts-jest": "^27.1.1",
"typescript": "^4.4.3" "typescript": "^4.4.3"
}, },
"dependencies": { "dependencies": {
...@@ -63,5 +71,22 @@ ...@@ -63,5 +71,22 @@
"koa-bodyparser": "^4.3.0", "koa-bodyparser": "^4.3.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"ws": "^8.2.3" "ws": "^8.2.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "tests",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
} }
} }
...@@ -57,7 +57,7 @@ export class KoishiModule implements NestModule { ...@@ -57,7 +57,7 @@ export class KoishiModule implements NestModule {
consumer.apply(KoishiMiddleware).forRoutes('*'); consumer.apply(KoishiMiddleware).forRoutes('*');
} }
static register(options: KoishiModuleOptions): DynamicModule { static register(options: KoishiModuleOptions = {}): DynamicModule {
return { return {
module: KoishiModule, module: KoishiModule,
providers: [ providers: [
......
...@@ -20,6 +20,9 @@ import { KoishiLoggerService } from './providers/koishi-logger.service'; ...@@ -20,6 +20,9 @@ import { KoishiLoggerService } from './providers/koishi-logger.service';
import { KoishiHttpDiscoveryService } from './koishi-http-discovery/koishi-http-discovery.service'; import { KoishiHttpDiscoveryService } from './koishi-http-discovery/koishi-http-discovery.service';
import { Filter, ReplacedContext } from './utility/replaced-context'; import { Filter, ReplacedContext } from './utility/replaced-context';
// eslint-disable-next-line @typescript-eslint/no-empty-function
Router.prepare = () => {};
@Injectable() @Injectable()
export class KoishiService export class KoishiService
extends App extends App
...@@ -37,6 +40,7 @@ export class KoishiService ...@@ -37,6 +40,7 @@ export class KoishiService
...koishiModuleOptions, ...koishiModuleOptions,
port: 0, port: 0,
}); });
this.options.baseDir ||= process.cwd();
this.globalInterceptors = this.koishiModuleOptions.globalInterceptors || []; this.globalInterceptors = this.koishiModuleOptions.globalInterceptors || [];
this.router = new Router(); this.router = new Router();
this._nestKoaTmpInstance.use(KoaBodyParser()); this._nestKoaTmpInstance.use(KoaBodyParser());
......
import { Test } from '@nestjs/testing';
import { KoishiModule } from '../src/koishi.module';
import { KoishiService } from '../src/koishi.service';
import { INestApplication } from '@nestjs/common';
import { KoishiWsAdapter } from '../src/koishi.ws-adapter';
import http from 'http';
import request from 'supertest';
describe('HttpServer', () => {
let app: INestApplication;
let koishiApp: KoishiService;
beforeEach(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [
KoishiModule.register({
useWs: true,
}),
],
}).compile();
app = moduleFixture.createNestApplication();
app.useWebSocketAdapter(new KoishiWsAdapter(app));
await app.init();
koishiApp = app.get(KoishiService);
});
it('should define koishi', () => {
expect(koishiApp).toBeDefined();
});
it('should register http and ws server', () => {
expect(koishiApp._httpServer).toBeDefined();
expect(koishiApp._wsServer).toBeDefined();
});
it('should be nest http server', () => {
expect(koishiApp._httpServer).toBeInstanceOf(http.Server);
expect(app.getHttpServer()).toEqual(koishiApp._httpServer);
});
it('should response to koishi routes', () => {
koishiApp.router.get('/ping', (ctx) => {
ctx.status = 233;
ctx.body = 'pong';
});
return request(app.getHttpServer()).get('/ping').expect(233).expect('pong');
});
});
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
"include": [ "include": [
"*.ts", "*.ts",
"src/**/*.ts", "src/**/*.ts",
"test/**/*.ts" "test/**/*.ts",
"tests/**/*.ts"
] ]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment