Sub topic 14
Testing components that import CSS Modules
This chapter assumes you know React components, Jest basics, and how a .module.css file is imported. Jest runs in Node. Node cannot parse CSS, so a test that imports a component with a CSS Module fails unless you tell Jest what to do with that file.
The problem
Without configuration, Jest tries to read Button.module.css as JavaScript and throws a syntax error. Jest does not run your bundler, so the build step that turns CSS into a class map never happens.
Mapping CSS Modules to a stand-in
The usual fix is identity-obj-proxy. It is a package that returns an object whose keys equal their values. styles.button becomes the string "button", and styles.primary becomes "primary".
Install it as a dev dependency:
npm install --save-dev identity-obj-proxyThen map module CSS files to it in the Jest configuration:
// jest.config.js
module.exports = {
testEnvironment: 'jsdom',
moduleNameMapper: {
'\\.module\\.css$': 'identity-obj-proxy',
'\\.css$': '<rootDir>/test/styleMock.js',
},
};The first pattern handles CSS Modules. The second handles plain CSS imports, such as a global stylesheet, which only need an empty module:
// test/styleMock.js
module.exports = {};Order matters. Jest uses the first matching pattern, so the .module.css rule must come before the general .css rule.
Writing a test
With the proxy in place, class names are predictable. This test uses React Testing Library and @testing-library/jest-dom for the matcher.
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { Button } from './Button';
test('applies the variant class', () => {
render(<Button variant="secondary">Save</Button>);
expect(screen.getByRole('button', { name: 'Save' })).toHaveClass('button', 'secondary');
});
test('adds the disabled class and attribute', () => {
render(<Button disabled>Save</Button>);
const button = screen.getByRole('button', { name: 'Save' });
expect(button).toHaveClass('disabled');
expect(button).toBeDisabled();
});These tests use the Button from the earlier chapter that combines classes with clsx.
What to test and what to skip
The proxy confirms that your component picks the right class keys. It does not confirm that the CSS looks right, because no real styles are loaded. Keep these limits in mind:
- Test behavior first: text, roles, and attributes such as
disabled. - Assert on class names only where the class is the behavior, for example a variant or a state.
- Do not assert on generated names such as
Button_primary__x7Yz2. They change between builds. - Use visual or browser-based tests for real layout and color checks.
Common errors
SyntaxError: Unexpected token '.'means the CSS file was not mapped. Check that themoduleNameMapperregex matches your file name.toHaveClassfails with an undefined class means the key does not exist in your component. With the proxy, a typo such asstyles.primrygives the string"primry"rather thanundefined, so a test can pass even though the real build would produce no class. Types from the previous chapter help catch this.- Vitest users do not need a mapper. Vitest processes CSS Modules itself, and by default gives you the generated names. Set
css.modules.classNameStrategy: 'non-scoped'in the Vitest config if you want plain names.
Exercise
A test asserts toHaveClass('primary') and passes under Jest with identity-obj-proxy. The .primary rule was deleted from Button.module.css. Why does the test still pass, and what could catch the problem?
Show answer
The proxy returns the key as a string for any property, so styles.primary is always "primary" whether or not the CSS defines it. Typed CSS Module declarations that list the real class names, or a browser or visual test that loads the real stylesheet, would catch the missing rule.