javascript 在对象数组上使用 Jest 属性匹配器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51619100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:37:27  来源:igfitidea点击:

Using Jest property matchers on arrays of objects

javascripttestingjestjs

提问by giodamelio

I am attempting to use Jest's new Property Matcherfeature (since Jest 23.0.0) to match on an array of objects that contain a generated field. I have tried putting both a plain object and a matcher definition using expect.arrayContainingand expect.objectContaininglike I might when matching manually. Is there any way to do this currently?

我正在尝试使用 Jest 的新属性匹配器功能(自Jest 23.0.0 起)来匹配包含生成字段的对象数组。我已经尝试将一个普通对象和一个匹配器定义同时使用expect.arrayContainingexpect.objectContaining就像我在手动匹配时所做的那样。目前有没有办法做到这一点?

const sportsBallPeople = [
  {
    createdAt: new Date(),
    name: 'That one famous guy from Cleveland'
  },
  {
    createdAt: new Date(),
    name: 'That tall guy'
  }
];
expect(sportsBallPeople).toMatchSnapshot(<something goes here>);

回答by Brian Adams

Version Info

版本信息

As is noted in the question, property matchers were introduced in Jest 23.0.0. Note that apps bootstrapped with create-react-app as of today (Aug 5, 2018) are still < 23.

正如问题中所指出的,在 Jest 23.0.0 中引入了属性匹配器。请注意,截至今天(2018 年 8 月 5 日),使用 create-react-app 引导的应用程序仍然小于 23。

OBJECT

目的

Here is an example using a property matcher for a single object:

这是对单个对象使用属性匹配器的示例:

test('sportsBallPerson', () => {
  expect(sportsBallPeople[0]).toMatchSnapshot({
    createdAt: expect.any(Date)
  })
});

The snapshot generated:

生成的快照:

exports[`sportsBallPerson 1`] = `
Object {
  "createdAt": Any<Date>,
  "name": "That one famous guy from Cleveland",
}
`;

This will correctly match createdAt to any date and the name to exactly "That one famous guy from Cleveland".

这将正确地将 createdAt 与任何日期匹配,并将名称与“来自克利夫兰的那个名人”准确匹配。

ARRAY

大批

To test an array of objects using property matchers use forEach to loop over the array and snapshot test each object individually:

要使用属性匹配器测试对象数组,请使用 forEach 循环遍历数组并分别快照测试每个对象:

test('sportsBallPeople', () => {
  sportsBallPeople.forEach((sportsBallPerson) => {
    expect(sportsBallPerson).toMatchSnapshot({
      createdAt: expect.any(Date)
    });
  });
});

The snapshots generated:

生成的快照:

exports[`sportsBallPeople 1`] = `
Object {
  "createdAt": Any<Date>,
  "name": "That one famous guy from Cleveland",
}
`;

exports[`sportsBallPeople 2`] = `
Object {
  "createdAt": Any<Date>,
  "name": "That tall guy",
}
`;

forEachensures that the objects are tested in order, and each object is properly snapshot tested as described above.

forEach确保按顺序测试对象,并且如上所述对每个对象进行适当的快照测试。

Additional Info

附加信息

It is interesting to note that directly testing an array using property matchers does not work properly and has unexpected side-effects.

有趣的是,使用属性匹配器直接测试数组不能正常工作,并且会产生意想不到的副作用。

My first attempt to test an array was to create the following test:

我第一次尝试测试数组是创建以下测试:

test('sportsBallPeople as array', () => {
  expect(sportsBallPeople).toMatchSnapshot([
    { createdAt: expect.any(Date) },
    { createdAt: expect.any(Date) }
  ]);
});

It generated the following snapshot:

它生成了以下快照:

exports[`sportsBallPeople as array 1`] = `
Array [
  Object {
    "createdAt": Any<Date>,
  },
  Object {
    "createdAt": Any<Date>,
  },
]
`;

This is incorrect since the name properties are missing, but the test still passes (Jest v23.4.2). The test passes even if the names are changed and additional properties are added.

这是不正确的,因为缺少 name 属性,但测试仍然通过 (Jest v23.4.2)。即使更改了名称并添加了其他属性,测试也会通过。

Even more interesting was that as soon as this test executed, any following tests using property matchers were adversely affected. For example, placing this test ahead of the the test looping over the objects changed those snapshots to the following:

更有趣的是,一旦执行此测试,任何使用属性匹配器的后续测试都会受到不利影响。例如,将此测试置于循环对象的测试之前将这些快照更改为以下内容:

exports[`sportsBallPeople 1`] = `
Object {
  "createdAt": Any<Date>,
}
`;

exports[`sportsBallPeople 2`] = `
Object {
  "createdAt": Any<Date>,
}
`;

In summary, directly passing an array to use with property matchers does not work and can negatively affect other snapshot tests using property matchers.

总之,直接传递数组以与属性匹配器一起使用是行不通的,并且会对使用属性匹配器的其他快照测试产生负面影响。