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

Usage along with Jest #10

Open
StefanoSega opened this issue Dec 9, 2018 · 3 comments
Open

Usage along with Jest #10

StefanoSega opened this issue Dec 9, 2018 · 3 comments

Comments

@StefanoSega
Copy link

I'm using successfully classnames-loader in my project but actually I've a problem when using it along with Jest, as I'm not able to run the loader on the files to test when I load them, and so the function statement breaks:

const styles = require('./styles.scss');

<div className={styles({
        classA: true,
        classB: true
      })}
      >Hi!</div>

something like this breaks because styles is not a classnames-loader function.

To mock .css files in Jest I used identity-obj-proxy:

{
  "jest": {
    "moduleNameMapper": {
      "\\.(css|less)$": "identity-obj-proxy"
    }
  }
}

this works fine when I use the styles object properties directly because mocks all the css classes into a single object, but of course it fails when is used as a function.

How I can configure Jest to run classnames-loader on the files that I need to test?

@gcdurastanti
Copy link

@StefanoSega Wondering if you ever resolved this? I'm having a similar issue with my project!

@StefanoSega
Copy link
Author

Hi @gcdurastanti

unfortunately no, I gave up and removed classnames-loader, instead I use classnames with es6/typescript this way:

import classNames from 'classnames';

const styles = require('./styles.scss');

<div className={classNames(styles.classA, {
        [styles.classB]: true
      })}
      >Hi!</div>

@gcdurastanti
Copy link

gcdurastanti commented Jan 28, 2019

@StefanoSega, I managed to work around this issue with Jest moduleNameMapper and a style mock.

/* styleMock.js */

import classNames from "classnames";

const ClassnameProxy = new Proxy(classNames, {
  get: function(target, key) {
    return key;
  }
});

export default ClassnameProxy;

/* jest.config.js */

module.exports = {
  setupTestFrameworkScriptFile: require.resolve('./jest.setup.js'),
  moduleNameMapper: {
    '\\.(css|less)$': '<rootDir>/src/util/styleMock.js',
  },
};

Cheers!

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

2 participants