Skip to content
This repository was archived by the owner on Oct 28, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions demo-unittest-jest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# midway-demo-unittest-jest

使用 jest 进行单测

## 使用

`npm run test`

调整了测试框架为 jest,修改了 test/cov 命令。


## @types/mocha 和 @types/jest 冲突问题

不要在依赖里安装两个不同的定义,或者相关的模块。

> 本示例由于在 lerna 仓库,无法去除根路径下的 @types/mocha,只能修改 tsconfig.json

**修改方法**

```json
"compilerOptions": {
"types": ["jest"]
},
```

或者

```json
"compilerOptions": {
"skipLibCheck": true
}
```
4 changes: 4 additions & 0 deletions demo-unittest-jest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'midway-bin/jest/env.js'
};
26 changes: 26 additions & 0 deletions demo-unittest-jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "demo-unittest-jest",
"version": "1.0.0",
"private": true,
"dependencies": {
"midway": "1"
},
"devDependencies": {
"@types/jest": "^24.0.17",
"jest": "^24.8.0",
"midway-bin": "1",
"midway-demo-lib": "1",
"ts-jest": "24.0.2"
},
"engines": {
"node": ">=8.9.0"
},
"scripts": {
"dev": "NODE_ENV=local midway-bin dev --ts",
"debug": "NODE_ENV=local midway-bin debug --ts",
"test": "jest",
"cov": "jest --coverage",
"ci": "jest --coverage",
"build": "midway-bin build -c"
}
}
11 changes: 11 additions & 0 deletions demo-unittest-jest/src/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { controller, get, provide } from 'midway';

@provide()
@controller('/')
export class HomeController {

@get('/')
async index(ctx) {
ctx.body = `Welcome to midwayjs!`;
}
}
16 changes: 16 additions & 0 deletions demo-unittest-jest/src/app/controller/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { controller, get, inject, provide } from 'midway';
import { IUserService, IUserResult } from '../../interface';

@provide()
@controller('/user')
export class UserController {
@inject('userService')
service: IUserService;

@get('/:id')
async getUser(ctx): Promise<void> {
const id: number = ctx.params.id;
const user: IUserResult = await this.service.getUser({id});
ctx.body = {success: true, message: 'OK', data: user};
}
}
12 changes: 12 additions & 0 deletions demo-unittest-jest/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = (appInfo: any) => {
const config: any = exports = {};

// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1539671912752_2826';

// add your config here
config.middleware = [
];

return config;
};
8 changes: 8 additions & 0 deletions demo-unittest-jest/src/config/config.unittest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = (appInfo) => {
return {
test: {
a: 1,
b: 2
}
}
}
2 changes: 2 additions & 0 deletions demo-unittest-jest/src/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// had enabled by midway
// exports.static = true;
23 changes: 23 additions & 0 deletions demo-unittest-jest/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @description User-Service parameters
*/
export interface IUserOptions {
id: number;
}

/**
* @description User-Service response
*/
export interface IUserResult {
id: number;
username: string;
phone: string;
email?: string;
}

/**
* @description User-Service abstractions
*/
export interface IUserService {
getUser(options: IUserOptions): Promise<IUserResult>;
}
19 changes: 19 additions & 0 deletions demo-unittest-jest/src/lib/service/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { provide } from 'midway';
import { IUserService, IUserOptions, IUserResult } from '../../interface';

@provide('userService')
export class UserService implements IUserService {

async getUser(options: IUserOptions): Promise<IUserResult> {
return new Promise<IUserResult>((resolve) => {
setTimeout(() => {
resolve({
id: options.id,
username: 'mockedName',
phone: '12345678901',
email: 'xxx.xxx@xxx.com',
});
}, 10);
});
}
}
31 changes: 31 additions & 0 deletions demo-unittest-jest/test/controller/home.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as assert from 'assert';
import { mm } from 'midway-mock';

describe('test/controller/home.test.ts', () => {

let app;

beforeAll(() => {
app = mm.app({
typescript: true,
});
return app.ready();
});

it('should assert config.keys', () => {
const pkg = require('../../package.json');
assert(app.config.keys.startsWith(pkg.name));
});

it('should get untest test config', () => {
assert(app.config.test.a === 1);
});

it('should GET /', () => {
return app
.httpRequest()
.get('/')
.expect('Welcome to midwayjs!')
.expect(200);
});
});
7 changes: 7 additions & 0 deletions demo-unittest-jest/test/hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as assert from 'assert';

describe('test/hello.test.ts', () => {
it('should work', () => {
assert(Date.now() > 0);
});
});
21 changes: 21 additions & 0 deletions demo-unittest-jest/test/service/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as assert from 'assert';
import { mm } from 'midway-mock';

describe('test/service/user.test.ts', () => {

let app;

beforeAll(() => {
app = mm.app({
typescript: true,
});
return app.ready()
});

it('#getUser', async () => {
const user = await app.applicationContext.getAsync('userService');
const data = await user.getUser({ id: 1 });
assert(data.id === 1);
assert(data.username === 'mockedName');
});
});
26 changes: 26 additions & 0 deletions demo-unittest-jest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noImplicitThis": false,
"noUnusedLocals": true,
"stripInternal": true,
"pretty": true,
"declaration": true,
"outDir": "dist",
"lib": ["es2017", "dom"],
"sourceMap": true,
"types": ["jest"]
},
"exclude": [
"node_modules",
"dist",
"src/app/public",
"src/app/view",
"test"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"cov": "lerna run cov",
"ci": "npm run build && npm run cov",
"build": "lerna run build",
"bootstrap": "rm -f ./packages/.DS*; lerna bootstrap --hoist --no-ci",
"bootstrap": "rm -f ./packages/.DS*; lerna bootstrap --hoist --nohoist=@types/jest --no-ci",
"purge": "npm run clean; rm -rf node_modules; ",
"clean": "lerna clean --yes; sh ./scripts/clean.sh"
}
Expand Down