Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toHaveCompiledCss() does not support modern at-rules like @starting-style, @container #1791

Open
michaelabrahamian opened this issue Feb 3, 2025 · 0 comments

Comments

@michaelabrahamian
Copy link
Collaborator

Describe the bug

The toHaveCompiledCss() jest matcher from @compiled/jest does not work when there are styles containing modern at-rules, such as @starting-style and @container.

This appears to be due to the css library used to parse the CSS into an AST object: https://github.com/reworkcss/css

Error:

● toHaveCompliedCss › should work with @container

    undefined:1:73: missing '}'

      152 |     }
      153 |
    > 154 |     const ast = CSS.parse(css);
          |                     ^
      155 |     classNames.forEach((c) => {
      156 |       const rules = getRules(ast, matchFilter, c);
      157 |       const search = findStylesInRules(stylesToFind, rules);

Code referenced in error: https://github.com/atlassian-labs/compiled/blob/master/packages/jest/src/matchers.ts#L154

This lines up with some open some in reworkcss/css:

reworkcss/css seems to inactive. It might be worth looking into migrating to @adobe/css-tools instead, which is forked off reworkcss/css: https://github.com/adobe/css-tools

@testing-library/jest-dom already uses it too.

@adobe/css-tools was updated to support these at-rules in the last few years:

To Reproduce
Steps to reproduce the behavior:

Create a simple jest unit test, like below (this file is a good place to quickly add it):

  it.only('should work with @container', () => {
    const { getByText } = render(
      <div
        css={{
          '@media (min-width: 2px)': {
            color: 'blue',
            '@container (width > 400px)': {
              color: 'red',
            },
          },
        }}>
        hello world
      </div>
    );

    const el = getByText('hello world');
    expect(el).toHaveCompiledCss('color', 'blue', { media: '(min-width: 2px)' });
  });

When toHaveCompiledCss runs, the above error will be thrown.

Expected behavior
An error is not thrown when toHaveCompiledCss is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant