npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mattwca/css-module-snapshots

v1.0.1

Published

Adds support for testing when using CSS Modules in Jest, including snapshots and style matching.

Readme

@mattwca/css-module-snapshots

A comprehensive Jest plugin for CSS Modules snapshot testing. This package provides a transformer, serializer, and a custom matcher to make testing CSS Modules easier and more reliable.

Features

  • CSS Module Transformer: Transforms CSS/SCSS files during Jest tests.
  • Snapshot Serializer: Automatically includes CSS module styles in your testing snapshots.
  • Custom Matchers: Adds a expect(...).toHaveCssStyle matcher, that allows you to verify a style rule is applied to an element.

Installation

npm install --save-dev @mattwca/css-module-snapshots

Usage

1. CSS Module Transformer (required)

Add the transformer to your Jest configuration to handle CSS/SCSS imports:

jest.config.js:

module.exports = {
  transform: {
    '\\.(css|scss)$': '@mattwca/css-module-snapshots/transformer',
  },
};

The transformer:

  • Compiles SCSS files using Sass
  • Processes CSS with PostCSS and postcss-modules
  • Injects styles into the DOM for testing

2. Snapshot Serializer and Custom Matcher

Add the serializer and toHaveCssStyle matcher to your Jest configuration:

In your jest config:

jest.config.js:

module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    '\\.(css|scss)$': '@mattwca/css-module-snapshots/transformer'
  },
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
};

In your jest setup:

jest.setup.js:

require('@mattwca/css-module-snapshots');

The serializer will automatically:

  • Find all CSS module styles in your rendered components
  • Include them in your snapshot output

The toHaveCssStyle matcher:

  • Allows you to verify that a given element has a set of expected style properties to be applied.

Complete Jest Configuration Example

jest.config.js:

module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    '\\.(css|scss)$': '@mattwca/css-module-snapshots/transformer'
  },
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
};

jest.setup.js:

require('@mattwca/css-module-snapshots');

Example Test

Button.module.css:

.button {
  background-color: blue;
}

Button.jsx:

import styles from './Button.module.css';

export const Button = ({children)) => {
  return (<button className={styles.button}>{children}</button>);
};

Button.test.jsx:

import React from 'react';
import { render } from '@testing-library/react';
import styles from './Button.module.scss';
import Button from './Button';

describe('Button', () => {
  it('renders with correct styles', () => {
    const { getByRole, asFragment } = render(<Button>Click me</Button>);
    
    // The snapshot will automatically include the CSS module styles
    expect(asFragment()).toMatchSnapshot();

    expect(getByRole('button')).toHaveCssStyle({ backgroundColor: 'blue' });
  });
});

How It Works

  1. Transform Phase: When Jest encounters a CSS/SCSS import, the transformer compiles it and generates a JavaScript module that:

    • Injects the styles into the document head
    • Exports the CSS module class name mappings
  2. Render Phase: Your components use the exported class names as usual

  3. Snapshot Phase: The serializer finds all injected CSS module styles and includes them in the snapshot output

  4. Matcher: The matcher will find all CSS module style definitions, parse them, and locate all rules which apply to the given element. The declarations within the matching rules are checked to see whether they contain all of the expected style rules.

Dependencies

This package requires:

  • Jest >= 27.0.0
  • Node.js >= 14.0.0

Peer dependencies are automatically installed:

  • sass: For SCSS compilation
  • postcss & postcss-modules: For CSS module processing
  • cross-spawn: For synchronous PostCSS execution

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Repository

https://github.com/mattwca/css-module-snapshots