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: facebook.github.io/jest/docs/api.html 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?
Nayaab Khan-Khan
Frontend Developer @ StarOfService.com
The answer lies in your question itself :)
You need to add some code to ignore out the import of less files:
// package.json "jest": { "scriptPreprocessor": "path/to/jest-preprocessor", ... }// jest-preprocessor.js var babelJest = require("babel-jest"); module.exports = { process: function(src, filename) { return babelJest.process(src, filename) .replace(/^(require|import).*\.less.*;$/gm, ''); } };Hope this helps.