【发布时间】:2020-02-24 19:04:33
【问题描述】:
我有一个 webpack 配置文件,其中包含一些配置字符串,例如:
const config={
'accessUrl': 'http://localhost:8000/links',
'firstAuthUrl': 'http://localhost:8000/firstAccess',
'baseUrl': '/video?url=',
'urlRefreshToken': 'http://localhost:8000/refreshAccessToken',
'loginUrl': 'http://localhost:8000/login',
'userUrl':'http://localhost:8000/user'
}
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
historyApiFallback: true,
hot: true,
},
plugins: [
new DefinePlugin({
'config': JSON.stringify(config)
}),
],
});
没关系,但是当我向一个内部使用配置的组件添加一个玩笑测试(见上文)时,它不起作用,因为它无法识别配置。
组件的部分是:
<a href={config.baseUrl} className='btn btn-outline-warning justify-content-center mt-5'>
当我启动应用程序时它可以工作,但是当我编写和启动测试时:
describe('Content Component', () => {
const content = shallow(<Content {...props}/>); expect(content.render);
expect(content.find('.content')).toBeDefined();
})
测试运行时说:ReferenceError: config is not defined
我该如何解决?
非常感谢。
【问题讨论】: