gpt4 book ai didi

node.js - 让 Jest 在测试时忽略 .less 导入

转载 作者:IT老高 更新时间:2023-10-28 23:16:03 27 4
gpt4 key购买 nike

测试组件

我正在尝试使用 Jest 对 react 组件运行一些非常简单的测试。由于组件文件顶部的这一行,我遇到了问题

import {} from './style.less';

这里的导入不需要测试,最好从测试中忽略。

[npm 测试]的结果

当通过 npm test 运行测试时,我得到响应

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

但是,如果我注释掉较少的导入行,我的测试就会正确运行。

问题

我怎样才能 Jest 跳过这行代码或忽略这个导入?

最佳答案

除了在测试顶部手动模拟 .less 文件之外,我可对于Jest地忽略更少文件的唯一方法,例如

jest.mock("../../src/styles.less", () => jest.fn());

是在 package.json 中添加以下内容:

"jest": {
"moduleNameMapper": {
".*\\.less$": "<rootDir>/pathToDummyFile/dummy.js"
}
},

每当您的代码尝试导入 less 文件时,它都会重定向导入以获取空的虚拟文件,这将避免您遇到的意外 token 错误。

关于node.js - 让 Jest 在测试时忽略 .less 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37072641/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com