Msw: [0.19.5] Docs and example now error regarding use of setupWorker in Node

Created on 24 Jun 2020  路  9Comments  路  Source: mswjs/msw

Describe the bug

Not really a bug, just a discrepancy between the documentation/example and the behavior in the latest release. #219 throws an error when you try to use setupWorker in Node (e.g. Jest tests), but the documentation and the rest-react example both call it directly in the mocks file, so you can't use that same file in the browser and Node-based tests.

Environment

  • msw: 0.19.5
  • nodejs: 12.18.1
  • npm: 6.14.5

Browser: N/A

To Reproduce

Steps to reproduce the behavior:

  1. Setup rest-react example
  2. Run npm run test:unit
  3. See error:
    [MSW] Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for NodeJS environment instead.

Expected behavior

I expected the example to work and the steps in the documentation to produce a working structure.

Possible solutions

Is it just a matter of restructuring the docs/example to match the graphql-react-apollo example where the handlers are separate from the setupWorker/setupServer calls?

bug

All 9 comments

Thanks for reporting this, @wKovacs64! Yes, you're right, it's a matter of separating those modules so they don't intersect.

Thank you for the changes to the REST example! The build passes, which means example is functional.

That fixed the example, but I think the docs still lead you down the path that will trigger this error. Not sure if/how you want to update them?

Are you speaking about the new documentation? I believe it separates the handlers/browser/server exactly as we had in the examples.

There's also an outdated docs on GitBook, I'd kindly ask not to refer to them anymore. We are planning on deleting them once most of the people get used to the new docs.

I was referring to the new docs, yes. For example, the "Getting Started > Defining mocks" section begins with creating mocks.js which is then later amended in "Integrate > Browser" section (under "Start the worker") to include the setupWorker call. That works fine in isolation, but assuming the reader moves on to the next section ("Integrate > Node"), they will likely get the error if they try to reuse mocks.js (one of the features of MSW is to reuse the mock definitions, so I think people will do this - I know I did).

You are completely right 馃う I didn't realize it suggests integration in the same mocks.js file. Thanks for spotting that. We'd have to update those pages to create separate browser.js/server.js modules, so you could follow both paths of the tutorial.

Cool, glad I wasn't imagining that. 馃槃

That's definitely a valid concern. I've opened an issue in the docs about that: https://github.com/mswjs/mswjs.io/issues/9

I've updated the "Getting stared" tutorial to recommend the proper file structure. Thanks once more for bringing this up.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

otaciliolacerda picture otaciliolacerda  路  3Comments

veronesecoms picture veronesecoms  路  3Comments

Afsoon picture Afsoon  路  3Comments

derekr picture derekr  路  3Comments

dashed picture dashed  路  3Comments