javascript - Replacement for `require.context` when running Jest - Stack Overflow

I'm trying to run tests for my React application using Jest, but my tests keep on failing due to a

I'm trying to run tests for my React application using Jest, but my tests keep on failing due to an error saying that require.context is not a function. I am looking for a replacement for that functionality that Jest will not choke on. I am trying to dynamically require and modify some *.vue file from Vue.jsto be used later on in my application. In addition to this, I occasionally get errors that don't recognize the format of the .vue files and say that they can't be parsed. This is the code in question:

function inject(p) {
    p.methods = Object.assign({}, p.methods, stub.methods);
    return p;
} 

let req = require.context('./', false, /\.vue$/);
ponents.forEach(function (ponent) {
    try {
        let filePath = __dirname + "\\" + ponent + '.vue';
        let injected = inject(req(filePath));
        Vueponent(getComponentName(ponent), injected);

        let appComponent = {
            name: injected.name,
            props: {
                autopletion: {
                    metadata: getComponentName('template'),
                    score: xTemplatesScore,
                    attributes: injected.props || []
                }
            }
        };

        appComponents.push(appComponent);
    } catch (err) {
        console.log(err);
        console.error('Vue file was not found for ponent:' + ponent + '. Please rename your files accordingly ( ponent-name.vue )');
    }
});

I'm trying to run tests for my React application using Jest, but my tests keep on failing due to an error saying that require.context is not a function. I am looking for a replacement for that functionality that Jest will not choke on. I am trying to dynamically require and modify some *.vue file from Vue.jsto be used later on in my application. In addition to this, I occasionally get errors that don't recognize the format of the .vue files and say that they can't be parsed. This is the code in question:

function inject(p) {
    p.methods = Object.assign({}, p.methods, stub.methods);
    return p;
} 

let req = require.context('./', false, /\.vue$/);
ponents.forEach(function (ponent) {
    try {
        let filePath = __dirname + "\\" + ponent + '.vue';
        let injected = inject(req(filePath));
        Vue.ponent(getComponentName(ponent), injected);

        let appComponent = {
            name: injected.name,
            props: {
                autopletion: {
                    metadata: getComponentName('template'),
                    score: xTemplatesScore,
                    attributes: injected.props || []
                }
            }
        };

        appComponents.push(appComponent);
    } catch (err) {
        console.log(err);
        console.error('Vue file was not found for ponent:' + ponent + '. Please rename your files accordingly ( ponent-name.vue )');
    }
});
Share Improve this question edited Sep 4, 2019 at 5:43 Daniel Richter 3581 gold badge5 silver badges25 bronze badges asked Feb 17, 2017 at 16:21 rafafan2010rafafan2010 1,5993 gold badges15 silver badges25 bronze badges 1
  • 4 Possible duplicate of How can I mock Webpack's require.context in Jest? – Nelaina Commented Dec 4, 2017 at 21:21
Add a ment  | 

1 Answer 1

Reset to default 1

If someone still run into this issue, you need to know that require.context is a webpack-specific feature, so it doesn't work in jest.

You can try to mock it, like someone already provided as an answer in this How can I mock Webpack's require.context in Jest? SO question. But you can't use it without any workaround.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745343892a4623455.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信