Skip to content

Testing Guidelines Confusion #68797

@mylifeandcode

Description

@mylifeandcode

Describe the problem that you experienced

Hi! I recently refactored an entire application to use Vitest instead of Jasmine/Karma, and had some confusion around tests where Observables needed to complete and the return values verified. At one point, the testing documentation stated that, with Vitest, these values should be converted to Promises via firstValueFrom(), and awaited. However, since then, that guidance has disappeared, but there's no replacement/alternative that I could find.

While looking for this now missing guidance, I found a testing guideline advising against using dependency mocks (aka test doubles), saying they should be used sparingly. Making use of real service instances in component unit tests seems like a bad idea. And at one point, code to retrieve an injected value is provided, followed immediately by a message informing the user that the aforementioned example is usually not necessary and that there's a simpler way (which, up until this point, had been the standard way, via TestBed.inject()). The page reads as though an intern wrote it and someone else came along later to correct it.

I love Angular and truly appreciate all the work that's been done to modernize testing with Vitest. But the guidance can, at times, be confusing, or non-existent. After migrating an entire application's unit tests to convert the Observables to Promises, I'm now wondering if this is not the correct approach.

Enter the URL of the topic with the problem

https://angular.dev/guide/testing/components-scenarios#provide-service-test-doubles

Describe what you were looking for in the documentation

The correct approach for how to verify Observable results using Vitest.
Clarity on best practices.

Describe the actions that led you to experience the problem

I had prepared a slide deck to show developers information about our upcoming migration, across all of our applications, from Jasmine/Karma to Vitest, and wanting to have the actual URL for the page where the guidance about converting those Observables to Promises in the specs was spelled out.

Describe what you want to experience that would fix the problem

Updated, stable, unchanging (within reason, of course) guidelines and information about the correct approaches for verifying Observables in Vitest tests. Also, the part that advises that mocks should only be used sparingly in unit tests is (no offense intended) mind boggling.

Add a screenshot if that helps illustrate the problem

No response

If this problem caused an exception or error, please paste it here


If the problem is browser-specific, please specify the device, OS, browser, and version


Provide any additional information here in as much as detail as you can


Metadata

Metadata

Assignees

No one assigned

    Labels

    area: docsRelated to the documentationarea: testingIssues related to Angular testing features, such as TestBed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions