运行Jest时替换`require.context`

时间:2017-02-17 16:21:15

标签: reactjs vue.js jestjs

我正在尝试使用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 )');
    }
});

0 个答案:

没有答案