Error: Unexpected value 'undefined' declared by the module 'DynamicTestModule'

Viewed 5329

In my Angular/Typescript/Webpack project, I was rewriting unit tests after code modifications.

The symptom of the issue was an error when running a very basic component test: "Error: Unexpected value 'undefined' declared by the module 'DynamicTestModule'"

In order to debug the issue, I wound up stripping out all dependencies from the constructor of the component and essentially all code. The component did nothing and yet I still got the error. It couldn't be a circular dependency because there were no dependencies.

The folder files were:

--profile
----profile.component.ts
----profile.component.spec.ts
----profile.component.html
----index.ts (barrel)

The component (with most meaningful code removed in order to figure out problem) is:

import { Component } from '@angular/core';
@Component({
    selector: 'profile',
    template: `<h1></h1>` 
})
export class ProfileComponent {
    title = 'Test Tour of Heroes';
    constructor(){}
}

And the spec is:

import { NO_ERRORS_SCHEMA, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } 
  from '@angular/core/testing';

import { ProfileComponent } from './profile.component';

console.log(ProfileComponent);

fdescribe('Component: Jasmine Spy Test', () => { 

  console.log(ProfileComponent);

  let fixture: ComponentFixture<ProfileComponent>;
  let component: ProfileComponent;
  let fixture2: ComponentFixture<ProfileComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ProfileComponent]
    }).compileComponents();

    fixture = TestBed.createComponent(ProfileComponent);
    component = fixture.componentInstance;
  });
  it(`should create instance of objects`, () => {
    expect(1).toBe(1);
  });
});

Versions:

"@angular/core": "^4.0.0",
"karma": "~1.4.1"
"webpack": "^2.3.3"

There are lots of other packages but I don't think that was the issue.

I was using VisualCode 1.14.0 (1.14.0).

2 Answers

I also faced this issue. The component I was testing was not declared in any module. I had commented out the code in the module, after removing the comments, the error was fixed.

Related