I am trying to run some very simple tests on react components with Jest. I am running into problems because of this line at the top of my component file
import {} from './style.less';
The import here doesn't need to be tested and would ideally be ignored from the test.
When the test is run via npm test
I get the response
FAIL tests/app_test.js ● Runtime Error SyntaxError: Unexpected token { in file 'client/components/app/style.less'.
Make sure your preprocessor is set up correctly and ensure your 'preprocessorIgnorePatterns' configuration is correct: http://facebook.github.io/jest/docs/api.html#preprocessorignorepatterns-array-string If you are currently setting up Jest or modifying your preprocessor, try
jest --no-cache
. Preprocessor: node_modules/jest-css-modules. Jest tried to the execute the following preprocessed code: //some .less code
But if I comment out the less import line my test runs correctly.
How can I get jest to skip over this line of code or ignore this import?
To mock an imported function with Jest we use the jest. mock() function. jest. mock() is called with one required argument - the import path of the module we're mocking.
Many people choose to use Jest and Enzyme together to test their React web applications. They use Jest as a test runner and assertion library, then use Enzyme to build the tests for their UI. This results in slimmer, cleaner testing code that's also easier to debug when a test breaks.
You should use testPathIgnorePatterns in your jest config. An array of regexp pattern strings that are matched against all test paths before executing the test. If the test path matches any of the patterns, it will be skipped.
jest uses Babel to transpile all TypeScript code - including node_modules when they need to be transpiled - to JavaScript. mocha uses ts-node to directly run TypeScript. This is the reason for the difference.
The only way I could get jest to ignore less files for me, other than manually mocking the .less file at the top of the test e.g.
jest.mock("../../src/styles.less", () => jest.fn());
Was to add the following to the package.json:
"jest": {
"moduleNameMapper": {
".*\\.less$": "<rootDir>/pathToDummyFile/dummy.js"
}
},
Whenever your code tries to import a less file it will instead redirect the import to fetch an empty dummy file, which will avoid the unexpected token error you experienced.
If you still have this issue, try to add the following config to your jest.config.json
file:
"transform": {
"^.+\\.(css|less)$": "./styleMock.js"
}
Here, in your styleMock.js
you're creating a transformer which will transform your .less
files like this:
module.exports = {
process() {
return ''
}
}
A little late to the party but maybe this will help someone today. Use jest-transform-stub.
In your Jest config, add jest-transform-stub to transform non JavaScript assets you want to stub:
{ "jest": { // .. "transform": { "^.+\\.js$": "babel-jest", ".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "jest-transform-stub" } } }
FAQ
My module isn't being transformed
Jest doesn't apply transforms to node_modules by default. You can solve this by using
moduleNameMapper
:{ "jest": { // .. "moduleNameMapper": { "^.+.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "jest-transform-stub" } } }
I've solved same problem with ignore-styles(https://github.com/bkonkle/ignore-styles). I've used this lib with mocha but I think it should be ok with jest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With