Karma unit testing: Module name “react” has not been loaded yet for context: _. Use require([])

后端 未结 1 618
情话喂你
情话喂你 2020-12-11 23:48

I am trying to set up the unit testing framework for react. While doing so, the following error has occurred. I have searched all over the internet with no solution that is

相关标签:
1条回答
  • 2020-12-12 00:34

    The error you describe is exactly what RequireJS gives you when you have a require call in the CommonJS form (require('modX')) instead of the AMD form (require(['modX'], function (modX) {...})), and the call is done without being wrapped in define. RequireJS provides some support for using the CommonJS form, but there is a minimum of work that must be done by the developer to ensure that it works. A script that starts with this won't work:

    var modX = require('modX');
    // rest of the module
    

    You get the error message you got. You need this instead:

    define(function (require) {
        var modX = require('modX');
        // rest of the module
    });
    

    What is going on with your setup is that, as it is, Babel is transforming the ES6 modules into something that uses require without the define wrapper. In order to get Babel to output proper AMD modules, you need to install babel-plugin-transform-es2015-modules-amd and add transform-es2015-modules-amd to your list of Babel plugins. See the documentation here.

    0 讨论(0)
提交回复
热议问题