Skip to content

toHaveStyle does not compare empty values #272

Closed
@just-boris

Description

@just-boris
  • @testing-library/jest-dom version: 5.11.0
  • node version: 12.17.0
  • npm (or yarn) version: 6.14.4

Relevant code or config:

test('div max-width', () => {
  const div = document.createElement('div');
  div.style.maxWidth = '300px';
  div.style.minWidth = '200px';
  expect(div).toHaveStyle({ minWidth: '200px', maxWidth: '' }); // passes, but it should not
});

I expect that this assertion will fail, because maxWidth is not empty.

It works, when I am using toEqual instead of toHaveStyle

expect(div.style).toEqual(expect.objectContaining({ minWidth: '200px', maxWidth: '' }));

Suggested solution:

Somehow this information about empty values is lost around these lines:

const cssToParse = getCSStoParse(htmlElement.ownerDocument, css)
const parsedCSS = parseCSS(cssToParse, toHaveStyle, this)

A potential fix would be to check that all keys are from the expectation are defined on the element style definition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions