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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-css-modules

v2.0.0

Published

Mock CSS Modules for testing.

Downloads

34,343

Readme

Release Build Status codecov.io

mock-css-modules

Webpack loaders are great. With them, you can require() just about any file and the loaders will take care of transpiling into javascript.

CSS Modules are great because you can write CSS for each of your components without worrying about rules from one stepping on the rules of another. The aforementioned webpack loaders (the css-loader in particular) will let you require() your CSS and return a nice map of original class names to generated CSS Module class names so you can do something like:

import styles from './styles.css';
import {render} from 'react-dom';

render(<h1 class={styles.myClass}>Hello, World!</h1>, document.body);

The problem is testing... your testing toolchain (mocha perhaps) doesn't know how to require CSS files. This inevitably leads to a syntax error while node tries to parse the CSS as if it was javascript.

How Can We Fix This?

There are several solutions to this problem. The most common solutions either attempt to parse the CSS faithfully or attempt to ignore the CSS require() altogether.

In the first case, you're just complicating things and wasting time. In my automated tests, I don't need to know that myClass is going to become _23_aKvs-b8bW2Vg3fwHozO, so why should I waste the time to parse the CSS to find that out? Further, if there's an error in my CSS, the parsing will fail and cause my component test to fail... is that where the failure belongs? I dunno... maybe, maybe not...

In the second case, all of my class names become empty strings in my automated tests. While it's true that I don't need to know exactly what my class names will become after transpiling, I might want to be able to test that there is some class and I can't do that if just make my CSS require()s return null.

mock-css-modules' solution

mock-css-modules' solution is somewhere between the former and the latter. mock-css-modules registers a handler for requiring CSS files. When node comes upon a require() for a CSS file, it will run mock-css-modules' handler which will return a Proxy object. This Proxy object will trap getters and return the name of the requested property as a string. So, for example:

import styles from './styles.css';

styles.myClass
=> "myClass"

styles.anotherClass
=> "anotherClass"

etc ...

This gives all of our classes names without the overhead of parsing the actual CSS files. And since code that is using CSS Modules shouldn't be making any assumptions about the names of the generated classes, these values are just as valid as the real ones so they shouldn't cause any issues.

Installation

Install with npm:

npm install --save-dev mock-css-modules

Usage

Simply require("mock-css-modules") before any CSS files and you'll be rockin'. By default, mock-css-modules will handle require()d .css files. If your project has some other extensions (such as .sass, .scss, etc), you'll need to register handlers for those, too:

var mockCssModules = require("mock-css-modules");

mockCssModules.register(['.sass', '.scss', ...]);

Unfortunately, this means that if you are taking advantage of webpack's resolvers to require() files without extensions, it won't work. You should use extensions for your CSS files.

Mocha

If you are using mocha to run your tests, you can use mock-css-modules from the command line:

mocha --require mock-css-modules ...

If you need to handle additional extensions, copy the two lines above into a file called test-setup.js, for example, and require the file instead of mock-css-modules directly:

mocha --require test-setup.js ...