Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular service unit test DoneFn

I'm following the angular official doc and I can see this code:

it("#getObservableValue should return value from observable", (done: DoneFn) => {
    service.getObservableValue().subscribe(value => {
      expect(value).toBe("observable value");
      done();
    });
  });

I'm wondering where DoneFn comes from because I've no error either import for the typing.

like image 474
user3887366 Avatar asked Dec 08 '18 18:12

user3887366


People also ask

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

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.

What is tick () in Angular?

The tick() function simulates the asynchronous passage of time for the timers in the fakeAsync zone in Angular test.

How is unit testing done in Angular?

If Angular CLI is used to manage the Angular projects, it will automatically support Jasmine and Karma Configurations. All you need in order to test your application is to type the command ng test. As far as possible, run tests on real browsers and devices to ensure that software is verified under real user conditions.

What is SpyOn in Angular unit testing?

Test the Component logic using SpyOn. SpyOn is a Jasmine feature that allows dynamically intercepting the calls to a function and change its result. This example shows how spyOn works, even if we are still mocking up our service.


1 Answers

If you follow the Interface definition you will see that it is under:

node_modules/@types/jasmine/index.d.ts

/** Action method that should be called when the async work is complete */
interface DoneFn extends Function {
    (): void;

    /** fails the spec and indicates that it has completed. If the message is an Error, Error.message is used */
    fail: (message?: Error | string) => void;
}

You don't need to import or use it, it's mostly for reference. I am not sure how @types exactly work but I suppose if there is a @types typing within the project, node knows how to find the definition as they are all indexed in this one folder.

UPDATE:

I found out this is being configured by tsconfig.json

"typeRoots": [
  "node_modules/@types"
],
like image 77
mchl18 Avatar answered Sep 28 '22 15:09

mchl18