Igo*_*nko 15 javascript testing unit-testing typescript jestjs
Jest 允许通过it.each前缀变量在测试用例的数据中使用。name$
下面的代码产生如下输出:
\n\n PASS src/array-functions/find-pairwise.spec.ts\n findPairwise\n \xe2\x88\x9a should return [1, 2] for [1, 2, 3] and [Function anonymous] (7ms)\n \xe2\x88\x9a should return [1, 2] for [1, 2, 3] and [Function anonymous] (1ms)\n \xe2\x88\x9a should return [2, 3] for [1, 2, 3] and [Function anonymous]\n \xe2\x88\x9a should return [2, 3] for [1, 2, 3] and [Function anonymous] (1ms)\n \xe2\x88\x9a should return [undefined, undefined] for [1, 2, 3] and [Function anonymous]\n\nTest Suites: 1 passed, 1 total\nTests: 5 passed, 5 total\nSnapshots: 0 total\nTime: 4.061s\nRan all test suites related to changed files.\nRun Code Online (Sandbox Code Playgroud)\n\n正如您所看到的,$expected和$array变量以人类友好的形式呈现(在本例中是基本的 JavaScript 数组)。\n但是,显示的是$predicate通用文本[Function anonymous]而不是实际代码。\n我知道常规函数和如果你调用 JS 中的箭头函数,它们会暴露它们的源代码.toString()。\n有没有办法指示 Jest 渲染该toString()调用的结果?\n我确实尝试过$predicate.toString(),$(predicate.toString())但它们都不起作用。
PASS src/array-functions/find-pairwise.spec.ts\n findPairwise\n \xe2\x88\x9a should return [1, 2] for [1, 2, 3] and [Function anonymous] (7ms)\n \xe2\x88\x9a should return [1, 2] for [1, 2, 3] and [Function anonymous] (1ms)\n \xe2\x88\x9a should return [2, 3] for [1, 2, 3] and [Function anonymous]\n \xe2\x88\x9a should return [2, 3] for [1, 2, 3] and [Function anonymous] (1ms)\n \xe2\x88\x9a should return [undefined, undefined] for [1, 2, 3] and [Function anonymous]\n\nTest Suites: 1 passed, 1 total\nTests: 5 passed, 5 total\nSnapshots: 0 total\nTime: 4.061s\nRan all test suites related to changed files.\nRun Code Online (Sandbox Code Playgroud)\n
mga*_*cia 23
当您使用 的参数的标记模板文字版本时,jest正在幕后使用漂亮格式库从测试数据生成标题(如果测试数据不是原始类型)。tableit.each
不幸的是,出于您的目的,该pretty-format库似乎没有使用toString函数中的方法来格式化它们。
作为替代解决方案,您可以使用参数的数组版本table:
it.each([
[ [1, 2], [1, 2, 3], (l, r) => l === 1 ],
[ [1, 2], [1, 2, 3], (l, r) => r === 2 ],
[ [2, 3], [1, 2, 3], (l, r) => r === 3 ],
[ [2, 3], [1, 2, 3], (l, r) => l + r === 5 ],
[ [undefined, undefined], [1, 2, 3], (l, r) => l === r ],
])
('should return %p for %p and %s', (expected, array, predicate) => {
expect(findPairwise(array, predicate)).toEqual(expected);
});
Run Code Online (Sandbox Code Playgroud)
请注意,我更改了参数的顺序,以便预期值位于第一位。这是因为测试数据和标题中的占位符之间的映射基于tablefor数组版本中的顺序it.each。
| 归档时间: |
|
| 查看次数: |
54045 次 |
| 最近记录: |