Karma抱怨“模块不可用”

时间:2021-05-30 19:42:29

Up until a couple days ago my unit tests were running well and my code ran in the browser flawlessly. Then I noticed this after I added a stub module called 'profile':

直到几天前,我的单元测试运行良好,代码在浏览器中完美运行。然后我在添加了一个名为“profile”的存根模块后注意到了这一点:

INFO [karma]: Karma v0.12.24 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket 7W-0vnkWZaWxYYtwFrhT with id 9336780
PhantomJS 1.9.7 (Mac OS X) ERROR
  Error: [$injector:nomod] Module 'profile' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Now, I realise this is the most common AngularJS error on earth, and I've gotten pretty good at debugging this, but for the life of me I can't make this go away. The details:

现在,我意识到这是地球上最常见的AngularJS错误,我在调试这方面做得很好,但是对于我的生命,我不能让它消失。细节:

  • The code works in the browser with no errors.
  • 代码在浏览器中工作,没有错误。
  • Karma picks up my files this way:

    Karma以这种方式接收我的文件:

        'node_modules/angular/angular.js',
        'node_modules/angular-mocks/angular-mocks.js',
        'node_modules/angular-ui-router/release/angular-ui-router.js',
        'node_modules/angular-bootstrap/ui-bootstrap.js',
        'node_modules/angular-ui-form-validation/dist/angular-ui-form-validation.js',
        'node_modules/angular-animate/angular-animate.js',
        'client/app/**/*.js'
    
  • The main module has a dependency on 'profile' declared already.

    主模块依赖于已经声明的“profile”。

  • profile.js, profile.controller.js, and profile.controller.spec.js are stubs generated by cg-angular-fullstack and have no special code. The generator by default sticks all them in the main module so I changed the module for each to 'profile'.
  • 概要文件。js,profile.controller。js和profile.controller.spec。js是cg-angular-fullstack生成的存根,没有特殊的代码。默认情况下,生成器将所有模块都粘在主模块中,因此我将每个模块的模块改为“profile”。

I'm completely out of ideas. If I can't debug this I'll have to assume my whole app design is flawed and restart. :(

我完全没有主意了。如果我不能调试,我将不得不假设我的整个应用程序设计是有缺陷的,并重新启动。:(

1 个解决方案

#1


20  

Somehow Karma wasn't importing files in the right order, even though it had been for weeks prior. My resolution was three parts:

不知何故,Karma并没有按照正确的顺序导入文件,即使是在几周前。我的决心有三个部分:

  1. Renamed all module definitions from *.js to *.module.js.
  2. 从*重命名所有模块定义。* .module.js js。
  3. Renamed all UI Router work that wasn't in a module definition from *.js to *.state.js.
  4. 重命名所有不在*模块定义中的UI路由器工作。* .state.js js。
  5. Imported files in this order:

    按此顺序导入文件:

          'node_modules/jquery/dist/jquery.js',
          'node_modules/angular/angular.js',
          'node_modules/angular-mocks/angular-mocks.js',
          'node_modules/angular-ui-router/release/angular-ui-router.js',
          'node_modules/angular-bootstrap/ui-bootstrap.js',
          'node_modules/angular-ui-form-validation/dist/angular-ui-form-validation.js',
          'node_modules/angular-animate/angular-animate.js',
    
          // client files
          'client/app/app.module.js',
          'client/app/app.controller.js',
          'client/app/**/*.service.js',
          'client/app/**/*.directive.js',
          'client/app/**/*.module.js',
          'client/app/**/*.state.js',
          'client/app/**/*.controller.js',
          'client/app/**/*.spec.js'
    

Only then did the tests start working again.

直到那时,测试才重新开始工作。

#1


20  

Somehow Karma wasn't importing files in the right order, even though it had been for weeks prior. My resolution was three parts:

不知何故,Karma并没有按照正确的顺序导入文件,即使是在几周前。我的决心有三个部分:

  1. Renamed all module definitions from *.js to *.module.js.
  2. 从*重命名所有模块定义。* .module.js js。
  3. Renamed all UI Router work that wasn't in a module definition from *.js to *.state.js.
  4. 重命名所有不在*模块定义中的UI路由器工作。* .state.js js。
  5. Imported files in this order:

    按此顺序导入文件:

          'node_modules/jquery/dist/jquery.js',
          'node_modules/angular/angular.js',
          'node_modules/angular-mocks/angular-mocks.js',
          'node_modules/angular-ui-router/release/angular-ui-router.js',
          'node_modules/angular-bootstrap/ui-bootstrap.js',
          'node_modules/angular-ui-form-validation/dist/angular-ui-form-validation.js',
          'node_modules/angular-animate/angular-animate.js',
    
          // client files
          'client/app/app.module.js',
          'client/app/app.controller.js',
          'client/app/**/*.service.js',
          'client/app/**/*.directive.js',
          'client/app/**/*.module.js',
          'client/app/**/*.state.js',
          'client/app/**/*.controller.js',
          'client/app/**/*.spec.js'
    

Only then did the tests start working again.

直到那时,测试才重新开始工作。