没有"框架的提供者:requirejs"! (解决:框架:requirejs)

时间:2015-07-07 02:33:11

标签: unit-testing requirejs karma-jasmine

我已经下载了this karma-jasmine sample code,我已经搜索了很多内容,似乎它必须有效。

但是我什么时候跑

   karma start karma.conf.js

它给了我这个错误:

 /Users/xe4me/www/html/apache/requirejs-     karma/node_modules/karma/node_modules/di/lib/injector.js:9
  throw error('No provider for "' + name + '"!');
        ^
 Error: No provider for "framework:jasmine"! (Resolving: framework:jasmine)
at error (/Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/node_modules/di/lib/injector.js:22:68)
at Object.parent.get (/Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/node_modules/di/lib/injector.js:9:13)
at get (/Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/node_modules/di/lib/injector.js:54:19)
at /Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/lib/server.js:29:14
at Array.forEach (native)
at start (/Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/lib/server.js:28:21)
at invoke (/Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/node_modules/di/lib/injector.js:75:15)
at Object.exports.start (/Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/lib/server.js:307:12)
at Object.exports.run (/Users/xe4me/www/html/apache/requirejs-karma/node_modules/karma/lib/cli.js:220:27)
at requireCliAndRun (/usr/local/lib/node_modules/karma-cli/bin/karma:44:16)

我也做了以下方法,没有运气:

 npm install karma-requirejs --save-dev

我做的一切都是正确的,但仍然没有运气,而且有一些问题似乎与我的相同,我已经尝试了所有的答案,但仍然没有运气; < / p>

任何帮助将不胜感激

由于

2 个答案:

答案 0 :(得分:4)

您需要requirejs&amp; karma-requirejs

npm install requirejs --save-dev
npm install karma-requirejs --save-dev

之后,请确保按照“Run Karma with Require.js”中的说明配置Karma。

最小工作示例(由karma init生成)

<强> karma.conf.js

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

<强>测试main.js

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

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    // Normalize paths to RequireJS module names.
    // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
    // then do not normalize the paths
    var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
    allTestFiles.push(normalizedTestModule);
  }
});

require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base',

  // dynamically load all test files
  deps: allTestFiles,

  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start
});

答案 1 :(得分:0)

尝试将karma-requirejs添加到karma.conf.js中的插件数组中

...

plugins: [             
            'karma-jasmine',                      
            'karma-chrome-launcher',                
            'karma-jasmine-html-reporter',
            'karma-webpack',
            'karma-requirejs'
        ],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false, 

...