【问题标题】:Jest - log parameters passed into a function called inside the test when test failedJest - 当测试失败时,将参数记录到测试内部调用的函数中
【发布时间】: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 &gt;= 3 letters, testing: %s'){...
但是这里每次迭代都被认为是一个单独的测试,我在数十个不同的测试中有数百个这样的迭代(全名必须 Full name must be >= 3 letters, testing: 文本。

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

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

不确定它是否重要,但我正在将 ts-jest 用于打字稿。

【问题讨论】:

    标签: javascript logging jestjs ts-jest


    【解决方案1】:

    使用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 | });
    
    

    【讨论】:

    • 太棒了!我使用describe 对与每个业务实体相关的测试进行分组。多亏了你,我意识到我可以使用嵌套的 describe 块来实现我想要的。
    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2017-12-02
    • 2019-09-21
    相关资源
    最近更新 更多