jest 笔记
发布: 2019-06-08 10:28:52标签: 前端开发
安装
npm install jest -g
Matchers(匹配)
- toBe (基本数据类型相等)
01test('two plus two is four', () => {02 expect(2 + 2).toBe(4);03});
复制代码- toEqual (引用数据相等)
01test('Object assign', () => {02 expect(Object.assign({ a: 1 }, { a: 2 })).toEqual({ a: 2 });03});
复制代码- not (取反)
- Truthiness(真假判断)
01test('null', () => {02 const n = null;03 expect(n).toBeNull(); // null04 expect(n).toBeDefined(); // 不是 undefined05 expect(n).not.toBeUndefined(); // 不匹配 undefined06 expect(n).not.toBeTruthy(); // 不是 真07 expect(n).toBeFalsy(); // 假08});0910test('zero', () => {11 const z = 0;12 expect(z).not.toBeNull(); // 不是 null13 expect(z).toBeDefined(); // 不是 undefined14 expect(z).not.toBeUndefined(); // 不是 不是 undefined15 expect(z).not.toBeTruthy(); // 不是 真16 expect(z).toBeFalsy(); // 是假17});
复制代码- 数字比较
01test('two plus two', () => {02 const value = 2 + 2;03 expect(value).toBeGreaterThan(3); // 比3大04 expect(value).toBeGreaterThanOrEqual(3.5); // 大于等于05 expect(value).toBeLessThan(5); // 小于06 expect(value).toBeLessThanOrEqual(4.5); // 小于等于0708 expect(value).toBe(4); // 相等09 expect(value).toEqual(4); // 相等10});
复制代码- 字符串比较
01test('there is no I in team', () => {02 expect('team').not.toMatch(/I/); // 包含03});
复制代码- 数组比较
01const shoppingList = [02 'diapers',03 'kleenex',04 'trash bags',05 'paper towels',06 'beer',07];0809test('the shopping list has beer on it', () => {10 expect(shoppingList).toContain('beer'); // 包含11 expect(new Set(shoppingList)).toContain('beer'); // 包含12});
复制代码- toThrow (指定函数抛出错误)
01function compileAndroidCode() {02 throw new ConfigError('you are using the wrong JDK');03}0405test('compiling android goes as expected', () => {06 expect(compileAndroidCode).toThrow();07 expect(compileAndroidCode).toThrow(ConfigError);0809 // You can also use the exact error message or a regexp10 expect(compileAndroidCode).toThrow('you are using the wrong JDK');11 expect(compileAndroidCode).toThrow(/JDK/);12});
复制代码- 测试异步函数(callback、promise、async)
01function fetchData() {02 return Math.random() > 0.5 ? Promise.resolve(1234) : Promise.reject('error')03}0405test('the data is peanut butter', () => {06 expect.assertions(1) // 只会触发一次断言07 return fetchData()08 .then(data => {09 expect(data).toBeDefined();10 })11 .catch(e => expect(e).toMatch(/err/))12});
复制代码- Setup and Teardown(测试执行顺序)
beforeAll beforeEach test afterEach afterAll
- describe(作用域)
Mock 函数
- Mock Functions
01function forEach(items, callback) {02 for (let index = 0; index < items.length; index++) {03 callback(items[index]);04 }05}0607const mockCallback = jest.fn(x => 42 + x);08forEach([0, 1], mockCallback);0910// mock函数被触发两次11expect(mockCallback.mock.calls.length).toBe(2);1213// 第一次第一个参数是014expect(mockCallback.mock.calls[0][0]).toBe(0);1516// 第二次第一个参数是117expect(mockCallback.mock.calls[1][0]).toBe(1);1819// 第一个次返回的结果4220expect(mockCallback.mock.results[0].value).toBe(42);
复制代码- Mock 的返回值
01const myMock = jest.fn();02console.log(myMock());03// > undefined0405myMock06 .mockReturnValueOnce(10)07 .mockReturnValueOnce('x')08 .mockReturnValue(true);0910console.log(myMock(), myMock(), myMock(), myMock());11// > 10, 'x', true, true
复制代码