我正在尝试在Jest中导入和安装组件。不幸的是,我遇到一个错误,指出
styles/variables.scss:2
$blue:#324157;
^
SyntaxError: Invalid or unexpected token
这里是不适合我的解决方案。
使用identity-obj-proxy(无法使用):
"moduleNameMapper": {
"\\.(css|scss|sass|less)$": "identity-obj-proxy"
}
使用styleMock(无法使用):
//package.json
"moduleNameMapper": {
"\\.(css|scss|sass|less)$": "<rootDir>/tests/__mocks__/styleMock.js"
}
//<rootDir>/tests/__mocks__/styleMock.js
module.exports = {}
以下解决方案不起作用,错误仍然存在。
我发现了另一种解决方案,可以通过在jest-transform-stub
中简单地添加jest.transform
来帮助其他人>
"jest": {
"transform": {
".+\\.(css|styl|less|sass|scss|png|jpg|svg|ttf|woff|woff2)$": "jest-transform-stub"
}
}