4
votes

I use Angular2 RC1 and I have several unit tests regarding different components with the following structure:

import {provide} from '@angular/core';
import {
  TestComponentBuilder
} from '@angular/compiler/testing';

import {
  beforeEach,
  ddescribe,
  xdescribe,
  describe,
  expect,
  iit,
  inject,
  injectAsync,
  async,
  beforeEachProviders,
  setBaseTestProviders,
  it,
  xit
} from '@angular/core/testing';

import {
  TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
  TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
} from '@angular/platform-browser-dynamic/testing/browser';

describe('Test component 1', () => {
  setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
    TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);

  it('should something',
    async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
      var updateService = new UpdateService();

      tcb.overrideProviders(ShapeCircleLayerComponent, [
        provide(UpdateService, { useValue: updateService })
      ])
      .createAsync(Component1).then((componentFixture) => {
        (...)
      });
    });
  });
});

Each test works if run alone but when I run them at the same time within Karma, I get the following error:

Chrome 50.0.2661 (Linux 0.0.0) Test for shape circle layer encountered a declaration exception FAILED Error: Cannot set /home/(...)/my-project providers because it has already been called at new BaseException (/home/(...)/my-project/node_modules/@angular/core/src/facade/exceptions.js:17:23) at Object.setBaseTestProviders (/home/(...)/my-project/node_modules/@angular/core/testing/test_injector.js:74:15) ```

It seems that several tests that set base test providers (TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS) can't be executed at the same time.

Does anyone have this problem? Thanks very much!

1
You only need to setBaseTestProviders once, try moving this setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); to the karma entry file - teleaziz
This is a good example of a karma entry file github.com/AngularClass/angular2-webpack-starter/blob/… - teleaziz

1 Answers

1
votes

As @teleaziz suggested, you should do this only once. So such processing needs to be moved into the karma-test-shim.js file. Here is a sample:

System.import('@angular/platform-browser/src/browser/browser_adapter')
  .then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
  .then(function() {
    return Promise.all([
      System.import('@angular/core/testing'),
      System.import('@angular/platform-browser-dynamic/testing/browser')
    ]);
  })
  .then(function(modules) {
    var testing = modules[0];
    var testingBrowser = modules[1];

    testing.setBaseTestProviders(
       testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
       testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
    })
  .then(function() { return Promise.all(resolveTestFiles()); })
  .then(function() { __karma__.start(); }, function(error) { __karma__.error(error.stack || error); });