【发布时间】:2021-09-25 19:34:35
【问题描述】:
我有一个 Jest 测试,描述为:
it('Full name must be >= 3 letters', () => {
['', 'a', 'ab', 'a ', 'a ', ' a'].forEach((fullName) => {
expect(() => userBuilder.build({ fullName })).toThrowError();
});
});
当任何迭代失败时,测试失败并指向expect 行:
user › Full name must be >= 3 letters
expect(received).toThrowError()
Received function did not throw
15 | ['', 'a', 'ab', 'a ', 'a ', ' a', 'asdf'].forEach((fullName) => {
16 | console.debug('testing', fullName);
> 17 | expect(() => userBuilder.build({ fullName })).toThrowError();
| ^
18 | });
19 | });
20 |
at src/api/user/user.test.ts:17:53
at Array.forEach (<anonymous>)
at Object.<anonymous> (src/api/user/user.test.ts:15:54)
这不是很有帮助,因为它处于循环中。当expect 失败时,我需要知道在迭代中作为fullName 传递了哪些参数。
来自this SO question,我知道我可以使用类似test.each([...])('Full name must be >= 3 letters, testing: %s'){...
但是这里每次迭代都被认为是一个单独的测试,我在数十个不同的测试中有数百个这样的迭代(全名必须 Full name must be >= 3 letters, testing: 文本。
我知道我可以 console.debug('testing', fullName) 进行每次迭代,但它会记录每次迭代的输出,并在每个输出周围添加一些详细信息,并且它不会打印出失败的消息,因此通过多次测试,很难找出哪个迭代在哪个测试中失败。我不知道如何仅在期望失败时记录输出。
对于测试运行者来说,这似乎是一件很简单的事情,我敢肯定我一定在这里遗漏了一些东西。
不确定它是否重要,但我正在将 ts-jest 用于打字稿。
【问题讨论】:
标签: javascript logging jestjs ts-jest