Msw: Error: Response for preflight has invalid HTTP status code 404 when running in GitHub action

Created on 8 Jan 2021  路  4Comments  路  Source: mswjs/msw

Environment

| Name | Version |
| ---- | ------- |
| msw | 0.25.0 |
| node | 14.15.1 |
| OS | Ubuntu 18.04 |

Request handlers

server.ts

import { setupServer } from 'msw/node';
import { handlers } from './handlers';

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

handlers.ts

import buildResolvers from './buildResolvers';

export const handlers = [
  ...buildResolvers,
];

buildResolver.ts

import { rest } from 'msw';
import { createWebBuildMock } from '../utils/mockInterfaces';

export const buildSuccessResolver = rest.get('http://localhost:7000/apps/:appId', (req, res, ctx) => {
  return res(
    ctx.json({
      data: {
         // insert data
      },
    })
  );
});

export default [buildSuccessResolver];

and I am using fetch to make the request

Actual request

// Example of making a request. Provide your code here.
fetch('???').then((res) => res.json())

Current behavior

Unit tests using Jest in a create-react-app application with jest-environment-jsdom-sixteen pass when running locally on macOS Big Sur and Ubuntu 20.04, but they fail with an error when running the tests in CI with GitHub actions using ubuntu-latest which is 18.04.

Error:

    console.error node_modules/@testing-library/react/dist/act-compat.js:52
      Error: Error: Response for preflight has invalid HTTP status code 404
          at Object.dispatchError (/home/runner/work/project/project/node_modules/jest-environment-jsdom-sixteen/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
...
      TypeError: Network request failed

Expected behavior

If the tests work locally, then they should work in CI.

bug node

Most helpful comment

I'm going to go ahead and close this issue. My problem ended up being on my end and not msw. In our CI pipeline we were replacing the API URL so the API requests in our tests didn't match.

All 4 comments

Hi Devon! Thanks for creating this issue, do you have a reproduction?
@kettanaito We could also set up a GitHub actions CI for our examples repository (next to the CircleCI pipeline), if this could be helpful.

This is in an older create-react-app repo that has been updated several times, and since creating this issue I attempted to recreate this in a new CRA repo but it worked as expected. I'll try and see if I can figure out if this is a dependency version issue or something else.

I'm going to go ahead and close this issue. My problem ended up being on my end and not msw. In our CI pipeline we were replacing the API URL so the API requests in our tests didn't match.

Thanks for the update 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hauptrolle picture hauptrolle  路  4Comments

slowselfip picture slowselfip  路  3Comments

kettanaito picture kettanaito  路  3Comments

ff6347 picture ff6347  路  3Comments

lukesmurray picture lukesmurray  路  3Comments