我正在尝试使用 Jest
为我的 React 应用程序运行测试。 ,但我的测试一直失败,因为错误提示 require.context
不是函数。我正在寻找 Jest
的功能的替代品。不会窒息。我正在尝试动态 require
并修改一些*.vue
来自 Vue.js
的文件稍后在我的应用程序中使用。除此之外,我偶尔会遇到无法识别 .vue
格式的错误。文件并说它们无法解析。这是有问题的代码:
function inject(comp) {
comp.methods = Object.assign({}, comp.methods, stub.methods);
return comp;
}
let req = require.context('./', false, /\.vue$/);
components.forEach(function (component) {
try {
let filePath = __dirname + "\\" + component + '.vue';
let injected = inject(req(filePath));
Vue.component(getComponentName(component), injected);
let appComponent = {
name: injected.name,
props: {
autocompletion: {
metadata: getComponentName('template'),
score: xTemplatesScore,
attributes: injected.props || []
}
}
};
appComponents.push(appComponent);
} catch (err) {
console.log(err);
console.error('Vue file was not found for component:' + component + '. Please rename your files accordingly ( component-name.vue )');
}
});
最佳答案
如果有人仍然遇到这个问题,你需要知道 require.context
是一个特定于 webpack 的特性,所以它不能开 Jest 。
您可以尝试模拟它,就像有人在此 How can I mock Webpack's require.context in Jest? 中提供的答案一样所以问题。但是如果没有任何解决方法,您将无法使用它。
关于javascript - 运行 Jest 时替换 `require.context`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42302677/