仅是天才不能成为作家,因为书的背后极需要作家的人格。——爱默生

Jest是一款优雅、简洁的JavaScript测试框架。

官方文档:

https://jestjs.io/zh-Hans/

github:

https://github.com/jestjs/jest

创建项目:

1
2
3
4
5
6
7
8
9
10
11
12
13
Last login: Wed Sep 18 16:56:31 on ttys001

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
Github-Id-VampireAchao:streampark achao$ cd ~/IdeaProjects/simple-jest/
Github-Id-VampireAchao:simple-jest achao$ npm install --save-dev jest
npm WARN deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.

added 277 packages in 42s

32 packages are looking for funding
run `npm fund` for details

新建sum.js

1
2
3
4
function sum(a, b) {
return a + b;
}
module.exports = sum;

新建sum.test.js

1
2
3
4
5
const sum = require('./sum');

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

配置脚本package.json

1
2
3
4
5
6
7
8
9
10
11
{
"scripts": {
"test": "jest"
},
"devDependencies": {
"@babel/preset-typescript": "latest",
"@jest/globals": "latest",
"jest": "latest",
"ts-jest": "latest"
}
}

执行命令成功测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
Github-Id-VampireAchao:simple-jest achao$ npm run test

> test
> jest

PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (1 ms)

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.089 s, estimated 1 s
Ran all test suites.

然后我们配置typescript支持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Github-Id-VampireAchao:simple-jest achao$ npm install --save-dev @babel/preset-typescript

added 9 packages, and audited 287 packages in 4s

32 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
Github-Id-VampireAchao:simple-jest achao$ npm install --save-dev ts-jest

added 12 packages, and audited 299 packages in 7s

32 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
Github-Id-VampireAchao:simple-jest achao$ npx ts-jest config:init

Jest configuration written to "/Users/achao/IdeaProjects/simple-jest/jest.config.js".
Github-Id-VampireAchao:simple-jest achao$ npm install --save-dev @jest/globals

up to date, audited 299 packages in 1s

32 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities

配置babel.config.js

1
2
3
4
5
6
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};

以及tsconfig.json

1
2
3
4
5
6
7
8
{
"compilerOptions": {
"module": "commonjs",
},
"include": [
"**/*.ts"
]
}

然后修改代码为sum.ts

1
2
3
4
// sum.ts
export function sum(a: number, b: number): number {
return a + b;
}

以及sum.test.ts

1
2
3
4
5
6
7
8
9
// sum.test.ts
import {describe, expect, test} from '@jest/globals';
import {sum} from './sum';

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

然后我们运行一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Github-Id-VampireAchao:simple-jest achao$ npm run test

> test
> jest

PASS ./sum.test.ts
sum module
✓ adds 1 + 2 to equal 3 (1 ms)

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.582 s, estimated 1 s
Ran all test suites.

然后配置github action,也就是.github/workflows/ci.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
name: Jest CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- name: Install dependencies
run: npm install

- name: Run tests
run: npm run test

测试一下正常的:

init · VampireAchao/simple-jest@91045d4 · GitHub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Run npm run test

> test
> jest

PASS ./sum.test.ts
sum module
✓ adds 1 + 2 to equal 3 (2 ms)

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.361 s
Ran all test suites.

测试一下错误的:

test wrong ci · VampireAchao/simple-jest@337088e · GitHub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Run npm run test

> test
> jest

FAIL ./sum.test.ts
sum module
✓ adds 1 + 2 to equal 3 (2 ms)
wrong module
✕ adds 1 + 1 to equal 3 (2 ms)

● wrong module › adds 1 + 1 to equal 3

expect(received).toBe(expected) // Object.is equality

Expected: 3
Received: 2

11 | describe('wrong module', () => {
12 | test('adds 1 + 1 to equal 3', () => {
> 13 | expect(sum(1, 1)).toBe(3);
| ^
14 | });
15 | });
16 |

at Object.<anonymous> (sum.test.ts:13:27)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 2.172 s
Ran all test suites.
Error: Process completed with exit code 1.

希望我没有写漏,完整代码

https://github.com/VampireAchao/simple-jest/