javascript - Jest - 当测试失败时记录传递到测试内部调用的函数的参数

标签 javascript logging jestjs ts-jest

我有一个 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([...])('全名必须 >= 3 个字母,测试:%s'){...
但这里每次迭代都被认为是一个单独的测试,并且我在数十个不同的测试中进行了数百次这样的迭代(全名必须<= 50个字符,全名必须仅包含字母和空格等)。这不必要地使我的测试套件臃肿,一遍又一遍地打印重复的全名必须> = 3个字母,测试:文本。

我知道我可以为每次迭代console.debug('testing', fullName),但它会记录每次迭代的输出,并在每个输出周围添加一些详细信息,并且它不会打印失败的消息,因此通过多次测试,很难找出哪个迭代因哪个测试而失败。我不知道如何仅在期望失败时记录输出。

对于测试运行者来说,这似乎是一件很简单的事情,我确信我一定在这里遗漏了一些东西。

不确定它是否重要,但我正在使用 ts-jest 作为 typescript 。

最佳答案

使用describe对验证规则进行分组,然后使用 test.each或 forEach 进行测试。

describe('Full name must be >= 3 letters', () => {
  ['', 'a', 'ab', 'a ', 'a    ', '     a'].forEach((fullName) => {
    it(`should throw error when full name is "${fullName}"`, () => {
      expect(() => userBuilder.build({ fullName })).toThrowError();
    });
  });
});

然后,失败消息将如下所示:

  Full name must be >= 3 letters
    ✓ should throw error when full name is "" (9 ms)
    ✓ should throw error when full name is "a" (1 ms)
    ✕ should throw error when full name is "ab" (1 ms)
    ✓ should throw error when full name is "a " (1 ms)
    ✓ should throw error when full name is "a    "
    ✓ should throw error when full name is "     a"

  ● Full name must be >= 3 letters › should throw error when full name is "ab"

    expect(received).toThrowError()

    Received function did not throw

      10 |   ['', 'a', 'ab', 'a ', 'a    ', '     a'].forEach((fullName) => {
      11 |     it(`should throw error when full name is "${fullName}"`, () => {
    > 12 |       expect(() => userBuilder.build({ fullName })).toThrowError();
         |                                                     ^
      13 |     });
      14 |   });
      15 | });

关于javascript - Jest - 当测试失败时记录传递到测试内部调用的函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69292440/

相关文章:

PHP 5.3.9 log_errors 不工作,不在 Debian Lenny 的日志文件中保存错误

python - 机器人框架和 Python 日志记录 : Logging from multiple threads and logging channel names

jestjs - .env for jest 和 nest 不一样

javascript - Node JS |类型错误 : Cannot read property 'first_name' of undefined

c++ - 分析 "invalid arc tag"(0x00000000)

javascript - vanilla javascript 中的按钮闪烁效果

javascript - Jest 模拟类实例函数

javascript - '命令未找到 : jest'

javascript - 无法获取XML中的节点值,如何获取?

javascript - 如何在 React 中将用户输入限制为十六进制值?