• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How does one mock a browser fetch call in a unit test using vitest?

 
Marshal
Posts: 5654
329
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, I'm trying to write a unit test for a TypeScript function that makes a couple of HTTP calls to an external service, and I'd like my test to assert that the calls are made in the right order with the right payload particularly because the second call content depends on the response of the first.

The function under test looks roughly like this. The first call gets a json object containing a pre-signed URL for the client to make a PUT request to.

As far as my understanding of the browser environment goes, fetch appears to be kinda built in and always there, so how can I get a handle on it to inject my own responses and assert on the call parameters?

The project this belongs to is using vitest. I have a similar question for calls made using the openapi-fetch library but let's start with fetch and stick with one question at a time.

Cheers, Tim
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ick. I hate Javascript's inside-out thread model.

The biggest problem with await-promise etc. is that the actual code execution doesn't literally wait, like in sane threading systems. The await means "come back later and do this".  

You'd probably suffer less if you did an outer "login" request and did an inner work request inside the outer request's callback.

Barf.
 
Tim Cooke
Marshal
Posts: 5654
329
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The async/await/promise stuff is definitely something I don't have a clear mental model of. In this case I'm following existing patterns that are in the codebase written by my colleagues who I have no doubt are more skilled in JS / TS than I am.
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic