Getting “Mismatched anonymous define() module…” when I try running tests

前端 未结 2 1433
囚心锁ツ
囚心锁ツ 2020-11-30 09:24

I am trying to configure my karma jasmine unit testing with requirejs. But each time i run it, i am getting the below error:

Chrome 34.0.1847 (Mac OS X 10.9.         


        
相关标签:
2条回答
  • 2020-11-30 09:49

    Finally i solved all the issues and was able to run the jasmine test successfully with requirejs configuration. I had top mention all the dependencies in the karma config and mark them as included: false exclusively so that they get loaded by requirejs through my test-config file.

    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}]
    

    only the test-config is loaded through karma and all others to be included in the karma config but mark as false.

    Also, i had to load the app.js in my spec file so that the modules and controllers get loaded:

    define(['angular-mocks', 'jquery', 'app/app'], function(angularmocks, $, app){
    describe.....
    }
    
    0 讨论(0)
  • 2020-11-30 09:54

    Since you're loading the specific .js files you need in your test-main.js with the paths: { } parameter, you don't have to explicitly list them in karma.config.js. Instead, you can just use your line { pattern: 'app/**/*.js', included: false }. All the lines before that are redundant. Just make sure you have the included: false modifier, otherwise Karma will load them in-line, and you'll get the Uncaught Error: Mismatched anonymous define() problem

    0 讨论(0)
提交回复
热议问题