测试时让Jest忽略.less导入 [英] Make Jest ignore the .less import when testing
问题描述
我正在尝试对使用Jest的react组件运行一些非常简单的测试.由于组件文件顶部的这一行,我遇到了问题
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.
通过 npm test
运行测试时,我得到响应
When the test is run via npm test
I get the response
失败测试/app_test.js●运行时错误语法错误:意外令牌{位于文件"client/components/app/style.less"中.
FAIL tests/app_test.js ● Runtime Error SyntaxError: Unexpected token { in file 'client/components/app/style.less'.
确保正确设置了预处理器,并确保'preprocessorIgnorePatterns'配置正确: http://facebook.github.io/jest/docs/api.html#preprocessorignorepatterns-array-string 如果您当前正在设置Jest或修改预处理器,尝试 jest --no-cache
.预处理器:node_modules/jest-css-modules.Jest试图执行以下预处理代码://一些.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?
推荐答案
如果仍然存在此问题,请尝试将以下配置添加到您的 jest.config.json
文件中:
If you still have this issue, try to add the following config to your jest.config.json
file:
转换":{"^.+ \\.(css | less)$":"./styleMock.js"}
Here, in your styleMock.js
you're creating a transformer which will transform your .less
files like this:
module.exports = {过程() {返回 ''}}
这篇关于测试时让Jest忽略.less导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!