Skip to content

Commit 16dc601

Browse files
jest
1 parent 0adbdda commit 16dc601

15 files changed

Lines changed: 531 additions & 1 deletion
File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
["@babel/preset-env", {
4+
"targets": {
5+
"node": "current"
6+
}
7+
}]
8+
]
9+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/*
3+
# 安装
4+
1. 初始化 package.json 并安装 jest
5+
$ npm init
6+
$ npm install jest -D
7+
8+
2. 初始化 jest 并根据需要修改生成的配置文件 jest.config.js
9+
$ npx jest --init
10+
11+
3. 修改 scripts,添加执行指令
12+
# (1) 普通执行测试
13+
$ npx jest
14+
{
15+
"test": "jest"
16+
}
17+
18+
# (2) 生成覆盖率报告
19+
$ npx jest --coverage
20+
{
21+
"coverage": "jest --coverage"
22+
}
23+
24+
# (3) 持续监听变化,默认 o 模式
25+
{
26+
"test": "jest --watch"
27+
}
28+
29+
# (4) 持续监听所有文件变化
30+
{
31+
"test": "jest --watchAll"
32+
}
33+
34+
4. 编写对应测试用例文件 init.test.js
35+
36+
5. 执行测试
37+
$ npm run test
38+
$ npm run coverage
39+
*/
40+
function add (a, b) {
41+
return a + b;
42+
}
43+
44+
function minus (a, b) {
45+
return a - b;
46+
}
47+
48+
function multi (a, b) {
49+
return a * b;
50+
}
51+
52+
module.exports = {
53+
add,
54+
minus,
55+
multi
56+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
const math = require('./01-init.js');
3+
const {add, minus, multi} = math;
4+
5+
test('测试加法 3 + 7', () => {
6+
expect(add(3, 7)).toBe(10);
7+
})
8+
9+
test('测试减法 3 - 3', () => {
10+
expect(minus(3, 3)).toBe(0);
11+
})
12+
13+
test('测试乘法 3 * 3', () => {
14+
expect(multi(3, 3)).toBe(9);
15+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
jest 默认不支持 es6,需要使用 babel 来支持 es6
3+
4+
1. 安装babel
5+
$ npm install @babel/core @babel/preset-env -D
6+
7+
2. 配置babel,修改 .babelrc 文件
8+
{
9+
"presets": [
10+
["@babel/preset-env", {
11+
"targets": {
12+
"node": "current"
13+
}
14+
}]
15+
]
16+
}
17+
18+
*/
19+
20+
function add (a, b) {
21+
return a + b;
22+
}
23+
24+
function minus (a, b) {
25+
return a - b;
26+
}
27+
28+
function multi (a, b) {
29+
return a * b;
30+
}
31+
32+
module.exports = {
33+
add,
34+
minus,
35+
multi
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {add, minus, multi} from './02-es6.js';
2+
3+
test('测试加法 3 + 7', () => {
4+
expect(add(3, 7)).toBe(10);
5+
})
6+
7+
test('测试减法 3 - 3', () => {
8+
expect(minus(3, 3)).toBe(0);
9+
})
10+
11+
test('测试乘法 3 * 3', () => {
12+
expect(multi(3, 3)).toBe(9);
13+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* jest 匹配器
3+
* 见 test 文件
4+
*/
5+
function add (a, b) {
6+
return a + b;
7+
}
8+
9+
function minus (a, b) {
10+
return a - b;
11+
}
12+
13+
function multi (a, b) {
14+
return a * b;
15+
}
16+
17+
module.exports = {
18+
add,
19+
minus,
20+
multi
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
test('jest匹配器', () => {
3+
const a = { one: 1 };
4+
const b = null;
5+
const c = 0.1;
6+
const d = 0.2;
7+
const e = 'www.csxiaoyao.com';
8+
const f = ['www', 'csxiaoyao', 'com'];
9+
const g = new Set(f);
10+
const h = () => {
11+
throw new Error('this is a new error');
12+
};
13+
// 【 toBe 】 匹配器
14+
// expect(a).toBe({ one: 1 }); // false 地址不同
15+
// 【 toEqual 】 匹配器
16+
expect(a).toEqual({ one: 1 });
17+
18+
// 【 toBeNull 】【 toBeUndefined 】【 toBeDefined 】【 toBeTruthy 】【 toBeFalsy 】
19+
expect(b).toBeNull();
20+
21+
// 【 not 】
22+
expect(c).not.toBeFalsy();
23+
24+
// Number【 toBeCloseTo 】【 toBeGreaterThan 】【 toBeLessThan 】【 toBeGreaterThanOrEqual 】【 toBeLessThanOrEqual 】
25+
expect(10).toBeGreaterThan(9);
26+
expect(10).toBeGreaterThanOrEqual(10);
27+
expect(c + d).toBeCloseTo(0.3);
28+
29+
// String【 toMatch 】
30+
expect(e).toMatch('csxiaoyao');
31+
32+
// Array/Set 【 toContain 】
33+
expect(f).toContain('csxiaoyao');
34+
expect(g).toContain('csxiaoyao');
35+
36+
// 异常 【 toThrow 】
37+
expect(h).toThrow();
38+
expect(h).toThrow('this is a new error');
39+
expect(h).toThrow(/this is a new error/);
40+
// expect(h).not.toThrow();
41+
42+
// ...
43+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
jest --watchAll 时对应的命令行模式
3+
4+
› Press f to run only failed tests.
5+
› Press o to only run tests related to changed files.
6+
› Press p to filter by a filename regex pattern.
7+
› Press t to filter by a test name regex pattern.
8+
› Press q to quit watch mode.
9+
10+
其中 o 模式要配合 git 监听文件修改变化
11+
12+
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 异步处理
3+
*
4+
* 1. 安装 axios
5+
* $ npm install axios --save
6+
*
7+
* 2. 测试用例分别处理 callback 和 promise
8+
* 见 test 文件
9+
*
10+
*/
11+
import axios from 'axios';
12+
13+
// 传入 callback 函数
14+
export const fetchData = (fn) => {
15+
axios.get('http://www.csxiaoyao.com/api/temp/data.json').then((response) => {
16+
fn(response.data);
17+
})
18+
}
19+
20+
// 返回 promise
21+
export const fetchData2 = () => {
22+
return axios.get('http://www.csxiaoyao.com/api/temp/data.json')
23+
}
24+
25+
// export {fetchData, fetchData2}

0 commit comments

Comments
 (0)