GithubHelp home page GithubHelp logo

Comments (4)

slaweet avatar slaweet commented on August 11, 2024 1

@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.

kettanaito avatar kettanaito commented on August 11, 2024

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.

slaweet avatar slaweet commented on August 11, 2024

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.

kettanaito avatar kettanaito commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.