代码之家  ›  专栏  ›  技术社区  ›  Igor Soloydenko

Jest的`it.each()`描述用于在引用$谓词时呈现箭头函数源代码

  •  0
  • Igor Soloydenko  · 技术社区  · 6 年前

    问题定义

    Jest允许在以下情况下使用测试用例的数据 it.each s name 通过 $ -前缀变量。

    下面的代码会得到这样的输出:

     PASS  src/array-functions/find-pairwise.spec.ts
      findPairwise
        √ should return [1, 2] for [1, 2, 3] and [Function anonymous] (7ms)
        √ should return [1, 2] for [1, 2, 3] and [Function anonymous] (1ms)
        √ should return [2, 3] for [1, 2, 3] and [Function anonymous]
        √ should return [2, 3] for [1, 2, 3] and [Function anonymous] (1ms)
        √ should return [undefined, undefined] for [1, 2, 3] and [Function anonymous]
    
    Test Suites: 1 passed, 1 total
    Tests:       5 passed, 5 total
    Snapshots:   0 total
    Time:        4.061s
    Ran all test suites related to changed files.
    

    如您所见 $expected $array 变量以人类友好的形式呈现(在这种情况下是基本的JavaScript数组)。 然而 $predicate 显示通用文本 [Function anonymous] 而不是实际的代码。 我知道JS中的常规函数和箭头函数都可以公开它们的源代码,如果你调用 .toString() 在他们身上。 有没有一种方法可以指示Jest渲染该结果 toString() 打电话? 我确实试过了 $predicate.toString() $(predicate.toString()) 但它们都不起作用。

    enter image description here

    代码

    import { findPairwise } from './find-pairwise';
    
    describe(findPairwise.name, () => {
      it.each`
        array        | predicate                                | expected
        ${[1, 2, 3]} | ${(l: number, r: number) => l === 1}     | ${[1, 2]}
        ${[1, 2, 3]} | ${(l: number, r: number) => r === 2}     | ${[1, 2]}
        ${[1, 2, 3]} | ${(l: number, r: number) => r === 3}     | ${[2, 3]}
        ${[1, 2, 3]} | ${(l: number, r: number) => l + r === 5} | ${[2, 3]}
        ${[1, 2, 3]} | ${(l: number, r: number) => l === r }    | ${[undefined, undefined]}
      `
      ('should return $expected for $array and $predicate', ({ array, predicate, expected }) => {
    //                                         ^^^^^^^^^^
    //                                         ||||||||||
    //                                         I'd like this to be rendered
    //                                         as the code of the arrow function.
    //                                         E.g. "(l: number, r: number) => l === 1"
    //                                         ...or something close to it.
    
        expect(findPairwise(array, predicate)).toEqual(expected);
      });
    });
    
    0 回复  |  直到 6 年前
        1
  •  9
  •   mgarcia    6 年前

    当你使用 tagged template literal 版本为 table 参数 it.each ,jest正在使用 pretty-format 后台库,用于从测试数据生成标题(如果测试数据不是基元类型)。

    不幸的是,为了您的目的 pretty-format 图书馆似乎不使用 toString 从函数中提取方法,以便对其进行格式化。

    作为替代解决方案,您可以使用 array version 对于 桌子 参数:

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

    请注意,我已经更改了参数的顺序,以便将预期值放在首位。这是因为测试数据和标题中的占位符之间的映射是基于数组版本的顺序的 桌子 对于 it.each .