Questions tagged [msw]

Mock Service Worker is an API mocking library that uses Service Worker API to intercept actual requests.

184 questions
1
vote
1 answer

MSW with Jest (confusion)

I am trying to test a component that calls an API, i came across MSW and thought this would be great. const server = setupServer( rest.get('http://localhost:3001/vote/test-slug', (req, res, ctx) => res(ctx.delay(500), ctx.status(200),…
Andy
  • 679
  • 2
  • 10
  • 25
1
vote
1 answer

How to make mock service worker running in node + jsdom (for tests) receive cross-domain cookies?

I've got a reproduction repo with Jest + MSW, and I'm trying get MSW to receive cross-domain cookies. By default, JSDOM loads on http://localhost. When sending fetch requests to this domain & MSW listens on this domain too, all is fine, the cookies…
aryzing
  • 4,982
  • 7
  • 39
  • 42
1
vote
0 answers

How can I use Mock Service Worker with AWS App Sync GraphQL Queries?

I have this query but it's not getting called by MSW. const response = (await API.graphql(graphqlOperation(listNotarys, { filter: filterInput }))) as { data: unknown; }; My handler looks like the following: graphql.query("ListNotarys",…
1
vote
2 answers

Testing with Jest while using MSW and RTK Query leads to strange error in test

I have been spending most of the day trying to sort out this insanely annoying bug. I am using redux-toolkit, MSW, RTK query, and React Testing Libary and am currently busy writing an integration test that tests a simple login flow. The problem I…
1
vote
2 answers

What should be the public directory for msw initialization when using webpack-dev-server?

I had read the Where is my "public" directory? documentation. A public directory is usually a root directory of your server (i.e. ./build, ./public, or ./dist). This directory is often committed to Git, and so should be the Mock Service Worker. I…
Lin Du
  • 88,126
  • 95
  • 281
  • 483
1
vote
1 answer

How to mock fetch in custom hook with Mock Service Worker?

I want to test my application that has a user input a query that sends a request to the Google Books API, then the response is displayed. I've separated the data fetching logic into its own custom hook. I want to mock the fetch with Mock Service…
ln09nv2
  • 965
  • 4
  • 19
  • 35
1
vote
1 answer

Getting error in browser - Failed to register a ServiceWorker for scope

I am trying to use MSW for running my React app with mock data. Below is my index.js; import React from 'react'; import * as serviceWorker from './serviceWorker'; if (process.env.NODE_ENV === 'development') { const { worker } =…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
1
vote
1 answer

Unable use mock servise worker in react testing library

Instead of mocking an axios request, I try to test the component using msw, but after the request I don't get the visibility of the content in the component, what am I doing wrong? My component import React, {useEffect, useState} from 'react' import…
1
vote
1 answer

@testing-library/react with msw/node; window.fetch not returning body on POST

I created a simple application using create-react-app. I am now trying to write some automated test cases using @testing-library and Mock Service Worker. For reasons unknown to me when I POST a request using Window.fetch (or just fetch) the body of…
316
  • 11
  • 3
1
vote
1 answer

How to use service worker library (MSW) with Parcel bundler

I have a small TypeScript React project that I bundle using Parcel. I wanted to add MSW for mocking server requests, but I have troubles getting it to work. I have a very minimalistic Parcel setup: "scripts": { "start": "parcel index.html", …
zorza
  • 2,814
  • 3
  • 27
  • 41
1
vote
1 answer

How to make assertions on graphql query variables when using Mock Service Worker?

When we are mocking out a graphql query with a mock service worker (MSW), we want to assert that the variables passed to the query have certain values. This goes beyond the type validation with the typescript typings. We are using jest with MSW. Do…
Josh Birdwell
  • 745
  • 4
  • 21
0
votes
1 answer

React MSW - my code does not mock and actual API gets called

I am trying to test the below component App.js. The component is a simple one that sends a GET request using fetch and displays the result. import React, { useState, useEffect } from "react"; const App = () => { const [users, setUsers] =…
0
votes
1 answer

What's the difference between the MSW Storybook Addon and storybook-addon-mock?

I need to mock API requests in my Storybook environment, I found 2 libraries for the job, MSW Storybook Addon and storybook-addon-mock, they all look the same to me, I'm not sure which one to use and why use one over another one, any difference…
Tonni
  • 94
  • 2
  • 7
0
votes
1 answer

MSW (mock service worker) is not setting the body in mocked server response

I am trying to test a request handler that returns a stream with MSW, however, when testing, MSW does not appear to be setting the response body as expected. Here is an example of my code // example of request method const apiRequest = async (route)…
Marcus Ruddick
  • 9,795
  • 7
  • 28
  • 43
0
votes
2 answers

Is it possible to mock a file with MSW (mock service worker)

I've been using MSW to intercept REST api calls and mock responses. However I am wanting to intercept a JS file (e.g. "/some-file.js") and override its content. Is this possible using MSW handlers?
Carlton
  • 531
  • 1
  • 5
  • 19