当我尝试运行testing时获取“不匹配的匿名define()模块…”

我想用requirejsconfiguration我的业力茉莉花unit testing。 但是,每次我运行它,我得到以下错误:

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 

下面是differenr文件: spec文件:

 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'] } } }); 

testingmain.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 }); } 

我已经尝试了其他线程中提到的不同的选项,但似乎没有工作。

最后,我解决了所有的问题,并能够运行茉莉花testingrequirejsconfiguration成功。 我最先提到了karmaconfiguration中的所有依赖关系,并将它们标记为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}] 

只有testingconfiguration是通过业力加载的,其他的都包含在业务configuration中,但是标记为假。

此外,我不得不加载app.js在我的spec文件,以便模块和控制器加载:

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

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

Interesting Posts