jest: Jest doesn't works with JSX which imports CSS
Do you want to request a feature or report a bug? Bug
What is the current behavior?
If app.jsx
have import './app.css';
it’s impossible to run test, I got this error:
yarn run:test v0.21.3
$ NODE_ENV=client jest -u
FAIL src/__tests__/app.test.js
● Test suite failed to run
/home/svipben/Documents/react-boilerplate/src/client/app/app.css:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){body {
^
SyntaxError: Unexpected token {
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at Object.<anonymous> (src/client/app/app.jsx:7:1)
at Object.<anonymous> (src/__tests__/app.test.js:2:12)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.693s
Ran all test suites.
error Command failed with exit code 1.
What is the expected behavior? To run tests without error…
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
OS: Linux Ubuntu 16.04
Jest: Latest
Yarn: Latest
Node: Latest
babel-jest
: Latest
.babelrc:
"client": {
"presets": [
"latest", // All you need to compile what's in ES2015+
"flow", // Flow support
"react", // Strip flow types and transform JSX
"stage-2" // Experimental syntax extensions
],
"sourceMaps": true // Compile with Source Maps
},
jest: NODE_ENV=client jest -u
P.S. Everything runs well if I comment/remove import './app.css';
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 69
- Comments: 30 (2 by maintainers)
I solved this by using the
moduleNameMapper
key in the jest configurations in the package.json fileAfter this you will need to create the two files as described below
__mocks__/styleMock.js
__mocks__/fileMock.js
If you are using CSS Modules then it’s better to mock a proxy to enable className lookups. hence your configurations will change to:
But you will need to install
identity-obj-proxy
package as a dev dependancy i.e.For more information. You can refer to the jest docs
@svipben https://facebook.github.io/jest/docs/webpack.html#mocking-css-modules
I solved this problem by using ‘jest-transform-css’. After installing, added the below in the jest config.
Now css files will get transpiled just like js files did.
@SimenB
Incidentally, I WAS able to mock the CSS module successfully like this:
Hopefully that’s useful for someone else who is using all the features of CSS modules and, like me, was tearing their hair out at all the default “just use
identity-obj-proxy
” advice.Solution of @import Unexpected token=:)
Install package:
npm i --save-dev identity-obj-proxy
Add in jest.config.js
I added suggested config to
packages.json
, without success, then I realized I had ajest.config.js
in my project root directory, Jest uses that config file overpackages.json
.For reference here is my
jest.config.js
file:I’m using an external package and it imports its own files to a single file by using the following:
@import "progress-tracker/progress-tracker-variables.scss";
Whenever I run the Jest for to start testing the error above shows up. I already have specified css mockup inside the package.json
Im looking forward to get some help about that issue. Any tricks, suggestions?
Edit: I have tried both babel-jest and identity-obj-proxy packages and none of those worked. The problem still persists.
Edit 2: Also I have tried the config below according to solutions that I have come across with on the web.
Note that the approaches recommended here will not work if your CSS modules export values which your JSX files depend on. Most guides assume you’re only using CSS modules to export class names. I wish there was a guide for properly mocking CSS modules.
This is a contrived example, just demonstrating that you can share constants (values) between CSS modules and JS, and that conditional rendering or dynamic props can be assigned based on values.
But with
identity-obj-proxy
, these values can’t be cast to integers, let alone the actual values that are present. So if you have JSX code that relies on constants defined in your CSS modules, then you can’t test this without properly mocking the CSS module (having an object returned that has the values as testable integers).identity-obj-proxy
only assumes that your CSS modules contain an object with nothing more than scoped CSS classnames; it does not support the other aspect of CSS modules which is the ability to export values / constants that in turn get used in JS operations.I was able to resolve this by adding
@babel/polyfill
as a dev dependency@maximderbin Thank you! It resolves my issue by adding
moduleNameMapper
I have to import a raw css in my code
import "./cloud.css?raw"
I also modified jest config
"jest":{ "moduleNameMapper": { "\\.(css|less|scss|sass|css?raw)$": "identity-obj-proxy" },
still, I am getting test error on the import line. Any idea how to mock raw import?
Thanks, bro @krajasekhar It works for me 👍
It worked for me. Thanks!!!
I am struggling to solve almost same issue where my scss file contain @import statements. Test fails telling SyntaxError: Invalid or unexpected token. Can somebody help me please?
I followed solutions given above but none of them seems to be working for me
I’m using Babel
7
, babel-jest^24.5.0
, Jest^24.1.0
.After following the above advice, I’m still getting this error when I try to
import
a function or object into a Jest test file (e.g.Post.test.js
) when the target file (e.g.Post.js
) imports a CSS file - e.g../postStyles.css
.As per the article linked above, this is in my
package.json
:and in the root directory, I have
__mocks__/styleMock.js
which has the following code:Lastly, my
jest.config.js
:Which in turn configures Enzyme:
If somebody arrives here: This module is the most updated I have seen: https://www.npmjs.com/package/jest-css-modules-transform
it worked for me when I add the moduleNameMapper in the jest.config.ts, not the package.json
@matthew-dean where did you write the mock the CSS module and how do you reference it to jest?