Skip to content

Commit

Permalink
添加jest作为单元测试框架
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfish2020 committed Jun 4, 2019
1 parent 259297f commit 3d0f973
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
- npm compiler

将TS编译成js

- npm run

运行node程序

- npm dev

运行node程序并且热更新

### 参考资料
- jest

https://jestjs.io/docs/en/getting-started

- ts-jest

https://github.com/kulshekhar/ts-jest


4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
"version": "1.0.0",
"scripts": {
"compiler": "tsc --watch",
"run": "ts-node ./src/index.ts",
"dev": "ts-node-dev ./src/index.ts"
"run": "ts-node src/http.ts",
"dev": "ts-node-dev src/http.ts",
"test": "jest"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/node": "^12.0.2",
"jest": "^24.8.0",
"ts-jest": "^24.0.2",
"ts-node": "^8.1.0",
"ts-node-dev": "^1.0.0-pre.39",
"typescript": "^3.4.5"
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts → src/http.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {IncomingMessage, OutgoingMessage} from "http";

const http = require("http")
const http = require("http");

http.createServer((req: IncomingMessage, res: OutgoingMessage) => {
res.write("hello world")
res.write("hello world");
res.end()
}).listen(8088)

console.log("hello world")
}).listen(8088);
11 changes: 11 additions & 0 deletions src/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function add(a: number, b: number): number {
return a + b
}

export function minus(a: number, b: number): number {
return a - b
}

export function multi(a: number, b: number): number {
return a * b
}
Empty file added test/http.test.ts
Empty file.
13 changes: 13 additions & 0 deletions test/math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {add, minus, multi} from '../src/math'

test('adds 1 + 2 to equal 3', () => {
expect(add(1, 2)).toBe(3);
});

test('adds 1 - 2 to equal -1', () => {
expect(minus(1, 2)).toBe(-1);
});

test('adds 1 * 2 to equal 2', () => {
expect(multi(1, 2)).toBe(2);
});

0 comments on commit 3d0f973

Please sign in to comment.