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.

Why use "done" in Testing services doc

See original GitHub issue

Description

In the Testing services doc: https://angular.io/guide/testing-services, the example uses done

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

There are situations where done can lead to unnecessary unit test delays, as described here: https://www.piotrl.net/angular-testing-avoid-done/

We can avoid using done by writing the test this way:

  it('#getObservableValue should return value from observable', () => {
    let result;
    service.getObservableValue().subscribe((value) => {
      result = value;
    });
    expect(result).toBe('observable value');
  });

And if service.getObservableValue() is asynchronous, we can use fakeAsync:

  it('#getObservableValue should return value from observable', fakeAsync(() => {
    let result;
    service.getObservableValue().subscribe((value) => {
      result = value;
    });
    tick(); // or tick(whatevertimeneeded); whatevertimeneeded being a number representing milliseconds
    expect(result).toBe('observable value');
  }));

I think done should be avoided in Angular unit tests since we have better alternatives. I am curious about what the Angular team and the Angular community think.

What is the affected URL?

https://angular.io/guide/testing-services

Please provide the steps to reproduce the issue

take a look at the doc

Please provide the expected behavior vs the actual behavior you encountered

don’t use done

Please provide a screenshot if possible

image

Please provide the exception or error you saw

No response

Is this a browser-specific issue? If so, please specify the device, browser, and version.

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Airbladercommented, Jan 20, 2022

I didn’t want to start a bigger discussion here, so I’ll refrain from driving that further; but just an unopinionated note that there is precedence for your suggestion in Angular already, see https://angular.io/api/core/testing/fakeAsync#example.

For the sake of beginners I’d personally like to see there being utilities such as a function that subscribes to an observable and collects all values and returns them. Ideally this could detect that it is run in fakeAsync and throw a helpful error otherwise; that way the problematic pattern is hidden from the user, it looks better in code and it is protected against usage outside fakeAsync. But such an idea is outside the scope of this issue.

1reaction
Airbladercommented, Jan 20, 2022

@dereklin Well, exactly the version you are proposing to use. It works only if the observable is synchronous, which observables typically are not, and will only be in tests due to fakeAsync. Outside of tests, writing the code this way is essentially always wrong, and there are hundreds of questions on Stack Overflow about it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Software Testing Documentation Guide (Why It's Important)
In general, software testing documentation is “It can be done only by the person who has free time”. We need to change this...
Read more >
What Is Test Documentation and Why Do We Need It?
A test plan describes all the test activities within one project. Here, you can find information about everything a software tester or a...
Read more >
Software Testing Documentation: Types & Examples - PFLB
Testing documentation is beneficial to product owners, testers, and developers. Whenever there is a shadow of misunderstanding, stakeholders and participants ...
Read more >
Test Documentation in Software Testing (Example) - Guru99
It helps the testing team to estimate testing effort needed, test coverage, resource tracking, execution progress, etc. It is a complete suite ...
Read more >
Why is Documentation Important in Software Testing? - e-Zest
Now let us examine the importance of documents. Good documentation and planning always leads to better quality software testing and products.
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