Unit testing 如何测试HTML属性';不要开玩笑;酶?

Unit testing 如何测试HTML属性';不要开玩笑;酶?,unit-testing,testing,jestjs,enzyme,expect,Unit Testing,Testing,Jestjs,Enzyme,Expect,我正在尝试编写一个测试,以确保输出html中不存在特定的属性,但是,我很难找到合适的方法。 我在用玩笑和酶 正在测试的html示例是 <a href="https://material.io/components/buttons" target="test" class="makeStyles-link-37 makeStyles-link-135">Material Design</a> 我不确定第一行是否是

我正在尝试编写一个测试,以确保输出html中不存在特定的属性,但是,我很难找到合适的方法。 我在用玩笑和酶

正在测试的html示例是

<a href="https://material.io/components/buttons" target="test" class="makeStyles-link-37 makeStyles-link-135">Material Design</a>
我不确定第一行是否是找到标签的最有效的方法,但已经证实它是有效的。然而,第二行失败,即使html中不存在rel attr

它失败于

expect(received).not.toHaveProperty(path)
Expected path: not "rel"
Received value: undefined

当我使用toHaveProperty来测试某个属性是否存在时,这很好,但测试该属性是否不存在的适当方法是什么?

我意识到一个可能的答案是使用prop()和toBe() 如果我希望属性是未定义的,那么这就是我在toBe函数中所做的

const linkTag = component.find('a');
expect( linkTag.prop('rel') ).toBe(undefined);
不过,可能会有更好的答案,所以我现在还没有将此答案标记为正确答案。

如果您的测试标题为“attribute“rel”不应该存在,我会在您的测试中遵循相同的说明,例如:

test('attribute "rel" should not exist', () => {
  const linkTag = component.find('a');
  expect(linkTag).not.toHaveAttribute('rel');
});
检查

test('attribute "rel" should not exist', () => {
  const linkTag = component.find('a');
  expect(linkTag).not.toHaveAttribute('rel');
});