mardi 9 janvier 2018

Angular's prevent reimport technique fails in test

I'm making a CoreModule for my Angular apps by following the NgModule's guide but I get an Error: Can't resolve all parameters for CoreModule: (?). when I use the module in a test.

Here's the CoreModule's code:

import {NgModule, Optional, SkipSelf} from '@angular/core';

@NgModule({})
export class CoreModule {

  constructor(
      @Optional() @SkipSelf() parentModule: CoreModule
  ) {

    // Making sure this is not imported twice.
    // http://ift.tt/2CX36Bd
    if (parentModule) {
      throw new Error(
        CoreModule.name + ` is already loaded. Import it in the app's root module only.`);
    }
  }
}

And here's the test file's content:

import {TestBed, async} from "@angular/core/testing";
import {CoreModule} from "../../../core.module";
import {Injector} from "@angular/core";

describe(`Some test importing CoreModule`, () => {

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        CoreModule,
      ]
    });
  }));


  it(`should work`, () => {

    TestBed.get(Injector);
  });
});

What am I doing wrong?

1 commentaire:

  1. Add the `--aot` flag to `ng serve` in your `start` script in `package.json`, e.g. `"start": "ng serve --aot",`.

    RépondreSupprimer