question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, Jun 26, 2020

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

1reaction
kettanaitocommented, Jun 25, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Worker threads | Node.js v19.3.0 Documentation
The node:worker_threads module enables the use of threads that execute JavaScript in parallel. To access it: const worker = require('node:worker_threads');.
Read more >
setupServer() - Api - Mock Service Worker Docs
This function is designed for NodeJS environment. If looking for a way to apply API mocking in a browser environment, consider using setupWorker...
Read more >
msw: Versions - Openbase
The worker script ( mockServiceWorker.js ) now contains the library version number to improve its inspection and debugging across releases (#781). Bug fixes....
Read more >
Configuring Mock Service Worker (MSW) - Daniel Afonso blog
Mock Service Worker is an API mocking library that uses Service Worker API to intercept actual requests. MSW docs. In a short description, ......
Read more >
Getting started with Mock Service Worker - LogRocket Blog
What is Mock Service Worker, and what problem does it solve? ... Let's add Mock Service Work to our application now. We can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found