-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Automatically mock css module imports #1512
Comments
We have a css property on test config. We might expose this functionality as You can also use css option on Vite config to configure how the names are created. |
Thanks for the prompt reply! The Vite config for how the names are created isn't useful because this behavior is desirable in production - the hashes are how collisions are prevented. I would argue that keeping class names should be the default - I can't think of a scenario where mangling the class names is desirable. If you are taking snapshots, it is definitely problematic. If you are matching elements with className, it is problematic to mangle the names unless you import the styles object, in which case you would be unaffected by this change. |
You can apply it conditionally (using mode or process.env.VITEST). This is just another workaround. I like Mangling can be useful if you want to limit access by class name (testing-library prohibits access by className), so this is why there should be a way to allow it. Mangling can be disabled by default, ofc. |
Although I am not sure, if we should use |
I would disagree that mangling allows you to limit access by className. If mangling is on for a test, I simply import the CSS module in the test to access the mangled name for matching. On the aspect of proxy vs generate scoped name - I totally agree that generating a scoped name is better, it would be bad if there was different behavior in unit testing in that regard - undefined classes should return undefined. This could also be achieved with a proxy that tests if the mocked object has the key, although I doubt there is a good reason to do such. |
How would I be able to do this using mode or process.env.VITEST? |
Something like: export default defineConfig((mode) => {
return {
css: mode !== 'test' ? {} : {
generateScopedName: (name) => name
}
}
}) |
For anyone else that wants to use @sheremet-va's solution above, wanted to point out that export default defineConfig(({ mode }) => ({
css: {
modules:
mode !== 'test'
? {}
: {
generateScopedName: name => name,
},
},
})); |
So, #1803 implements this logic:
|
Just wanted to say thanks @sheremet-va ! This new option is exactly what we needed. 🎉 |
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. |
Clear and concise description of the problem
When using snapshots, any change to a
![image](https://user-images.githubusercontent.com/72410860/174535910-683d6afb-51a3-4d4d-9fed-0c78293865c6.png)
module.(css|scss)
file will change the generated class.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:This means that when a class is accessed, it returns the raw string, reproducably:
Instead of the occasionally randomized variant:
Alternative
This can be solved in each test file, by just mocking the module.
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
The text was updated successfully, but these errors were encountered: