react单元测试--Jest

2019-03-29  本文已影响0人  Mr君

关于安装

关于使用

  1. 命名
  1. 单元测试代码组织
    单元测试代码的最小单位就是测试用例(test case),每个测试用例考验的是被测试对象在某些特定场景下是否有正确的行为。
每个测试用例一个it函数代表

参数:

// it代表被测试对象,第一个参数描述它的预期行为
it('should return object when invoked', () = > {
  // 增加断言语句
})
describe

为了测试被测试对象在多种情况下的行为,需要创建多个单元测试用例,这里需要用测试套件(test suite)构建,用来组织多个it函数实例。
测试套件可以嵌套使用:一个测试套件=测试用例+测试套件
测试套件和测试用例形成一个属性组织结构,执行顺序从上到下,从里到外

describe('actions', () => {
  it('should return object when invoked', () => {
  })
  // 可以有更多it函数调用
})

多个it放到一个describe中主要目的是为了重置共同环境设置。
describe中包含如下函数帮助重用代码:

配置

  1. package.json中添加:
{
  "scripts": {
    "test": "jest --colors --coverage"
  }
}

--coverage 可以生成测试覆盖率报告
--colors 根据覆盖率生成不同颜色的报告(<50%红色,50%~80%黄色, ≥80%绿色)
执行npm run test命令可在终端运行查看测试运行结果。

  1. package.json中添加:
"jest": {
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleDirectories": [
      "node_modules"
    ],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__jest__/__mocks__/fileMock.js",
      "\\.(css|scss)$": "identity-obj-proxy",
      "^cpn(.*)$": "<rootDir>/src/components$1"
    }
  }

moduleFileExtensions对应webpack中的extensions;
moduleDirectories对应webpack中的modulesDirectories;
moduleNameMapper对应webpack中的alias。这里的<rootDir>对应我们项目的根目录。

语法规则

  1. 普通匹配器
test('two plus two is four', () => {
  expect(2 + 2).toBe(4);
});
test('object assignment', () => {
  const data = {one: 1};
  data['two'] = 2;
  expect(data).toEqual({one: 1, two: 2});
});
test('adding positive numbers is not zero', () => {
  for (let a = 1; a < 10; a++) {
    for (let b = 1; b < 10; b++) {
      expect(a + b).not.toBe(0);
    }
  }
});
  1. Truthiness
    在测试中,你有时需要区分 undefined、 null,和 false,但有时你又不需要区分。 Jest 让你明确你想要什么。
test(name, () => {
  const n = null;
  expect(n).toBeNull();
  expect(n).toBeDefined();
  expect(n).not.toBeUndefined();
  expect(n).not.toBeTruthy();
  expect(n).toBeFalsy();
});
  1. 数字
test('two plus two', () => {
  const value = 2 + 2;
  expect(value).toBeGreaterThan(3);
  expect(value).toBeGreaterThanOrEqual(3.5);
  expect(value).toBeLessThan(5);
  expect(value).toBeLessThanOrEqual(4.5);

  // toBe and toEqual are equivalent for numbers
  expect(value).toBe(4);
  expect(value).toEqual(4);
});
test('两个浮点数字相加', () => {
  const value = 0.1 + 0.2;
  //expect(value).toBe(0.3);           这句会报错,因为浮点数有舍入误差
  expect(value).toBeCloseTo(0.3); // 这句可以运行
});
  1. 字符串
test('there is no I in team', () => {
  expect('team').not.toMatch(/I/);
});

test('but there is a "stop" in Christoph', () => {
  expect('Christoph').toMatch(/stop/);
});
  1. 数组
const shoppingList = [
  'diapers',
  'kleenex',
  'trash bags',
  'paper towels',
  'beer',
];

test('the shopping list has beer on it', () => {
  expect(shoppingList).toContain('beer');
  expect(new Set(shoppingList)).toContain('beer');
});

例子

  1. action 构造函数测试
    步骤:
    1. 预设参数
    2. 调用纯函数
    3. 用expect验证纯函数返回结果


    action构造函数测试.png
测试结果.png
上一篇 下一篇

猜你喜欢

热点阅读