What is the difference between fakeAsync and async in Angular testing?

tl;dr In almost all cases, they can be used interchangeably, but using fakeAsync()/tick() combo is preferred unless you need to make an XHR call, in which case you MUST use async()/whenStable() combo, as fakeAsync() does not support XHR calls. For the most part they can be used interchangeably. I can’t think of anything off the … Read more

Angular 2 Final Release Router Unit Test

For testing we now create a testing module using TestBed. We can use the TestBed#configureTestingModule and pass a metadata object to it the same way we would pass to @NgModule beforeEach(() => { TestBed.configureTestingModule({ imports: [ /* modules to import */ ], providers: [ /* add providers */ ], declarations: [ /* components, directives, and … Read more

Error: supportsScrollBehavior is not declared configurable

It is not possible anymore to spy on individually exported functions. https://github.com/jasmine/jasmine/issues/1414 There are some workarounds that might work, but there isn’t a “works for all” solution. Quoting from above link: Actually setting “module”: “commonjs” in tsconfig.json for tests fixes this issue and you can use spyOn again. For me, this didn’t work. Jasmine needs … Read more

Angular 2 Testing – Async function call – when to use

async will not allow the next test to start until the async finishes all its tasks. What async does is wrap the callback in a Zone, where all asynchronous tasks (e.g. setTimeout) are tracked. Once all the asynchronous tasks are complete, then the async completes. If you have ever worked with Jasmine outside out Angular, … Read more

Testing – Can’t resolve all parameters for (ClassName)

Using Jest? In case anyone gets here AND you’re using Jest to test your Angular app (hopefully we’re a growing minority), you will run into this error if you are not emitting decorators (“emitDecoratorMetadata”:true). You’ll need to update your tsconfig.spec.json file so it looks like: { “extends”: “../../tsconfig.json”, “compilerOptions”: { “emitDecoratorMetadata”: true, “outDir”: “../../out-tsc/spec”, “types”: … Read more

Angular 2 Karma Test ‘component-name’ is not a known element

Because in unit tests you want to test the component mostly isolated from other parts of your application, Angular won’t add your module’s dependencies like components, services, etc. by default. So you need to do that manually in your tests. Basically, you have two options here: A) Declare the original NavComponent in the test describe(‘AppComponent’, … Read more