question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically mock css module imports

See original GitHub issue

Clear and concise description of the problem

When using snapshots, any change to a module.(css|scss) file will change the generated class. image This creates a massive amount of noise in commits and pr. This random class name behavior is not desirable in a testing context, where you want predictability.

I would be open to submitting a PR for this issue if that was desired.

Suggested solution

The object imported from a module.(css|scss) file could be mocked to:

new Proxy(new Object(), {
  get(_, style) {
    return style;
  },
});

This means that when a class is accessed, it returns the raw string, reproducably:

import styles from "./Renderer.module.scss";
console.log(styles.underline) // "underline"

Instead of the occasionally randomized variant:

import styles from "./Renderer.module.scss";
console.log(styles.underline) // "_underline_i69w2_32" (until someone modifies Renderer.module.scss)

Alternative

This can be solved in each test file, by just mocking the module.

vi.mock("./Renderer.module.scss", () => ({
  default: new Proxy(new Object(), {
    get(_, style) {
      return style;
    },
  }),
}));

This is perfectly workable, but there is no reason a developer should need to do this - returning the same string every time is expected behavior in my opinion.

Additional context

Diffs where every class had its name changed (but nothing was actually changed) are massive, and can obscure actual changes - they suck. For example, if a pr includes a change to a css file and a modification to the classes of an object, the second change will be hidden behind the noise of the first, reducing the utility of the snapshot as a change detector.

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
sheremet-vacommented, Sep 4, 2022

So, #1803 implements this logic:

  • By default, CSS processing is disabled for every css file
    • module.* exports a proxy in that case to not affect runtime (meaning, styles.{value} will always return a key with filename hash)
  • If processing is enabled (css.include), css is processed and injected into DOM
    • By default, module.* returns pseudo-scoped class names (styles.module always returns _module_${filenameHash}, styles.someRandomValue will return undefined)
    • If you want to enable original scoped names (so production and tests generate the same names), use css.modules.classNameStrategy: 'scoped'
  • Added css.modules.classNameStrategy option:
    • scoped - use css.modules.generateScopeName from user config or fallback to default behaviour, this is how it works in dev and prod
    • non-scoped - returns just the name
    • stable - returns the nama and hash, based on filepath, relative to root
5reactions
EvHauscommented, Oct 11, 2022

In case anyone else runs into this issue in the future – I got stuck here for while because I thought the option was:

export default defineConfig({
    css: {
        modules: {
            classNameStrategy: 'non-scoped'
        }
    }
})

But it’s actually:

export default defineConfig({
    test: {
        css: {
            modules: {
                classNameStrategy: 'non-scoped'
            }
        }
    }
})

The first one has no effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking css modules in jest - Stack Overflow
import styles from '~/shared/styles/Profile.css';. I am trying to mock all css files with identity-obj-proxy as I am using css-modules. It ...
Read more >
mock-css-modules - npm
Start using mock-css-modules in your project by running `npm i ... There are 2 other projects in the npm registry using mock-css-modules.
Read more >
Tim on Twitter: "Next.js has a built-in configuration for Jest now ...
Next.js has a built-in configuration for Jest now! ◇ Auto-mocking of .(s)css/.module.(s)css and image imports ◇ Transform using SWC ◇ Loads .env variants ......
Read more >
Unit test code that uses CSS Modules | by André Eife | Medium
It's a spec that describes a way of importing CSS files into JavaScript. By doing this you gain the possibility to reference your...
Read more >
What are CSS Modules and why do we need them?
import styles from "./styles.css"; element.innerHTML = `<h1 class="${styles.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found