Open zhouzhongyuan opened 7 years ago
.
Matcher | 作用 | 举例 |
---|
Common
.toBe(4)
| 基本数据类型是否相等. use Object.is | expect(2 + 2).toBe(4);
toEqual
| 引用数据类型是否相等.
recursively checks every field of an object or array. | const data = {one: 1};
data['two'] = 2; expect(data).toEqual({one: 1, two: 2});
Truthiness
toBeNull
| only null| const n = null;
expect(n).toBeNull();
toBeUndefined
| matches only undefined | const n = null;
expect(n).toBeUndefined();
toBeDefined
| the opposite of toBeUndefined
toBeTruthy
| matches anything that an if
statement treats as true
toBeFalsy
| matches anything that an if
statement treats as false
Number
toBeGreaterThan
toBeGreaterThanOrEqual
toBeLessThan
toBeLessThanOrEqual
Number(floating point number)
toBeCloseTo
String
toMatch
| 正则匹配 | test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});
Array
toContain
Exception
toThrow
| exact error message OR regexp | .toThrow();
.toThrow(Error)
.toThrow('wrong')
.toThrow(/wrong/')
Shallow rendering comes from react-addons-test-utils or Enzyme
Enzyme and React's TestUtils 具有同样功能
What if there is no test framework?
.
PASS src/js/lib/yes-native/components/DatePicker/__tests__/index-test.js
● Console
console.error node_modules/react-native/Libraries/Core/ExceptionsManager.js:71
Warning: ReactTestUtils has been moved to react-dom/test-utils. Update references to remove this warning.
console.error node_modules/react-native/Libraries/Core/ExceptionsManager.js:71
Warning: Shallow renderer has been moved to react-test-renderer/shallow. Update references to remove this warning.
Fix If you are using React 0.14 or React <15.5, in addition to enzyme, you will have to ensure that you also have the following npm modules installed if they were not already:
npm i --save-dev react-addons-test-utils react-dom
If you are using React >=15.5, in addition to enzyme, you will have to ensure that you also have the following npm modules installed if they were not already:
npm i --save-dev react-test-renderer react-dom
from SO
Test suite failed to run
测试套件无法运行
Statement coverage and Line coverage 是有区别的,目的用途也是不一样的。 反例: one line contains two statments
Jest ~笑话
snapshot文件应当提交到git
一个JavaScript测试框架
test runner: 大约是“测试框架”的意思。例如mocha
assertion library: 断言库。例如chai
Try Jest online
相关工具
测试文章