0

I am using MSW for my React app and am getting the following error when trying to run in mock mode; Error occurred while trying to proxy request /api/myApi from localhost:3000 to { port: 8080 } (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors) I am trying to use both for testing and as dev api server (mock data)

Not sure if there is additional config needed for proxying the request when running in mock mode.

Below is my command in package.json

"start:mock": "cross-env NODE_ENV=mock webpack serve --progress --config config/webpack.local.js",

Below is the code in src/index.js

import * as serviceWorker from './serviceWorker';

if (process.env.NODE_ENV === 'mock') {
  const { worker } = require('./browser')
  worker.start()
}

//src/browser.js

import { setupWorker } from 'msw'
import { handlers } from './testServer'
// This configures a Service Worker with the given request handlers.
export const worker = setupWorker(...handlers)

Below is src/testServer.js

import "whatwg-fetch";
import { rest } from "msw";
import { setupServer } from "msw/node";

import data from "./data";

const handlers = [
  rest.get("http://localhost/api/myApi", (req, res, ctx) => {
    return res(ctx.status(200), ctx.json(data));
  })
];


// This configures a request mocking server with the given request handlers.
const server = setupServer(...handlers);

beforeAll(() => server.listen());
afterAll(() => server.close());
afterEach(() => server.resetHandlers());

export { server, rest, handlers};
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • Can you please describe your expectations? Do you expect `/api/myApi` to be intercepted and mocked in Jest or in a browser? Both? A reproduction repository would be ideal if you wish to get this question resolved. – kettanaito Jul 01 '21 at 19:54
  • Yes, i want it to be intercepted in the browser – copenndthagen Jul 02 '21 at 05:53

0 Answers0