Comments (4)
@kettanaito thank you so much for the tips. I already asked in vue-test-utils discord chat, I'll see what answers will I get there.
from mswjs.io.
Hey, @slaweet.
It is not the responsibility of MSW to await responses. You perform an actual request, and you get the mocked response. MSW ensures it captures a request how you described, and responds with whichever mocked data you want. It's your app's responsibility to await the response, there's nothing new in that:
const res = await fetch('https://my.backend.dev')
In the Kent's article the part that awaits something is await screen.findByRole('alert')
. Notice that it's an async assertion, thus there is the await
keyword, which ensures the Promise returned by screen.findByRole
is resolved prior to running the assertion. That part awaits that the given element becomes present on the screen (note: not the response), as the consequence of a successful response.
Please, whenever you face an issue, include the way how you use mocks and how you perform an actual request. This information is automatically asked of you if you file an issue in the MSW repository, which I would highly encourage you to do in case of issues.
from mswjs.io.
Hi @kettanaito,
thank you for a thorough response.
I would very much like to handle it as in Kent's article. But I'm doing it on a project that uses vue-test-utils which doesn't provide such utility, unlike React Testing Library he's using in the article. My way around that was using https://www.npmjs.com/package/flush-promises That worked fine when I was mocking axios, but that doesn't work with MSW.
Looks like I opened an issue in a wrong place. I opened it here, because by migrating to MSW I lost the ability to ensure there are no pending API requests in a test and I still don't quite understand why. Are there any docs that I could read to understand what are all the steps that happen when a jest test calls an API mocked by MSW?
Thank you.
from mswjs.io.
vue-test-utils
reference the Asynchronous testing using axios
as an example. However, it leverages mocking of axios.get
, which I'd not consider a good practice.
I can recommend you to either discuss this with the maintainers of vue-test-utils
to provide a way to await for an element to appear (seems a very common scenario even outside of async testing), or implement your own assertion utility with an interval and a timeout that await for a certain thing to happen.
Take a look at how waitFor
is implemented in React Testing Library: https://github.com/testing-library/dom-testing-library/blob/fe04b503df13be9207769b5d61d67c5532e24636/src/wait-for.js#L21
from mswjs.io.
Related Issues (20)
- /docs/api/response results in 404 HOT 2
- Comments are hard to read in the examples due to font HOT 4
- Add "Mocking authentication" best practice
- Add light theme to the docs HOT 5
- Docs | minor issue with an unactive link HOT 1
- Missing page at "/404/" (GraphQL operation) HOT 3
- Error in onUnhandledRequest example? HOT 1
- Alternative Jest setup for when Request/Response/TextEncoder is not defined HOT 6
- Broken link in doco HOT 2
- Missing `bypass` description in listen() page. HOT 1
- TypeScript error when using async resolver predicates HOT 1
- Feedback: Using local HTTPS HOT 1
- Missing page at "/404/" https://mswjs.io/docs/api/setup-worker/print-handlers HOT 1
- Document how "cy.route()" works HOT 1
- Missing page at "/404/" HOT 2
- Cannot retrieve XMLHttpRequest HOT 1
- Missing page at "/404/" HOT 1
- React Native integration error index.js HOT 2
- MSW AND AXIOS DO NOT WORK TOGETHER IN REACT NATIVE
- Documentation missing on how to integrate with Expo App Router HOT 2
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 mswjs.io.