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.

Helpers: nextTick/flushPromises

See original GitHub issue

Follwing up on a discussion on Discord, I wanted to open this issue to gather feedback, experiences, and opinions on how to handle async testing and the usage of nextTick and flushPromises.

Differences between nextTick and flushPromises

I couldn’t explain it any better than here: https://github.com/testing-library/react-testing-library/issues/11

Current state of the art

We’re currently returning nextTick on several methods.

I think I was expecting to get away with await wrapper.something() most of the time.

by @cexbrayat. Turns out you might need an indeterminate number of nextTick calls to get everything up to date, so you end up using flushPromises. Happened the same to me while writing docs for HTTP requests.

Also, there’s this question by @JessicaSachs that is worth checking/considering:

nextTick might safely be able to be the default? I don’t remember if this prohibits testing of async created hooks (I’m thinking in Vue 2)

What are the alternatives?

1. “automagically” call flushPromises

The pros is that users have one less thing to worry about – and a thing which is quite low-level.

On the other hand, we might lose the ability to test intermediate states (such as “Loading states”) before the promise resolved/rejected. If that’s the case, this is a no-go.

2. Leave it as is and rely on docs

Pros is that current implementation of VTU-next would work.

Cons is that we all know that explaining something in a documentation does not translate well with “our users will know about this and will take it into account” 😅

3. Create a tool (method? helper?) to wrap nextTick and flushPromises

Not sure about the shape of that tool, but something along the lines of waitFor. Other alternatives mentioned on Discord are wrapper.findAsync.


disclaimer: this issue is meant to serve as a place to discuss alternatives. After that, if we reach a consensus, I open to write a proper RFC with the suggested proposal 👍

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:29 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
lmiller1990commented, Mar 16, 2022

We would probably include waitFor if Vue Testing Library didn’t already do that (which extends and builds on top of Test Utils, like React Testing Library builds on @react/test-utils. In general, I think Testing Library with the waitFor method is probably a more ergonomic alternative to Vue Test Utils for cases like this.

2reactions
cexbrayatcommented, Mar 15, 2022

Maybe my comment was misleading, I was talking about our (VTU) documentation.

As I said, it’s ok if you have a strong opinion on how that needs to be handled in your library: the point is the setTimeout with a 0 delay triggers the need for a second flushPromises, and other libs made different choices making it unnecessary. I understand that as a maintainer you don’t want to change that and introduce potential breaking changes (it’s the same for in VTU). We just need to inform developers using msw with VTU that a workaround exists in our docs. If anyone wants to open a PR on VTU docs, we’ll gladly review it 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Helpers: nextTick/flushPromises #137 - vuejs/test-utils - GitHub
Follwing up on a discussion on Discord, I wanted to open this issue to gather feedback, experiences, and opinions on how to handle...
Read more >
How to flush promises since Jest 26+ - Stack Overflow
For my test I am using the flushPromises method as you described. const flushPromises = () => new Promise(setImmediate);.
Read more >
Async Methods - Testing Library
Several utilities are provided for dealing with asynchronous code. These can be useful to wait for an element to appear or disappear in...
Read more >
Help! My Vue.js Tests are Breaking! - Highland Solutions
The original way to do this uses the Vue.js function nextTick([callback, context]) which optionally takes a callback and context that runs ...
Read more >
Testing Asynchronous Behavior - Vue Test Utils
One way is to use await Vue.nextTick() , but an easier and cleaner way is to just await the method that you mutated...
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