导航
导航
文章目录
  1. 起步
  2. Expect

Jest

起步

安装

1
npm install --save-dev jest

pageckage.json配置

1
2
3
4
5
{
"scripts": {
"test": "jest"
}
}

Expect

Expect是一个函数,第一个参数接收value,后面可接链式属性判断

  • expect(value)
    • expect(value).not.toEqual: 与后面等式取反比较
    • toEqual(compareValue): valuecompareValue必须完全相等

自定义校验方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 查询对象key值是否存在对象中
expect.extend({
toContainKeys(received, keys) {
const receivedKeys = Object.keys(received);
const pass = keys.every((val => receivedKeys.includes(val)));
if (pass) {
return {
message: () =>
`expected ${JSON.stringify(received)} have contain ${receivedKeys}`,
pass: true,
};
} else {
return {
message: () =>
`expected ${JSON.stringify(received)} don't have contain ${receivedKeys}`,
pass: false,
};
}
}
});