Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jest对象包含使用toBe而不是toEqual属性检查的方法?_Javascript_Unit Testing_Testing_Jestjs - Fatal编程技术网

Javascript Jest对象包含使用toBe而不是toEqual属性检查的方法?

Javascript Jest对象包含使用toBe而不是toEqual属性检查的方法?,javascript,unit-testing,testing,jestjs,Javascript,Unit Testing,Testing,Jestjs,是否有类似于objectContaining的方法,使用toBe而不是toEqual属性检查 以下人为设计的测试用例(通过)强调了这个问题。我想断言,传递给foo的对象的按钮属性实际上是button1,因此对于类似的对象button2应该失败 test('foo', () => { const button1 = document.createElement('button'); const button2 = document.createElement('button');

是否有类似于
objectContaining
的方法,使用
toBe
而不是
toEqual
属性检查

以下人为设计的测试用例(通过)强调了这个问题。我想断言,传递给
foo
的对象的
按钮
属性实际上是
button1
,因此对于类似的对象
button2
应该失败

test('foo', () => {
  const button1 = document.createElement('button');
  const button2 = document.createElement('button');
  const foo = jest.fn();
  foo({button: button1});
  expect(foo).toBeCalledWith(expect.objectContaining({button: button2}));
});

或者,我如何重写此测试用例以支持我所需的断言?

我认为在测试中比较输出的方式将导致其通过,因为参数实际上被理解为:

{ "button": <button /> }
如果您想使用toBe()的严格相等性检查,我不知道有可比较的对象包含带有Jest的方法,但是您可以询问Jest提供的参数:

expect(foo.mock.calls[0][0].button).toBe(button1);

是的,添加一个属性将允许
toEqual
属性检查失败,但它仍然没有测试所需的标识。您可以使用Jest在
调用
响应中公开的参数数组,允许您使用toBe并获得严格的相等性检查
expect(foo.mock.calls[0][0].button).toBe(button1);