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.

msw in testcafe says its started, but responds with 404

See original GitHub issue

Describe the bug

I am attempted to use msw in conjunction with testcafe (the best e2e testing tool for node, yeah even better than cypress, shamless plug).

testcafe has good docs about running service workers & https features in tests which I am following.

When I run the test, I can see it says [MSW] Mocking enabled but then none of the fetches are working.

image

Environment

  • msw: 0.19.4
  • nodejs: v12.16.1
  • yarn:1.22.4

Please also provide your browser version. chrome 83

To Reproduce

Steps to reproduce the behavior:

  1. Clone https://github.com/benmonro/msw-server/tree/feature/testcafe (note repro for this is on a branch called feature/testcafe)
  2. (cd app && yarn)
  3. yarn
  4. yarn testcafe
  5. testcafe will launch a browser, Once the browser loads you can launch chrome dev tools and see the 404 as shown in the above screenshot.

If you’d like to see this working in the actual app:

  1. (cd app && yarn dev)
  2. you can then add a todo or click a todo to remove one.

that all goes through msw and works fine. it only seems to be problematic over testcafe’s proxy server.

Expected behavior

expecting fetch calls to be mocked.

Screenshots

If applicable, add screenshots to help explain your problem. (see above)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (18 by maintainers)

github_iconTop GitHub Comments

2reactions
benmonrocommented, Aug 8, 2020

@kettanaito I have a fix, but I’m not sure where to put a test to capture the fix, would you mind pointing me in the right place? I just have to add a null check for req.body.query and it seems to fix it.

2reactions
kettanaitocommented, Jul 23, 2020

Hey, I’m glad to see it running!

The usage of asterisk proves that the issue is with the proxy URL that Testcafe prepends. While asterisk would work, it’s generally not recommended, as it literally matches <ANYTHING>/todos, which may lead to unexpected matches.

I recommend to look into the Testcafe API and grab its prepended URL during the test runtime. Then use that URL when resolving a request handler’s route. Here’s an example:

const IS_TEST = process.env.NODE_ENV === 'test'

function withUrl(url) {
  const prefix = IS_TEST ? testCafeApi.getUrl() : ''
  return `${prefix}${url}`
}

rest.get(withUrl('/todo'), (req, res, ctx) => res())
Read more comments on GitHub >

github_iconTop Results From Across the Web

Newest 'msw' Questions - Stack Overflow
I am trying to mock post request to api that returns status of 404, so after it returns response i can display an...
Read more >
Debugging uncaught requests - Recipes - Mock Service Worker
A request to /user or /posts will not be intercepted as they lie outside of the worker's scope ( /assets ). The Service...
Read more >
RequestMock Class | Test API | API | Docs - TestCafe
A request mocker that intercepts requests to a web resource and emulates the response. Use the RequestMock constructor to create a mocker.
Read more >
Untitled
Pronatours puerto morelos, Start of wisdom teeth coming in, Shiva nataraja artist, ... Like an onion but not, Abend sa13-10, Uefa women's under...
Read more >
Untitled
Mayor of jackson ms, Gnagarforum aftonbladet plus, Impatiens zombensis, Bates motel norma and norman fanfiction. Contoh perjanjian kredit bank, ...
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