score:0
I added moduleNameMapper at the bottom of my package.json where I configured my jest just like this:
"jest": {
"verbose": true,
"moduleNameMapper": {
"\\.(scss|less)$": "<rootDir>/config/CSSStub.js"
}
}
score:1
Update: Aug 2021 If you are using Next JS with TypeScript. Simply follow the examples repo.
Else you will be wasting days configuring the environment.
https://github.com/vercel/next.js/blob/canary/examples/with-typescript-eslint-jest/package.json
score:7
Solution of @import Unexpected token=:)
Install package:
npm i --save-dev identity-obj-proxy
Add in jest.config.js
module.exports = {
"moduleNameMapper": {
"\\.(css|less|scss)$": "identity-obj-proxy"
}
}
score:7
If you're using ts-jest, none of the solutions above will work! You'll need to mock transform.
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
roots: [
"<rootDir>/src"
],
transform: {
".(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jest-config/file-mock.js",
'.(css|less)$': '<rootDir>/jest-config/style-mock.js'
},
};
file-mock.js
module.exports = {
process() {
return `module.exports = 'test-file-stub'`;
},
};
style-mock.js
module.exports = {
process() {
return 'module.exports = {};';
}
};
I found this working example if you want more details.
score:15
Similar situation, installing identity-object-proxy and adding it to my jest config for CSS is what worked for me.
//jest.config.js
module.exports = {
moduleNameMapper: {
"\\.(css|sass)$": "identity-obj-proxy",
},
};
The specific error I was seeing:
Jest encountered an unexpected token
/Users/foo/projects/crepl/components/atoms/button/styles.css:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.button { }
^
SyntaxError: Unexpected token .
1 | import React from 'react';
> 2 | import styles from './styles.css';
score:27
UPDATE who use create-react-app from feb 2018. You cannot override the moduleNameMapper in package.json but in jest.config.js it works, unfortunately i havent found any docs about this why it does. So my jest.config.js look like this:
module.exports = {
...,
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(scss|sass|css)$": "identity-obj-proxy"
}
}
and it skips scss files and @import quite well.
Backing my answer i followed jest webpack
score:61
I solved this by using the moduleNameMapper
key in the jest configurations in the package.json file
{
"jest":{
"moduleNameMapper":{
"\\.(css|less|sass|scss)$": "<rootDir>/__mocks__/styleMock.js",
"\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js"
}
}
}
After this you will need to create the two files as described below
__mocks__/styleMock.js
module.exports = {};
__mocks__/fileMock.js
module.exports = 'test-file-stub';
If you are using CSS Modules then it's better to mock a proxy to enable className lookups. hence your configurations will change to:
{
"jest":{
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less|scss|sass)$": "identity-obj-proxy"
},
}
}
But you will need to install identity-obj-proxy
package as a dev dependancy i.e.
yarn add identity-obj-proxy -D
For more information. You can refer to the jest docs
score:104
moduleNameMapper
is the setting that tells Jest how to interpret files with different extension. You need to tell it how to handle Less files.
Create a file like this in your project (you can use a different name or path if you’d like):
config/CSSStub.js
module.exports = {};
This stub is the module we will tell Jest to use instead of CSS or Less files. Then change moduleNameMapper
setting and add this line to its object to use it:
'^.+\\.(css|less)$': '<rootDir>/config/CSSStub.js'
Now Jest will treat any CSS or Less file as a module exporting an empty object. You can do something else too—for example, if you use CSS Modules, you can use a Proxy so every import returns the imported property name.
Read more in this guide.
Source: stackoverflow.com
Related Query
- SyntaxError with Jest and React and importing CSS files
- SyntaxError with Jest and React importing SCSS files
- Test react components with jest and enzyme and css modules
- How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component?
- How to test a className with the Jest and React testing library
- How to import CSS modules with Typescript, React and Webpack
- How to mock React component methods with jest and enzyme
- Multiple classNames with CSS Modules and React
- Importing CSS files in Isomorphic React Components
- Remove unused css with React and Webpack
- Line two divs side by side with CSS and React
- Spying on React functional component method with jest and enzyme; Cannot spyOn on a primitive value
- How to use vw and vh css with React Native
- How to test form submission in React with Jest and Enzyme? Cannot read property 'preventDefault' of undefined
- Test a form with Jest and React JS TestUtils
- How to test snapshots with Jest and new React lazy 16.6 API
- using google fonts in nextjs with sass, css and semantic ui react
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- Testing debounced function in React component with Jest and Enzyme
- How to mock a React component lifecycle method with Jest and Enzyme?
- Jest Manual Mocks with React and Typescript: mock an ES6 class dependency
- CSS Code Splitting with Webpack 2 and React Router
- Test text input with react and jest
- how can I test if useState hook has been called with jest and react testing library?
- Test with Mocha and Enzyme a React component that uses React CSS Modules
- webpack not reflecting changes in js files with react and django
- Testing react context with jest and react-testing-library
- How to use Jest with React and Webpack
- Unit test method that calls clearInterval with Jest and Enzyme on React
- How to test redux state update with react testing library and jest
More Query from same tag
- Deploy create-react-app on Apache web server
- How to make a generic 'filter' Higher-Order Component in React.js?
- Input element not accepting capital letters
- React, mapping over a medium-complex json Object to get the keys inside of messages
- How to get proper error in catch statement?
- How to remove specific css in appendChild?
- getInitialState() is showing error in console
- Why react-select is breaking when I am using @react-google-maps/api?
- How to simplify setTimeout in ReactJS
- How to use gatsby-image with an image url?
- Divide Array list into 3 column but it not aligned properly in HTML CSS
- MongoDB get all distinct values
- Getting an ESLint: Unnecessary escape character
- Can't perform a React state update on an unmounted component - even when clearing interval
- change value of input field react
- Animation not working in ReactVR
- Force a form input in uppercase - onChange and React Hooks
- React Native - how to make a border image?
- Method parameter: destructuring + keeping original parameter (ReactJS component)
- Reconciliation in React detailed explanation
- Radio Input onChange only fires once?
- React Hooks Refs
- Displaying Different Images React
- React & TypeScript: Avoid context default value
- Background Image not being displayed
- React: How to traverse through all children nodes in the tree view structure?
- How do I get the title of an uploaded image which is to be entered as a value in the image ID column in typescript?
- How do I use Firebase and react to create static information that all my users have access to?
- sending results from node.js to react
- React : Cannot set property 'animation' of undefined