vitest-dev / vitest-dev/vitest

Browser Mode: toHaveStyle() should support partial matching of values

Open
#9,797 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement: pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Clear and concise description of the problem

When asserting the styles of an HTML element in Vitest Browser Mode with toHaveStyle(), the CSS property values can only be matched exactly. When using CSS Modules, certain values like an animation name include a hash and can't therefore be verified directly:

expect.element(screen.getByRole("img")).toHaveStyle({
  animationName: "spin", // doesn't work with CSS modules
});
Suggested solution

toHaveStyle() should either support RegEx or expect.*

expect.element(...).toHaveStyle({ animationName: expect.stringContaining("spin") });
expect.element(...).toHaveStyle({ animationName: /spin/ });
Alternative

It's possible to import the CSS module in the test and compare against the hashed animation name, but I would like to avoid importing CSS file in tests.

Additional context

Discussion on Discord

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Browser Mode implementation and tests for toHaveStyle(), then trace how property values are compared. Add coverage for partial matching with expect.stringContaining("spin") and /spin/, and consider the work done when these cases pass without requiring CSS module imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.