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:
- install
- build
- deploy
variables:
GIT_DEPTH: "1"
build:
stage: build
npm_ci:
stage: install
tags:
- linux
script:
- npm ci
artifacts:
paths:
- node_modules
.build_base:
stage: build
tags:
- linux
dependencies:
- npm_ci
build:
extends:
- .build_base
script:
- npm run build
artifacts:
paths:
- dist/
unit-test:
extends:
- .build_base
script:
- npm run test
deploy_npm:
stage: deploy
dependencies:
......
......@@ -11,3 +11,5 @@ Dockerfile
.prettierrc
webpack.config.js
tsconfig.json
/tests
/dist/tests
\ No newline at end of file
This diff is collapsed.
......@@ -6,7 +6,8 @@
"typings": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "tsc"
"build": "tsc",
"test": "jest --passWithNoTests"
},
"repository": {
"type": "git",
......@@ -39,20 +40,27 @@
"devDependencies": {
"@nestjs/common": "^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-bodyparser": "^4.3.3",
"@types/lodash": "^4.14.175",
"@types/node": "^16.10.2",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"express": "^4.17.1",
"jest": "^27.4.4",
"koishi": "^4.0.0-beta.4",
"prettier": "^2.4.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.4.0",
"supertest": "^6.1.6",
"ts-jest": "^27.1.1",
"typescript": "^4.4.3"
},
"dependencies": {
......@@ -63,5 +71,22 @@
"koa-bodyparser": "^4.3.0",
"lodash": "^4.17.21",
"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 {
consumer.apply(KoishiMiddleware).forRoutes('*');
}
static register(options: KoishiModuleOptions): DynamicModule {
static register(options: KoishiModuleOptions = {}): DynamicModule {
return {
module: KoishiModule,
providers: [
......
......@@ -20,6 +20,9 @@ import { KoishiLoggerService } from './providers/koishi-logger.service';
import { KoishiHttpDiscoveryService } from './koishi-http-discovery/koishi-http-discovery.service';
import { Filter, ReplacedContext } from './utility/replaced-context';
// eslint-disable-next-line @typescript-eslint/no-empty-function
Router.prepare = () => {};
@Injectable()
export class KoishiService
extends App
......@@ -37,6 +40,7 @@ export class KoishiService
...koishiModuleOptions,
port: 0,
});
this.options.baseDir ||= process.cwd();
this.globalInterceptors = this.koishiModuleOptions.globalInterceptors || [];
this.router = new Router();
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 @@
"include": [
"*.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