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).
The Real Issue VisualCode identified that ProfileComponent was in ./profile.component but the console.log lines printing that out said 'undefined' and no compile/transpile error was thrown.
Notice that there is a separate .html file in the folder but the code doesn't reference it.
ProfileComponent began to have value (no longer undefined) when I renamed the profile.component.html file -- must have been something about the webpack build.
I don't know what is wrong with this particular set of files because there any many folders with this exact same naming convention/setup in this project that do run the tests correctly.
I'm leaving this here in case someone runs across this error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With