angularjs - 当我尝试运行测试时得到 "Mismatched anonymous define() module..."

标签 angularjs requirejs karma-runner karma-jasmine

我正在尝试使用 requirejs 配置我的 karma Jasmine 单元测试。但是每次我运行它时,我都会收到以下错误:

Chrome 34.0.1847 (Mac OS X 10.9.2) ERROR
Uncaught Error: Mismatched anonymous define() module: function (angular){

 describe('Unit: Testing RequireJS', function(){
  var ctrl;
  var scope;
  var rootScope;

  beforeEach(angular.mock.module('wsaApp'));

  beforeEach(angular.mock...<omitted>...ch

以下是不同的文件:
规范文件:
define(['angular'], function(angular){

describe('Unit: Testing RequireJS', function(){
   var ctrl;
   var scope;
   var rootScope;

beforeEach(angular.mock.module('wsaApp'));

beforeEach(angular.mock.inject(function($rootScope){
    scope = $rootScope.$new();
    rootScope = $rootScope;
}));

});
});

main.js
require.config({

paths: {
    /* ABC order */
    'angular': 'vendor/angular/1.2.0/angular.min'
},
shim: {
    'angular': { exports: 'angular' },

    'app/controllers': { deps: ['angular'] }
}
});

test-main.js
// This creates an array of all the files that Karma finds with a suffix of
// Test.js (eg utilsTest.js) to be added to the Require JS config below
var tests = [],
file;
for (file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
    if(/spec\.js$/.test(file)) {
        tests.push(file);
    }
}
}
requirejs.config({
baseUrl: '/base/public/javascripts/',  // Karma serves files from /base/<your-base-path>
    paths: {
        /* ABC order */
        'angular': 'vendor/angular/1.2.1/angular.min'

    },
    shim: {
        'angular': { exports: 'angular' },
        'app/controllers': { deps: ['angular'] },           
         },
deps: tests,  // add tests array to load our tests

callback: window.__karma__.start  // start tests once Require.js is done
});

karma .conf.js
//Karma configuration
module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',

// Fix for "JASMINE is not supported anymore" warning
frameworks: ["jasmine", "requirejs"],

// list of files / patterns to load in the browser
files: [
    'vendor/angular/1.2.1/angular.js',
    'jquery-1.7.1.min.js',
    'test/spec/**/*.js',
    'test/test-main.js'
],

preprocessors: {
    'app/**/*.js': 'coverage'
},

// list of files to exclude
exclude: ['app/main.js'],

// test results reporter to use
// possible values: dots || progress || growl
reporters: ['progress', 'coverage'],

coverageReporter : {
    type: 'html',
    dir: 'coverage/'
},


// web server port
port: 9876,

// cli runner port
runnerPort: 9100,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],

browserNoActivityTimeout: 100000,
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 20000,

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
  });
 }

我尝试了其他线程中提到的不同选项,但似乎没有任何效果。

最佳答案

最后,我解决了所有问题,并且能够使用 requirejs 配置成功运行 jasmine 测试。我首先提到了 karma 配置中的所有依赖项并将它们标记为 included: false专门以便它们通过我的 test-config 文件由 requirejs 加载。

files: [
    {pattern: 'vendor/angular/1.2.1/angular.js', included: false},
    {pattern: 'vendor/angular/1.2.1/angular-mocks.js', included: false},
    {pattern: 'vendor/angular/1.2.1/angular-*.js', included: false},
    {pattern: 'vendor/bootstrap/bootstrap-*.js', included: false},
    {pattern: 'jquery-1.7.1.min.js', included: false},
    {pattern: 'app/app.js', included: false},
    {pattern: 'app/**/*.js', included: false},
    {pattern: 'test/test-config.js', included: true}]

只有 test-config 通过 karma 加载,所有其他配置都包含在 karma 配置中,但标记为 false。

此外,我必须在我的规范文件中加载 app.js,以便加载模块和 Controller :
define(['angular-mocks', 'jquery', 'app/app'], function(angularmocks, $, app){
describe.....
}

关于angularjs - 当我尝试运行测试时得到 "Mismatched anonymous define() module...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23689671/

相关文章:

javascript - jspm + karma + karma-jspm + 插件 typescript

javascript - 使用 angular.element ("[ng-controller="someCtrl"]").scope() 是个好习惯吗

angularjs - 使用 webpack 和 typescript 摇树

javascript - ng-重复 : Identify Change of Element Value In Sorted List

npm - Jhipster 安装 : The package karma does not satisfy its siblings' peerDependencies requirements

typescript - 找不到跟踪 : AssertionError (Karma-Typescript) 的 SourceMap 位置

angularjs - $http get 返回与 $resource get 不同

javascript - RequireJS:回调函数中依赖项和参数的不同计数

requirejs - 要求 js 删除定义以强制重新加载

javascript - 通过 require.js 加载所有内容时,如何将 Knockout View 模型的一部分传递给组件?