安装
1 | npm install jest -g |
Matchers(匹配)
- toBe (基本数据类型相等)
1 | test('two plus two is four', () => { |
- toEqual (引用数据相等)
1 | test('Object assign', () => { |
- not (取反)
- Truthiness(真假判断)
1 | test('null', () => { |
- 数字比较
1 | test('two plus two', () => { |
- 字符串比较
1 | test('there is no I in team', () => { |
- 数组比较
1 | const shoppingList = [ |
- toThrow (指定函数抛出错误)
1 | function compileAndroidCode() { |
- 测试异步函数(callback、promise、async)
1 | function fetchData() { |
Setup and Teardown(测试执行顺序)
1
2
3
4
5beforeAll
beforeEach
test
afterEach
afterAlldescribe(作用域)
Mock 函数
- Mock Functions
1 | function forEach(items, callback) { |
- Mock 的返回值
1 | const myMock = jest.fn(); |