Skip to content

Unable to get css classes injected in element with scss and css modules. #235

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

Closed
alveshelio opened this issue Dec 6, 2018 · 2 comments
Closed

Comments

@alveshelio
Copy link

alveshelio commented Dec 6, 2018

  • react-testing-library version: 5.3.1
  • react version: 16.6.3
  • node version: 10.4.1
  • npm version: 6.4.1

Reproduction:

Here's the Codesandbox: https://codesandbox.io/s/xvw9r8z24z
In this codesandbox I can run my tests
However, in this codesandbox I have the project setup: https://codesandbox.io/s/r15r76o8y4
I can not run the project because of missing dependencies.

Problem description:

I had created another issue regarding not seeing classes in components that I was testing and I thought it was a problem how I was developing my components in storybook. However, I've tested a component in our project and I still have the same problem.

The component displays just fine with the classes when I run the project, however when I try to test my components and check if they have a class, the test fails.

I've tried to rename the file from .scss to css and instead of having className={s.active}, I'd have className="active" and the test would pass because the class was injected into the dive element.

I've been struggling with this, I don't know if it has to do with postcss or how scss files should be handled. I'm not getting any error when I run the text other than why the test is not passing.

In jest.config.js I've tried with the following settings:
"moduleNameMapper": {
".+\.(css|styl|less|sass|scss|svg)$": "/node_modules/jest-css-modules-transform",
},

but the result is the same.

@kentcdodds, I'm sorry to be a pain but do you think you could check my codesandbox and see if I'm doing something wrong the way I'm configuring jest with scss?

@alveshelio alveshelio changed the title Unable to get css classes injected in element with scss and module css. Unable to get css classes injected in element with scss and css modules. Dec 6, 2018
@alexkrolick
Copy link
Collaborator

alexkrolick commented Dec 8, 2018

Jest config does not work in codesandbox, but here's what I think the problem is:

This is your style-mock.js

module.exports = {}

This means all style imports are replaced with an empty object.

Instead of this mock, you can point to a module called identity-obj-proxy that maps s.xyz to "xyz":

// in jest.config.js
moduleNameMapper: {
    ".+\\.(css|scss)$": "identity-obj-proxy"
  },

It looks like this module is already in package.json.

@alexkrolick
Copy link
Collaborator

Also, when I download the project locally it appears that react-scripts ts is not using all the options in your jest.config.js. It looks like only a few properties are supported for overrides: https://facebook.github.io/create-react-app/docs/running-tests#configuration

To get around this you may need to follow the instructions to eject from create-react-app, or run jest directly and copy in any missing config that CRA set up for you (e.g. setupTests.js).

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