Questions tagged [msw]

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

184 questions
2
votes
2 answers

mock service worker, sad path test failure

Recently just using Mock Service Worker to test my HTTP requests, i'm looking to test my failure path. My first test passes (happy), but the error i'm receiving for my failure is "Unexpected end of JSON input" It does behave in the way that I would…
fluffy-lionz
  • 81
  • 2
  • 12
2
votes
1 answer

How copy down mockServiceWorker.js file in React application during Webpack build proces to my build folder

I try to setup Mock Service Worker for my React project. My project doesn't use CRA. I want to intercept GraphQL request and mock it's response for creating an end 2 end test with Cypress. How do I copy down mockServiceWorker.js file to my build…
meez
  • 3,783
  • 5
  • 37
  • 91
2
votes
0 answers

Jest fails to await axios promise breaking tests

I'm writing tests with Jest as a test runner, trying to mock a server using the Mock Server Library (https://mswjs.io/). For some reason Jest seems not to await promises of Axios calls. To isolate he problem I created a simple example: a Vue…
Liam Arbel
  • 467
  • 1
  • 7
  • 14
2
votes
1 answer

Testing React-query with MSW with the data passed to a child component

I am using React-query and trying to test it, the data is called in the parent and passed into a child component to render a list of items. I have setup MSW, however when I try my test as: const queryClient = new QueryClient() it('Should give a…
Sole
  • 3,100
  • 14
  • 58
  • 112
2
votes
0 answers

'TypeError [ERR_INVALID_ARG_TYPE]: The "target" argument must be an instance of Buffer or Uint8Array. Received null' : React Testing library using msw

When I run a test code using React testing library with MSW, I got an error buffer.js:210 throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target); ^ TypeError [ERR_INVALID_ARG_TYPE]: The "target" argument must be an instance of…
swk333
  • 21
  • 2
2
votes
1 answer

Best practices with mock service workers and Apollo Client when caching enabled

I have an application that is using Apollo client and when I added MSW I am finding that the cache is returning stale requests with empty responses. The MSW team indicated that best practice is when running tests to clear the cache on every request…
Gus Higuera
  • 95
  • 1
  • 9
1
vote
0 answers

SyntaxError: Unexpected string in msw

Recently i started getting this error for mock service worker (msw) in my react project. Details: Node v16.19.1 vite/3.2.6 linux-x64 node-v16.19.1 react 18.2.0 Checked google, github issues but no good.
targhs
  • 1,477
  • 2
  • 16
  • 29
1
vote
1 answer

Stop msw fron showing warnings about unhandled request?

Is there any way to configure msw to only intercept requests to paths with certain pattern? For example only paths start with "/api". Currently it shows warning every time I navigate to some page of my React app.
CSSer
  • 2,131
  • 2
  • 18
  • 37
1
vote
0 answers

Getting msw's server.use() to work with refactored mock server

In trying to test my App component, I refactored the mock server into a separate file. However server.use() is no longer working. The tests that use server.use() keep failing and throwing the error "TypeError: Cannot read properties of undefined…
1
vote
0 answers

MSW Expect API Error with testing library fail assertion

I'm trying to test a failure on my API call with MSW, testing library and RTK Query. More on the context: I'm testing a hook and I'm calling a function and expect an API fail to assert that an error callback is call. This is the code, the test call…
1
vote
0 answers

(MSW + React-Query + Vitest) Requests occurring in prior test fail in subsequent test

I have built a ReactJS application using Vite, and I am testing my application using Vitest. I make heavy usage of react-query within my application. For mocking network requests during testing, I utilize MSW. I am dealing with a situation where…
pooley1994
  • 723
  • 4
  • 16
1
vote
1 answer

How to intercept requests to Firebase API using msw - mock service worker? React app testing

I encountered problems while testing my demo React app. I use msw to intercept Firebase API requests and I continuously keep getting error in my terminal. Please note, that I have Firebase Authentication implemented so only authorized users in my…
Maciej
  • 31
  • 4
1
vote
1 answer

MSW (Mock Service Worker) + Vite: Uncaught ReferenceError: require is not defined

I'm installing the service following the steps at the official docs: https://mswjs.io/docs/getting-started/install This piece of code: if (process.env.NODE_ENV === 'development') { const { worker } = require('../tests/mocks/browser'); …
Daniel Tkach
  • 576
  • 2
  • 9
  • 18
1
vote
1 answer

ReactJS msw data response for each test weird overlap

I have this code to mock the data response from the API but somehow, the 2nd test is always overwritten by the first one and leads to a test failure. Even using res.once not working. // ...import section test('Should component render successfully',…
Viet Nguyen
  • 2,285
  • 2
  • 26
  • 43
1
vote
0 answers

How to maintain state in Mock Service Worker

I have msw setup to mock a GET and POST endpoint by returning a static constant object. [ rest.get("/something", (r, w, c) => w( c.body({ ... }) ) ), rest.post("/something", (r, w, c) => w( …
radix
  • 53
  • 1
  • 5