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

自古美人都是妖i 提交于 2019-11-28 13:58:45

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!