Giter VIP home page Giter VIP logo

mock-css-modules's Introduction

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 ...

mock-css-modules's People

Contributors

bmatcuk avatar

Stargazers

Patrick Gross avatar Leo Furze-Waddock avatar akabeko avatar Khizar Aziz avatar Yury Shevchenko avatar Max Gallo avatar Sergey Khval avatar jmq avatar Yann S. avatar Amit Solanki avatar Shifa Khan avatar Travis Vander Hoop avatar Paolo Moretti avatar Michele Bertoli avatar Lucas Motta avatar Mengdi Chen avatar Chris Shepherd avatar Jason Bacchetta avatar Atif Afzal avatar  avatar David Moody avatar  avatar Anton Petrov avatar Shawn D'Souza avatar Misha Moroshko avatar  avatar  avatar Russell Vea avatar

Watchers

 avatar  avatar

mock-css-modules's Issues

Thanks!

Wanted to do more than just star the repo!

It doesn't work with transpiled imports

Hi, I am using Ava to run my tests.

I don't know what I am doing wrong but this should work:

import mockCSS from 'mock-css-modules';
mockCSS.register(['.scss', '.css']);

import '../some-style.scss';

throws an error

however if I rewrite it using commonjs require

import mockCSS from 'mock-css-modules';
mockCSS.register(['.scss', '.css']);

require('../some-style.scss');

it works.

Babel should transpire all imports into requires. I don't really know why this doesn't work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.