简体   繁体   English

模块名称__未加载因果报应和requirejs

[英]Module name __ not loaded with karma and requirejs

I have some repositories (A, B, C and TestRepo) all of them in local, i get karma to load spec files from repos A or B or C with a param (node script). 我在本地有一些存储库(A,B,C和TestRepo),我都可以使用参数(节点脚本)从存储库A或B或C加载规范文件。

Now im trying to transpile the files cause some test are using import so i need babel. 现在,我正在尝试转换文件,导致某些测试正在使用import因此我需要babel。 I configured babel as a preprocessor and it seems like it is working but know i get this error: 我将babel配置为预处理器,似乎可以正常工作,但知道出现此错误:

Uncaught Error: Module name "builder" has not been loaded yet for context: _. Use require([])

I tried all configs i know but still not working. 我尝试了所有我知道的配置,但仍然无法正常工作。

My karma.config.js is this: 我的karma.config.js是这样的:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      'test-main.js'
    ],
    exclude: [
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    concurrency: Infinity
  });
}

My test-main.js for requirejs: 我的test-main.js for requirejs:

var allTestFiles = []
var TEST_REGEXP = /(spec|test)\.js$/i

Object.keys(window.__karma__.files).forEach(function (file) {
  if (TEST_REGEXP.test(file)) {
    var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '')
    allTestFiles.push(normalizedTestModule)
  }
})

require.config({
  baseUrl: '/base',
  deps: allTestFiles,
  callback: window.__karma__.start
})

And my node file, where i set karma files, preprocessor and so on base on the repository i got from params is this: 我的节点文件是在基于params的存储库的基础上设置业力文件,预处理器等的:

const cfg     = require('karma').config;
const stopper = require('karma').stopper;
const runner  = require('karma').runner;
const Server  = require('karma').Server;
const path    = require('path');

const REPOSITORY = process.argv[2] || '';
let karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'), { port: 9876 } );
let BASE_PATH;

if (REPOSITORY !== 'all') {
    BASE_PATH = `/Users/fernando.delolmo/${REPOSITORY}`;
    karmaConfig.files.push({pattern: BASE_PATH + '/test/**/*.spec.js'});
    karmaConfig.files.push({pattern: BASE_PATH + '/src/**/*.js', included: true});
    karmaConfig.preprocessors[BASE_PATH + '/src/**/*.js']       = ['babel'];
    karmaConfig.preprocessors[BASE_PATH + '/test/**/*.spec.js'] = ['babel'];
    karmaConfig.exclude.push(BASE_PATH + '/node_modules/**/*.spec.js');   
} else {
    BASE_PATH = '/Users/fernando.delolmo/';
    karmaConfig.files.push({pattern: BASE_PATH + '**/test/**/*.spec.js'});
    karmaConfig.files.push({pattern: BASE_PATH + '**/src/**/*.js', included: true});
    karmaConfig.preprocessors[BASE_PATH + '**/src/**/*.js']       = ['babel'];
    karmaConfig.preprocessors[BASE_PATH + '**/test/**/*.spec.js'] = ['babel'];
    karmaConfig.exclude.push(BASE_PATH + '**/node_modules/**/*.spec.js');   
}

karmaConfig.basePath = BASE_PATH;

var server = new Server(karmaConfig, function(exitCode) {
  process.exit(exitCode);
});

server.start();

server.on('browser_start', function() {
    runner.run(karmaConfig, function(exitCode) {
        process.exit(exitCode);
    });
});

server.on('browser_complete', function() {
    stopper.stop(karmaConfig, function(exitCode) {
        process.exit(exitCode);
    });
});

Its my first time configuring karma, requirejs and babel all together so any help will be great :) 这是我第一次同时配置业力,requirejs和babel,因此任何帮助都将非常棒:)

Okay finally i got what's the problem here. 好吧,终于我明白了这里的问题。 Just in case some one get here finding any solution to problems like this. 以防万一有人来这里找到解决此类问题的方法。

If you want to run test with ES2015 modules with karma, you will use babel to transpile into CommonJS modules at first, good! 如果您想使用带有业力的ES2015模块运行测试,则将首先使用babel转换为CommonJS模块,很好! but... you will get errors like export is undefined.. and.. why?? 但是...您会得到类似未定义导出的错误..和..为什么? Simple, export/import still not supported on the browsers, this means you CAN'T run CommonJS modules on the browser without any module bundler (browersify or webpack). 浏览器仍然不支持简单的导出/导入,这意味着没有任何模块捆绑器(browersify或webpack),您将无法在浏览器中运行CommonJS模块。

So, keep this clear: 因此,请保持清楚:

1st -> Babel only transpile your ES2015 code and test into CommonJS modules 2nd -> Webpack / Browersify get CommonJS modules and bundle them into a bundle.js and then you can run that on the browser with karma. 1-> Babel仅将您的ES2015代码转换并测试到CommonJS模块中。2-> Webpack / Browersify获得CommonJS模块并将其捆绑到bundle.js中,然后您可以使用karma在浏览器上运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尚未为上下文加载RequireJS模块名称“ requirejs”。 使用require([]) - RequireJS Module name “requirejs” has not been loaded yet for context. use require([]) RequireJS - 加载已加载的模块 - RequireJS - Loading an already loaded module 使用RequireJS文本插件尚未为上下文加载模块名称 - Module name has not been loaded yet for context using RequireJS text plugin RequireJS:尚未为上下文加载模块名称__,仅针对SOME(并非全部)加载 - RequireJS: Module name __ has not been loaded yet for context, but only for SOME, not all RequireJS中的动态需求,获取“模块名称尚未加载上下文”错误? - Dynamic require in RequireJS, getting “Module name has not been loaded yet for context” error? RequireJS-更改jQuery模块名称 - RequireJS - change jQuery module name Karma 单元测试:尚未为上下文加载模块名称“react”:_。 使用 require([]) - Karma unit testing: Module name "react" has not been loaded yet for context: _. Use require([]) 在模块加载时有requirejs运行回调 - have requirejs to run callback when the module is loaded 是否可以检测脚本是否作为RequireJS模块加载? - Is it possible to detect if a script is being loaded as a RequireJS module? RequireJS:模块ID与模块名称 - RequireJS: module ID vs module name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM