Skip to content

Commit

Permalink
jest02
Browse files Browse the repository at this point in the history
  • Loading branch information
huxiguo committed Nov 9, 2023
1 parent eb87398 commit 8590ca2
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/.vuepress/dist
Submodule dist updated from d10d49 to 96c349
232 changes: 232 additions & 0 deletions docs/testing/单元测试.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,237 @@ module.exports = {
}

// main.test.js
const { add, sub } = require('./main')

test('5+5=10', () => {
expect(add(5, 5)).toBe(10)
})

test('5-5=0', () => {
expect(sub(5, 5)).toBe(0)
})
```

配置测试命令

```json
// package.json
"scripts": {
"test": "jest"
}
```

初始化`Jest`配置

```shell
pnpm jest init
```

默认配置会生成终端测试覆盖率报告,和`covrage`文件夹里面的前端网页报告

`Jest` 的匹配器

- `toBe()`匹配器,是在工作中最常用的一种匹配器,简单的理解它就是相等。这个相当是等同于===的,也就是我们常说的严格相等

```js
// pass
test('toBe匹配器', () => {
expect('这是一个toBe').toBe('这是一个toBe')
})

// FAIL
test('toBe匹配器完全相等', () => {
const obj = { number: 999 }
expect(obj).toBe({ number: 999 })
})
```

- `toEqual()`匹配器,内容相等,就可以通过测试

```js
// pass
test('toBe匹配器完全相等', () => {
const obj = { number: 999 }
expect(obj).toEqual({ number: 999 })
})
```

当你不严格匹配但要求值相等时时就可以使用`toEqual()`匹配器

- `toBeNul()` 匹配器只匹配`null`值,需要注意的是不匹配`undefined`的值

```js
test('toBeNul 只匹配null', () => {
const a = null
expect(a).toBeNull()
})

// 等价于
test('toBeNul 只匹配null', () => {
const a = null
expect(a).toBe(null)
})
```

- `toBeUndifined()`匹配

```js
test('toBeUndifined', () => {
const a = undefined
expect(a).toBeUndefined()
})
```

- `toBeDefined()` 只要定义过就可以通过

```js
test('toBeDefine', () => {
const a = 1
expect(a).toBeDefined()
})
```

- `toBeTruthy()` 只要不是`false``0``""``null``undefined``NaN`,就可以通过

```js
// pass
test('toBeTruthy', () => {
const a = 1
expect(a).toBeTruthy()
})

// fail
test('toBeTruthy', () => {
const a = 0
expect(a).toBeTruthy()
})
```

- `toBeFalsy()``toBeTruthy()`相对

```js
test('toBeFalsy', () => {
const a = 0
expect(a).toBeFalsy()
})
```

- `toBeGreaterThan(number | bigint)`大于什么数值,只要大于传入的数值,就可以通过测试

```js
test('toBeGreaterThan匹配器', () => {
const a = 10
expect(a).toBeGreaterThan(9.9);
});

test('toBeGreaterThan匹配器', () => {
const a = 10n
expect(a).toBeGreaterThan(9n);
});
```

- `toBeGreaterThanOrEqual(number | bigint)`大于等于什么数值,只要大于等于传入的数值,就可以通过测试

```js
test(`toBeGreaterThanOrEqual()`, () => {
const a = 10
expect(a).toBeGreaterThanOrEqual(10);
})
```

- `toBeLessThan(number | bigint)` 小于什么数值,只要小于传入的数值,就可以通过测试

```js
test(`toBeLessThan()`, () => {
const a = 10
expect(a).toBeLessThan(100);
})
```

- `toBeLessThanOrEqual(number | bigint)` 小于等于什么数值,只要小于等于传入的数值,就可以通过测试

```js
test(`toBeLessThanOrEqual()`, () => {
const a = 10
expect(a).toBeLessThanOrEqual(10);
})
```

- `toBeCloseTo(number, numDigits?)` 这个是可以自动消除JavaScript浮点精度错误的匹配器

```js
// fails
test('toEqual匹配器', () => {
const one = 0.1
const two = 0.2
// expect(one + two).toEqual(0.3)
})

// pass
test('toBeCloseTo', () => {
const one = 0.1
const two = 0.2
expect(one + two).toBeCloseTo(0.3, 5);
})
```

- `toMatch(regexp | string)` 字符串包含匹配器

```js
test('toMatch()', () => {
const a = 'qwertyuiop'
expect(a).toMatch('iop');
})
```

- `toContain(item)` 字符串数组包含匹配器

```js
test('toContain()', () => {
const a = ['a', 'b', 'c', 'd']
expect(a).toContain('c');
})

test('toContain()', () => {
const a = ['a', 'b', 'c', 'd', 'a']
const mySet = new Set(a)
expect(mySet).toContain('c');
})
```

- `toThrow(error?)` 专门对异常进行处理的匹配器,可以检测一个方法会不会抛出异常,可以对这个匹配器中加一些字符串,意思就是抛出的异常必须和字符串相对应,如果字符串不匹配,也没办法通过异常测试

```js
test('toThrow', () => {
const a = () => {
throw new Error('error')
}
expect(() => {
a()
}).toThrow('error');
})
```

- `.not` 取反

```js
test('not', () => {
expect(1).not.toBe(2)
})
```

### 引入`ES6`支持和`import`

`Jest` 默认支持的是 `CommonJS` 规范,使用`babel` 转换成 `CommonJS` 规范

```shell
pnpm add --save-dev babel-jest @babel/core @babel/preset-env
```

可以在工程的根目录下创建一个`babel.config.js`文件用于配置与你当前Node版本兼容的`Babel`

```js
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};
```

0 comments on commit 8590ca2

Please sign in to comment.