【发布时间】:2020-08-24 17:54:04
【问题描述】:
这是我的 jest.config:
module.exports = {
roots: ['<rootDir>/src'],
collectCoverageFrom: [
'<rootDir>/src/**/*.{ts,tsx}'
],
coverageDirectory: 'coverage',
testEnvironment: 'jsdom',
transform: {
'.+\\.(ts|tsx)$': 'ts-jest'
},
transformIgnorePatterns: [
'.+\\.scss$'
],
moduleNameMapper: {
'\\.scss$': 'identity-obj-proxy',
'@/(.*)': '<rootDir>/src/$1'
}
}
这就是我的 webpack.config:
const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
mode: 'development',
entry: './src/main/index.tsx',
output: {
path: path.join(__dirname, 'public/js'),
filename: 'bundle.js',
publicPath: '/public/js'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.scss'],
alias: {
'@': path.join(__dirname, 'src')
}
},
devServer: {
contentBase: './public',
writeToDisk: true
},
module: {
rules: [{
test: /\.(ts)x?$/,
exclude: /node_modules/,
loader: 'ts-loader'
}, {
test: /\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true
}
},
'sass-loader'
]
}]
},
externals: {
react: 'React',
'react-dom': 'ReactDOM'
},
plugins: [
new CleanWebpackPlugin()
]
}
我总是收到错误:找不到模块'./login-styles.scss'
【问题讨论】:
-
哪里产生的错误?您只添加了配置文件。项目中是否存在文件
login-styles.scss? -
是的。如果我使用 webpack 启动项目,它可以正常工作。所有样式均已正确加载。只是这个加载 sass 的 tsx 测试失败了。如果我删除 scss 导入我的测试通过。
标签: reactjs typescript webpack sass jestjs