Skip to content

toHaveStyle doesn't take account of CSS custom properties #280

Open
@thomaslombart

Description

@thomaslombart
  • @testing-library/jest-dom version: 5.1.1
  • node version: 12.16.2
  • npm (or yarn) version: 1.22.4

Relevant code or config:

test.only('failing test', () => {
  const style = document.createElement('style')
  style.innerHTML = `
    div {
      --color: blue;
    }
  `

  const {container} = render(`
    <div>
      Hello world
    </div>
  `)

  document.body.appendChild(style)
  document.body.appendChild(container)

  expect(container).toHaveStyle(`--color: blue`)
})

Problem description:

If you were to run the test above, this would fail because toHaveStyle doesn't take account of CSS custom properties.

Suggested solution:

I think the PR #276 caused this behavior. Indeed, the isSubset function has been changed and doesn't use getPropertyValue anymore (see this line).

It seems a custom property on CSSStyleDeclaration can't be accessed simply using the property key (for example computedStyle['--color']), you explicitely have to use getPropertyValue.

I forked the repo and changed back the isSubset function to use getPropertyValue and things work well. That may be a solution (I assume there's a reason why it's been changed so things may not be so simple).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions