Question

My services.js file was getting quite large so I decided it'd be best to split out the individual services into separate files (service1.js, service2.js, etc).

Unfortunately, this broke all my unit tests. I'm no longer able to import the service dependencies into my tests. I'm seeing errors like this when I run my unit tests:

Error: [$injector:unpr] Unknown provider: Service1Provider <- Service1

I can't find any article on the web that addresses these issues. My app structure is pretty standard and OOTB, nothing really different from angular-seed (except of course the separate files for each service).

Please let me know if you need more info.

Was it helpful?

Solution

I currently work with @mtical, and it turns out the error was indeed in karma.conf.js. As he said, we broke apart our services into multiple files, and our main service file was named "service.js". By default, karma loads all js files that are not explicitly listed in the karma.conf.js file in recursive alphabetical order.

This was causing our "service.js" file to be loaded after all of our other service files, which were listed before that file when in alphabetical order. Unfortunately, all of those other services had "service.js" as a dependency, so when our tests ran, they weren't able to find the services we needed.

The solution was to explicitly list "service.js" before the recursive loading of other files in our karma.conf.js file, as follows:

...
files : [
      'app/lib/angular/angular.js',
      'app/lib/angular/angular-*.js',
      'test/lib/angular/angular-mocks.js',
      'app/js/services/services.js',
      'app/js/**/*.js',
      'test/unit/**/*.js'
    ],
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top