用文字 table 开玩笑 test.each:"Not enough arguments supplied for given headings"

Jest test.each with literal table: "Not enough arguments supplied for given headings"

我正在尝试使用 Jest 24 进行参数化测试

https://archive.jestjs.io/docs/en/24.x/api#2--testeachtablename-fn-timeout-

我的测试看起来像:

  it.each`
    startDate                 | endDate                   | expected
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 21)}  | 1
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 22)}  | 1
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 23)}  | 1
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 24)}  | 2
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 29)}  | 7
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 30)}  | 7
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 31)}  | 8
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 2, 1)}   | 9
  `('weekend days are not counted', ({startDate, endDate, expected}) => {
    ...
  });

但是当我运行它时:

    Not enough arguments supplied for given headings:
    startDate | endDate | expected

    Received:
    Array [
      2022-02-20T00:00:00.000Z,
      2022-02-21T00:00:00.000Z,
      2022-02-20T00:00:00.000Z,
      2022-02-22T00:00:00.000Z,
      2022-02-20T00:00:00.000Z,
      2022-02-23T00:00:00.000Z,
      2022-02-20T00:00:00.000Z,
      2022-02-24T00:00:00.000Z,
      2022-02-20T00:00:00.000Z,
      2022-03-01T00:00:00.000Z,
      2022-02-20T00:00:00.000Z,
      2022-03-02T00:00:00.000Z,
      2022-02-20T00:00:00.000Z,
      2022-03-03T00:00:00.000Z,
      2022-02-20T00:00:00.000Z,
      2022-03-01T00:00:00.000Z,
    ]

    Missing 1 argument

谁能解释一下哪里出了问题?

注意文档说 table 中的值必须是:

One or more subsequent rows of data supplied as template literal expressions using ${value} syntax.

在您的情况下,这意味着:

  it.each`
    startDate                 | endDate                   | expected
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 21)}  | 1
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 22)}  | 1
  `('weekend days are not counted', ({startDate, endDate, expected}) => {
    ...
  });

应该是:

  it.each`
    startDate                 | endDate                   | expected
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 21)}  | 
    ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 22)}  | 
  `('weekend days are not counted', ({startDate, endDate, expected}) => {
    ...
  });

也插入了预期值。这是由于 tagged templates 的工作方式;没有大括号,值 1 最终出现在 strings 数组中,即函数的第一个参数,而不是后续的表达式参数。


您可以通过定义一个简单的函数来更详细地了解正在发生的事情,以显示 it.each 实际作用于什么:

> function reveal(strings, ...args) {
... return JSON.stringify({ strings, args }, null, 2);
... }
undefined
> console.log(reveal`
...     startDate                 | endDate                   | expected
...     ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 21)}  | 1
...     ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 22)}  | 1
...   `)
{
  "strings": [
    "\n    startDate                 | endDate                   | expected\n    ",
    "  | ",
    "  | 1\n    ",
    "  | ",
    "  | 1\n  "
  ],
  "args": [
    "2022-02-20T00:00:00.000Z",
    "2022-02-21T00:00:00.000Z",
    "2022-02-20T00:00:00.000Z",
    "2022-02-22T00:00:00.000Z"
  ]
}
undefined
> console.log(reveal`
...     startDate                 | endDate                   | expected
...     ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 21)}  | 
...     ${new Date(2022, 1, 20)}  | ${new Date(2022, 1, 22)}  | 
...   `)
{
  "strings": [
    "\n    startDate                 | endDate                   | expected\n    ",
    "  | ",
    "  | ",
    "\n    ",
    "  | ",
    "  | ",
    "\n  "
  ],
  "args": [
    "2022-02-20T00:00:00.000Z",
    "2022-02-21T00:00:00.000Z",
    1,
    "2022-02-20T00:00:00.000Z",
    "2022-02-22T00:00:00.000Z",
    1
  ]
}
undefined

通常在这种情况下,我还是倾向于使用 vanilla JS 语言特性:

[
  [new Date(2022, 1, 20), new Date(2022, 1, 21), 1],
  [new Date(2022, 1, 20), new Date(2022, 1, 22), 1]
].forEach(([startDate, endDate, expected]) => {
  it('weekend days are not counted', () => {
    ...
  });
});