【问题标题】:Getting "Mismatched anonymous define() module..." when I try running tests当我尝试运行测试时得到“不匹配的匿名定义()模块......”
【发布时间】:2014-07-04 13:48:50
【问题描述】:

我正在尝试使用 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;
}));

});
});

ma​​in.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
  });
 }

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

【问题讨论】:

    标签: angularjs requirejs karma-runner karma-jasmine


    【解决方案1】:

    最后我解决了所有问题,并且能够使用 requirejs 配置成功运行 jasmine 测试。我首先提到了业力配置中的所有依赖项,并将它们标记为included: false,以便它们通过我的测试配置文件由 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,以便加载模块和控制器:

    define(['angular-mocks', 'jquery', 'app/app'], function(angularmocks, $, app){
    describe.....
    }
    

    【讨论】:

      【解决方案2】:

      由于您使用 paths: { } 参数在 test-main.js 中加载所需的特定 .js 文件,因此您不必在 karma.config.js 中明确列出它们。相反,您可以使用您的线路{ pattern: 'app/**/*.js', included: false }。之前的所有行都是多余的。只要确保你有included: false 修饰符,否则Karma 会内联加载它们,你会遇到Uncaught Error: Mismatched anonymous define() 问题

      【讨论】:

        猜你喜欢
        • 2019-02-10
        • 2021-10-24
        • 2017-12-26
        • 1970-01-01
        • 2020-05-09
        • 2015-04-03
        • 2021-12-27
        • 2013-02-28
        • 2021-09-21
        相关资源
        最近更新 更多