Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivy: unit test failed due to 'moduleDef.imports is not iterable'. #34623

Closed
zalic0814 opened this issue Jan 3, 2020 · 0 comments
Closed

Ivy: unit test failed due to 'moduleDef.imports is not iterable'. #34623

zalic0814 opened this issue Jan 3, 2020 · 0 comments

Comments

@zalic0814
Copy link

@zalic0814 zalic0814 commented Jan 3, 2020

馃悶 bug report

Affected Package

The issue is caused by package @angular/core/testing

Is this a regression?

Not sure, this is my first time to use Ivy.
The error is occurred in Ivy mode, but not in normal mode.

Description

When running unit test using overrideProvider(), compileComponents(), and createComponent(), there is an error occurred:

moduleDef.imports is not iterable.

This error requires three conditions to occur:

  1. Iyv mode.
  2. Import a module containing a function as imports, such as HttpClientTestingModule.
  3. Using overrideProvider() and re-compile components.

Spec Code

beforeEach(async(() => {
    TestBed.configureTestingModule({
      // Import the module containing a function in imports.
      imports: [HttpClientTestingModule],
      declarations: [
        AppComponent
      ],
      // Initialization for providers.
      providers: [{ provide: InjectedString, useValue: { value: 'Initial...' } }],
    }).compileComponents();
  }));

  it('overrideProvider with HttpClientTestingModule should work', async(() => {
    // Override provider with value 'Success!'.
    TestBed.overrideProvider(InjectedString, { useValue: { value: 'Success!' } }).compileComponents();

    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('.injected').textContent).toContain('Success!');
  }));

tsconfig.spec.json

{
 "extends": "./tsconfig.json",
 "compilerOptions": {
   "outDir": "./out-tsc/spec",
   "types": [
     "jasmine",
     "node"
   ]
 },
 "files": [
   "src/test.ts",
   "src/polyfills.ts"
 ],
 "include": [
   "src/**/*.spec.ts",
   "src/**/*.d.ts"
 ],
 "angularCompilerOptions": {
   // If disable Ivy, the testing of overrideProvider() will pass.
   "enableIvy": true
 }
}

馃敩 Minimal Reproduction

https://github.com/zalic0814/ivy-override-provider

馃敟 Exception or Error

Error


Failed: moduleDef.imports is not iterable

at 
at R3TestBedCompiler.applyProviderOverridesToModule (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:2104:1)
at R3TestBedCompiler.applyProviderOverridesToModule (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:2105:1)
at R3TestBedCompiler.compileTestModule (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:2386:1)
at R3TestBedCompiler.finalize (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1874:1)
at TestBedRender3.get testModuleRef [as testModuleRef] (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3249:1)
at TestBedRender3.inject (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3110:1)
at TestBedRender3.createComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3200:1)
at Function.createComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3013:1)
at UserContext. (http://localhost:9876/_karma_webpack_/src/app/app.component.spec.ts:22:29)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)

馃實 Your Environment

Image 9

@crisbeto crisbeto self-assigned this Jan 3, 2020
@crisbeto crisbeto added the comp: ivy label Jan 3, 2020
@ngbot ngbot bot added this to the needsTriage milestone Jan 3, 2020
crisbeto added a commit to crisbeto/angular that referenced this issue Jan 3, 2020
鈥g provider

Fixes an error that is thrown when a provider is overridden in `TestBed`, if the module definition of one of the imported modules uses a function for the `imports` that is set via `setNgModuleScope`. The problem was that we have a `for...of` loop that assumes that the imports are an array, but they can also be a function. This was handled correctly in other places, but this one was missed.

Note that the above-mentioned error is only thrown at runtime when the code is transpiled to es6. In es5 TS generates a call to a helper that handles the error silently so the attached unit test only fails in es6.

Fixes angular#34623.
crisbeto added a commit to crisbeto/angular that referenced this issue Jan 3, 2020
鈥g provider

Fixes an error that is thrown when a provider is overridden in `TestBed`, if the module definition of one of the imported modules uses a function for the `imports` that is set via `setNgModuleScope`. The problem was that we have a `for...of` loop that assumes that the imports are an array, but they can also be a function. This was handled correctly in other places, but this one was missed.

Note that the above-mentioned error is only thrown at runtime when the code is transpiled to es6. In es5 TS generates a call to a helper that handles the error silently so the attached unit test only fails in es6.

Fixes angular#34623.
alxhub added a commit that referenced this issue Jan 6, 2020
鈥g provider (#34629)

Fixes an error that is thrown when a provider is overridden in `TestBed`, if the module definition of one of the imported modules uses a function for the `imports` that is set via `setNgModuleScope`. The problem was that we have a `for...of` loop that assumes that the imports are an array, but they can also be a function. This was handled correctly in other places, but this one was missed.

Note that the above-mentioned error is only thrown at runtime when the code is transpiled to es6. In es5 TS generates a call to a helper that handles the error silently so the attached unit test only fails in es6.

Fixes #34623.

PR Close #34629
@alxhub alxhub closed this in 5b864ed Jan 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can鈥檛 perform that action at this time.