Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Testing - Async function call - when to use

When do you use the async function in the TestBed when testing in Angular 2?

When do you use this?

 beforeEach(() => {         TestBed.configureTestingModule({             declarations: [MyModule],             schemas: [NO_ERRORS_SCHEMA],         });     }); 

And when do you use this?

beforeEach(async(() => {     TestBed.configureTestingModule({         declarations: [MyModule],         schemas: [NO_ERRORS_SCHEMA],     }); })); 

Can anyone enlighten me on this ?

like image 406
xiotee Avatar asked Oct 19 '16 09:10

xiotee


People also ask

What is async in Angular testing?

We wrap our test spec function in another function called async . 2. We place the tests we need to run after the isAuthenticated promise resolves inside this function. This async function executes the code inside its body in a special async test zone. This intercepts and keeps track of all promises created in its body.

What is the difference between async ()' and fakeAsync?

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.

How do you wait for async to finish in Jasmine?

Usually, the most convenient way to write async tests is to use async / await . async functions implicitly return a promise. Jasmine will wait until the returned promise is either resolved or rejected before moving on to the next thing in the queue.


1 Answers

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, you may have seen done being passed to the callback

it('..', function(done) {   someAsyncAction().then(() => {     expect(something).toBe(something);     done();   }); }); 

Here, this is native Jasmine, where we tell Jasmine that this test should delay completion until we call done(). If we didn't call done() and instead did this:

it('..', function() {   someAsyncAction().then(() => {     expect(something).toBe(something);   }); }); 

The test would complete even before the expectation, because the promise resolves after the test is finished executing the synchronous tasks.

With Angular (in a Jasmine environment), Angular will actually call done behind the scenes when we use async. It will keep track of all the asynchronous tasks in the Zone, and when they are all finished, done will be called behind the scenes.

In your particular case with the TestBed configuration, you would use this generally when you want to compileComponents. I rarely run into a situation in which I would have to call it otherwise

beforeEach(async(() => {    TestBed.configureTestingModule({      declarations: [MyModule],      schemas: [NO_ERRORS_SCHEMA],    })    .compileComponent().then(() => {       fixture = TestBed.createComponent(TestComponent);    }); })); 

When testing a component that uses templateUrl (if you are not using webpack), then Angular needs to make an XHR request to get the template, so the compilation of the component would be asynchronous. So we should wait until it resolves before continuing testing.

like image 140
Paul Samsotha Avatar answered Sep 20 '22 08:09

Paul Samsotha