Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.19.5] Docs and example now error regarding use of setupWorker in Node #245

Closed
wKovacs64 opened this issue Jun 24, 2020 · 9 comments
Closed
Labels
bug Something isn't working

Comments

@wKovacs64
Copy link
Contributor

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?

@wKovacs64 wKovacs64 added the bug Something isn't working label Jun 24, 2020
@kettanaito
Copy link
Member

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

@kettanaito
Copy link
Member

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

@wKovacs64
Copy link
Contributor Author

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?

@kettanaito
Copy link
Member

kettanaito commented Jun 25, 2020

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.

@wKovacs64
Copy link
Contributor Author

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).

@kettanaito
Copy link
Member

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.

@wKovacs64
Copy link
Contributor Author

Cool, glad I wasn't imagining that. 😄

@kettanaito
Copy link
Member

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

@kettanaito
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants