Skip to content
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

css-modules-require-hook and jest #109

Open
rodoabad opened this issue Jan 11, 2018 · 4 comments
Open

css-modules-require-hook and jest #109

rodoabad opened this issue Jan 11, 2018 · 4 comments

Comments

@rodoabad
Copy link

rodoabad commented Jan 11, 2018

#I have this setup in my jest config.

    moduleNameMapper: {
        '^.+\\.(scss)$': '<rootDir>/test/utils/css-hooks.js'
    },
const hook = require('css-modules-require-hook');
const sass = require('node-sass');

hook({
    extensions: [
        '.scss'
    ],
    preprocessCss: (data, file) => {
        return sass.renderSync({ // eslint-disable-line no-sync
            data,
            file
        }).css;
    }
});

When I use mocha this resolves to _styles_header__styles which is something I can test against.

// mocha.opts
--require ./test/utils/css-hooks.js

However, in jest this returns undefined.

@mightyaleksey
Copy link
Member

mightyaleksey commented Jan 13, 2018

There is a good solution for jest in official docs: http://facebook.github.io/jest/docs/en/webpack.html#mocking-css-modules

It replaces all the css files with Proxy object, so you'll be able to access all the keys without any computations.

{
  "jest": {
    "moduleNameMapper": {
      "\\.scss$": "identity-obj-proxy"
    }
  }
}

@LukeChannings
Copy link

This is only a solution in that it allows tests not to fail.
Some people (like myself) actually want to write tests based on the computed css-modules class names.

Using this actually changes the behaviour of a React component where className is based on props, className will be undefined when mocking CSS, but not when actually importing the CSS.

I have been trying to find a solution to this that actually imports the CSS via CSS Modules. I have a documentation generation script that uses css-modules-require-hook and it works perfectly. Importing and hooking css-modules-require-hook in jest/setup.js or through jest.config.js has not been possible so far.

@pascalduez
Copy link
Contributor

Hi,

I've been using babel-plugin-css-modules-transform to apply the same transform for both build and unit test steps.
Might be a solution.

@pragmaticivan
Copy link

Hi there, any progress with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants