软件测试面试刷题,这个小程序(永久刷题),靠它可以快速找到工作!https://blog.youkuaiyun.com/AI_Green/article/details/134931243?spm=1001.2014.3001.5502编辑https://blog.youkuaiyun.com/AI_Green/article/details/134931243?spm=1001.2014.3001.5502编辑https://blog.youkuaiyun.com/AI_Green/article/details/134931243?spm=1001.2014.3001.5502编辑https://blog.youkuaiyun.com/AI_Green/article/details/134931243?spm=1001.2014.3001.5502编辑https://blog.youkuaiyun.com/AI_Green/article/details/134931243?spm=1001.2014.3001.5502编辑https://blog.youkuaiyun.com/AI_Green/article/details/134931243?spm=1001.2014.3001.5502
https://blog.youkuaiyun.com/AI_Green/article/details/134931243?spm=1001.2014.3001.5502
Jest
是一个 JavaScript
集大成的测试库,是我们单元测试的基础
;而 React Testing Library
则提供了一些 React Component
的 Api
,来协助我们进行 React Dom
和事件相关的单测编写
。
本文主要介绍下 jest的常见断言
按类型划分
场景方向 | 断言Api |
---|---|
基础类型的比较 | not toBe(value) toBeTruthy(value) toBeFalsy(value) toBeDefined() toBeUndefined() toBeCloseTo(value) toBeNaN() |
引用类型的比较 | toEqual(value) |
数字符号 | toBeGreaterThan(value) toBeLessThan(value) toBeGreaterThanOrEqual(value) toBeLessThanOrEqual(value) |
正则匹配 | toMatch(value) toMatchObject(value) |
表单验证 | toContain(value) arrayContaining(value) toContainEqual(value) toHaveLength(value) toHaveProperty(value) |
错误抛出 | toThrow() toThrowError() |
基础类型
ps: 前置知识:
test
用于定义单个
的用例,与此类似的还有describe
和it
describe
表示一组
用例,其中可以包含多组test
it
是test
的别名,有相同的作用
JavaScript 中分为基础类型和引用类型,其中基础类型中,大部分比较都可以通过 toBe
来完成,而not
则用来表示非的判断,比如下面的简单例子。
// ./src/__test__/expect.test.ts
import React from "react";
describe("jest 断言", () => {
test("基础类型的比较", () => {
// tobe
expect(1 + 1).toBe(2);
// not
expect(1 + 1).not.toBe(3);
// ...
});
});
不仅是数字,包括 boolean 和 undefined 在内都是可以的。
//