Comments (4)
Hey, @jmcrthrs. Thanks for reaching out.
When it comes to testing intermediate state (i.e. loading), consider using the ctx.delay()
utility. It allows to set a response delay, thus making the loading state apparent in the UI. Here's how it'd look in your request handlers:
rest.post('/login', (req, res, ctx) => {
return res(ctx.delay(5000), ctx.json({ some: 'response' })
})
The example above would delay the mocked response by 5000ms (5s), making it enough time for your testing framework to capture the loading state.
You can also consider using it like ctx.delay(Infinity)
, which would produce a mocked response that would never resolve.
So I assume there is no need for us to explicitly wait in our tests for the network response?
You're assuming correctly. Most of the test frameworks have a retry mechanism built-in, so the framework would run your assertion repeatedly for an N amount of time until it becomes true. This works beneficial when asserting UI derived from asynchronous operations like HTTP requests.
Certain frameworks may provide you with an API to explicitly await HTTP requests, but you'd have to look such API up in their respective documentations. The usage of such API is up to you.
Let us know if you have any other questions.
from examples.
I have forked this repo and added an assertion after the Login form is submitted, but it can never find the button.
In the html the button is marked with submit-button
as test id
<button data-testid="submit-button" type="submit" className={loading ? "loading" : ""}>
Submit
</button>
While the test is looking for button
as test id
cy.get('[data-testid=button]').should('have.class', 'loading')
from examples.
@timdeschryver Hey Tim, I think perhaps you have an old commit. Can you pull and try again?
The latest commit should be jmcrthrs@8bae3d8
from examples.
So I assume there is no need for us to explicitly wait in our tests for the network response
Cypress has a retry ability, so even when the element isn't visible cypress will retry the query until it finds the element or times out. Because of this, I almost never wait for an HTTP request in my tests.
I have forked this repo and added an assertion after the Login form is submitted, but it can never find the button.
This is because the MSW handler returns the result immediately so React doesn't render the loading indicator.
When you add a delay to the handler (like the one you commented out), it will work.
from examples.
Related Issues (20)
- Remix + MSW + Playwright HOT 1
- Add relative request URL examples to "jest-jsdom"
- Set multiple "msw.workerDirectory" in root-level package.json
- React Native example app is missing
- Asserting request payload (waitForRequest) in v2 documentation HOT 4
- Question: Should we use msw to mock server-side apis based on the request body?
- Vite + React but not Create-React-App + React HOT 2
- use msw-storybook-addon in storybook example
- There is no condition inclusion in main.ts HOT 2
- NetworkError at XMLHttpRequest.send HOT 2
- Jest tests all return ' No element indexed' error HOT 1
- MSW with GrapqhQl fragments
- mockup msw doesn't work HOT 3
- Not intercepting Apollo GQL requests at all HOT 6
- msw can't work in localhost or 127.0.0.1 HOT 1
- Run tests in supported version of Node.js
- Add example which uses Web Test Runner (@web/test-runner)
- Broken link from docs HOT 1
- Question: Is it possible to intercept ALL requests? HOT 3
- Separate with-jest and with-jest-and-jsdom examples HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from examples.