jest 笔记

发布: 2019-06-08 10:28:52标签: 前端开发

jest官网
jest demo

安装

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(); // null
04 expect(n).toBeDefined(); // 不是 undefined
05 expect(n).not.toBeUndefined(); // 不匹配 undefined
06 expect(n).not.toBeTruthy(); // 不是 真
07 expect(n).toBeFalsy(); // 假
08});
09
10test('zero', () => {
11 const z = 0;
12 expect(z).not.toBeNull(); // 不是 null
13 expect(z).toBeDefined(); // 不是 undefined
14 expect(z).not.toBeUndefined(); // 不是 不是 undefined
15 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); // 小于等于
07
08 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];
08
09test('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}
04
05test('compiling android goes as expected', () => {
06 expect(compileAndroidCode).toThrow();
07 expect(compileAndroidCode).toThrow(ConfigError);
08
09 // You can also use the exact error message or a regexp
10 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}
04
05test('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}
06
07const mockCallback = jest.fn(x => 42 + x);
08forEach([0, 1], mockCallback);
09
10// mock函数被触发两次
11expect(mockCallback.mock.calls.length).toBe(2);
12
13// 第一次第一个参数是0
14expect(mockCallback.mock.calls[0][0]).toBe(0);
15
16// 第二次第一个参数是1
17expect(mockCallback.mock.calls[1][0]).toBe(1);
18
19// 第一个次返回的结果42
20expect(mockCallback.mock.results[0].value).toBe(42);
复制代码
  • Mock 的返回值
01const myMock = jest.fn();
02console.log(myMock());
03// > undefined
04
05myMock
06 .mockReturnValueOnce(10)
07 .mockReturnValueOnce('x')
08 .mockReturnValue(true);
09
10console.log(myMock(), myMock(), myMock(), myMock());
11// > 10, 'x', true, true
复制代码

更多