0

I've been using MSW since v0.35.0. Recently I updated it to v0.40.1 and now it seems like MSW is not intercepting the request to the servers and I'm getting the following error.

Here is my test code.

import axios from 'axios';
import { rest } from 'msw';
import { setupServer } from 'msw/node';

const path = 'login/';
const accessToken = 'AccessTokenValue';
const correctCredential = { email: 'test@email.com', password: 'password' };

const server = setupServer(
  rest.post(path, (req, res, ctx) => {
    return res(ctx.status(200), ctx.json({ data: { access: accessToken } }));
  }),
);

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

describe('Login', () => {
  test('Case: Success', async () => {
    let token = '';
    await axios
      .post('https://test.com' + path, correctCredential)
      .then((response) => (token = response.data.data.access));
    expect(token).toBe(accessToken);
  });
});

And this is the error I get.

Error: Request failed with status code 400

at createError (<my_local_path>)
at settle (<my_local_path>)
at IncomingMessage.handleStreamEnd (<my_local_path>)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)

Here are the versions of the other packages I use.

  • jest: 27.0.6
  • axios: 0.25.0

I read Examples from MSW and I don't see any problem with my implementation.

UK4
  • 460
  • 7
  • 25

1 Answers1

0

Perhaps this issue is relevant. https://github.com/mswjs/msw/issues/1125

This issues was fixed 5 days ago, so I believe a corrected version will be released in the near future.

btw, downgrading to 0.36.8 is temporary solution.

  • Downgrading didn't work for me. I upgraded to version 0.44.2 and now it's working. – UK4 Jul 26 '22 at 09:28