question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SwUpdate breaks unit test

See original GitHub issue

I’m submitting a…

[ x] Bug report (nothing found on Github, nor wider web)

Current behavior

Injecting swUpdate into (app) component breaks unit test of component with No provider for NgswCommChannel error.

Importing NgswCommChannel results in cannot resolve... compiler error.

Full karma/Jasmin error:

Error: StaticInjectorError(DynamicTestModule)[SwUpdate -> NgswCommChannel]: 
  StaticInjectorError(Platform: core)[SwUpdate -> NgswCommChannel]: 
    NullInjectorError: No provider for NgswCommChannel!

Expected behavior

The test framework would generate the component and allow unit testing to proceed.

Minimal reproduction of the problem with instructions

AppModule (excerpt): providers: [ ..., SwUpdate, ... ]

Test setup:

beforeEach(async(() => {
	TestBed.configureTestingModule({
		declarations: [
		        AppComponent
		],
		imports: [
			RouterTestingModule
		],
		providers: [
			...,
                        //NgswCommChannel,
			SwUpdate,
                        ...
		],
		...
	})
	.compileComponents();
}));

beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
});

Failing test: it('can create the component', () => { expect(component).toBeTruthy(); });

What is the motivation / use case for changing the behavior?

Get component unit tests to pass.

Environment


Angular version: 5.2.27

Browser:
- [x ] Chrome (desktop) version 64.0.3282.186
 
For Tooling issues:
- Node version: 6.10.0
- Platform:  Windows 10
- Karma: ~2.0.0.
- Jasmine: ^2.5.38
- angular-cli: 1.7.2

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
michelcvecommented, Nov 1, 2018

Just to clarify it a bit more, importing the ServiceWorkerModule into the test module with enabled: false solves it…E.g.:

imports: […, ServiceWorkerModule.register(‘’, {enabled: false}), …]

I kind of missed that, so perhaps it helps someone else 😉

9reactions
gkalpakcommented, Mar 12, 2018

@evelbulgroz, this sounds to be working as expected. If you are trying to inject SwUpdate, you also need to provide the dependencies that it needs.

You could try importing the ServiceWorkerModule (which provides the necessary services) with enabled: false (since you obviously don’t want to be testing the ServiceWorker in your tests); e.g. import: [ServiceWorkerModule.register('', {enabled: false}).

Alternatively, you could provide a mock version of the service yourself; e.g. {provide: SwUpdate, useValue: mockSwUpdateInstance}. You could try creating mockSwUpdateInstance by instantiating SwUpdate yourself (e.g. new SwUpdate({isEnabled: false} as any)), but I think this will error if you try to interact with it. A more robust solution would be creating a mock object based on the methods that you component interacts with. This makes more sense imo, since in a unit test you want to test the component and not the services it interacts with.

FWIW, here is the mock that we (will) use on angular.io for a similar scenario: https://github.com/alxhub/angular/blob/3ad088d15909e2f5b54ec14f5da87799b0cba8a9/aio/src/app/sw-updates/sw-updates.service.spec.ts#L206-L220

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 7 swUpdate breaks unit testing and cannot be mock ...
For testing a similar service I've manually created an instance per test instead of using the TestBed and passed in a mock swUpdate...
Read more >
SWUpdate: software update for embedded system
SWUpdate is open to talk with back end servers for rolling out software updates. Current version supports the hawkBit server, but other backend...
Read more >
SwUpdate - Angular
Updating a client without reloading can easily result in a broken application due to a version mismatch between the application shell and other...
Read more >
[swupdate] [PATCH 3/3] [meta-swupdate] [v2,3/3] swupdate
Nov 19 17:21:05 blueye systemd[1]: swupdate.service: Unit process 386 ... have the possibility to prepare and test a patch for meta-swupdate without custom ......
Read more >
[Solved]-Upgrading angular from 4.0.0-beta.5 ... - appsloveworld
Coding example for the question Upgrading angular from 4.0.0-beta.5 to 4.0.0 breaks animations and unit tests-angular.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found