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.

Assert in between to async actions

See original GitHub issue

I have a scenario where when the screen (component) I’m testing makes an API request on initial render and upon a successful result queries the async storage for a stored value. Obviously, both methods are mocked.

I want to test the message shown to the user on-screen when these two things are happening is correct.

const { queryByText, queryByA11yRole } = setup();
await waitForElementToBeRemoved(() => queryByText('Checking API...'));
expect(queryByText('Checking async storage...')).toBeTruthy();
// asserting whether the correct methods were called

This is how I try to test this. But no matter what configuration I use for waitForElementToBeRemoved such as { timeout:4500, interval:1 } my assertion for the second message always fails, because, by then the app has gone past the render where it queries async storage, got the result and done the rest and rendered accordingly.

Asserting whether the correct methods were called is successful but I want to verify the messages are shown to the user as well.

How do I go about testing this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Luk-karcommented, Apr 20, 2021

For this case, you can use waitFor instead. waitFor takes a function and retries until it stops throwing errors.

await waitFor(() => { 
  expect(queryByText('Checking API...')).toBeNull();
  expect(queryByText('Checking async storage...')).toBeTruthy();
});

When I write the following code:

        await waitFor(() => {
            expect(getByText("Some string")).toBeNull();
        });

Got error: Unable to find an element with text: Some string

1reaction
Kamalnrfcommented, Nov 25, 2020

For this case, you can use waitFor instead. waitFor takes a function and retries until it stops throwing errors.

await waitFor(() => { 
  expect(queryByText('Checking API...')).toBeNull();
  expect(queryByText('Checking async storage...')).toBeTruthy();
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - how to assert an async action dispatched inside an ...
another async action that dispatches the previous async action export const postObjectAsync = newObject => dispatch => axios.post(URL, ...
Read more >
Assert.ThrowsAsync - NUnit Docs
The Assert.ThrowsAsync is the async equivalent to Assert.Throws for asynchronous code. See Assert.Throws for more information. Exception Assert.ThrowsAsync(Type ...
Read more >
Difference between Synchronous & Asynchronous Assertion ...
Functional tests are asynchronous on the web. This means that we cannot get the expected changes immediately after a user's actions. For example ......
Read more >
Async Programming - Unit Testing Asynchronous Code
As of this writing, NUnit supports asynchronous code in its verification methods such as Assert.Throws. However, in order to get this to work,...
Read more >
Testing-library: avoid these mistakes in async tests
Good and stable tests should still reliably assert component output against the given input, no matter what happens at the lower levels.
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