From 8590ca23b3440b2ef2d72ecabdbb2c403075332b Mon Sep 17 00:00:00 2001 From: huxiguo Date: Thu, 9 Nov 2023 22:45:37 +0800 Subject: [PATCH] jest02 --- docs/.vuepress/dist | 2 +- ...25\345\205\203\346\265\213\350\257\225.md" | 232 ++++++++++++++++++ 2 files changed, 233 insertions(+), 1 deletion(-) diff --git a/docs/.vuepress/dist b/docs/.vuepress/dist index d10d495d..96c34956 160000 --- a/docs/.vuepress/dist +++ b/docs/.vuepress/dist @@ -1 +1 @@ -Subproject commit d10d495d3014461fb297006b1161e4dc3d63bff6 +Subproject commit 96c34956837d5656ab28469ecb5da10811c7fcf1 diff --git "a/docs/testing/\345\215\225\345\205\203\346\265\213\350\257\225.md" "b/docs/testing/\345\215\225\345\205\203\346\265\213\350\257\225.md" index 644ee767..3c55da0d 100644 --- "a/docs/testing/\345\215\225\345\205\203\346\265\213\350\257\225.md" +++ "b/docs/testing/\345\215\225\345\205\203\346\265\213\350\257\225.md" @@ -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'}}]], +}; ```