reactjsmockingservice-workermswmswjs

SyntaxError: Unexpected string in msw


Recently i started getting this error for mock service worker (msw) in my react project.

Details:

Checked google, github issues but no good.

enter image description here

enter image description here


Solution

  • This error is caused by the graphql package releasing a build that cannot run in the browser. Since MSW depends on graphql, the error surfaces for you when you try to run MSW in the browser.

    The issue has been resolved in the graphql package, although I've heard that it has regressed since then and you have to pin the graphql version in order for it to remain fixed. graphql@16.7.1 seems to be fixing this issue.

    You can use overrides with NPM (and resolutions with Yarn) to pin that module's version in place:

    // package.json
    {
      "overrides": {
        "graphql": "16.7.1"
      }
    }
    

    Hopefully, this won't regress on the latest GraphQL package and you will be able to use MSW without errors.