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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TestBed configs with missing declarations, imports and providers #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brendoncaulkins
Copy link

@brendoncaulkins brendoncaulkins commented Oct 19, 2019

Resolves #1

The errors detailed in #1 indicate that components, modules or services are missing from the TestBed configurations.

In short, the Angular TestBed makes zero assumptions, and needs you to wire in all child components as declarations, any dependent modules you need to import (like routers, layout modules, etc) and any Dependency Injected services as providers. Doing this for the components and services you write in your own app is fairly easy, but sometimes you inherit some messy configurations from third-party libraries.

In this case, the use of the Nebular UI means there's a load of extra stuff you need to provide in order for that to work in your testing environment. I found some examples of how the Nebular dev team tests their own stuff and borrowed some of their strategies. Once I had this working in the app.component.spec.ts file, I moved the majority into a separate module (nebular-testing.module.ts so it is easily packaged and re-usable across any of your future spec files.

Hopefully, this unblocks your testing efforts. If you have any other Angular or testing related questions, please don't hesitate to reach out here or on twitter.

Comment on lines -28 to -34

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('.content span').textContent).toContain('myapp app is running!');
});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was no longer needed since the app doesn't render "myapp app is running" anywhere.

Comment on lines +35 to +36
{ provide: NB_DOCUMENT, useValue: document },
{ provide: NB_WINDOW, useValue: window },
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like using the real document and window here, but I'm unsure what else to do. Creating mock objects to use instead would be better, but who knows what Nebular is reaching for on the Document or Window, so that's a rabbit hole for another day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unit test aren't working
1 participant