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.

TypeError: expected.toLowerCase() is not a function when using "rest.all()"

See original GitHub issue

Describe the bug

When using rest.all() to mock all requests of a given path, it blocks HMR (and other requests).

That’s due to an issue with the usage of isStringEqual in the file src/utils/request/onUnhandledRequest.ts. It assumes that method is always a string and with the introduction of rest.all it seems to be a RegExp now.

Environment

  • msw: 0.36.0
  • nodejs: 14.17.6
  • npm: 6.14.15
  • yarn: 1.22.15
  • Chrome: Version 96.0.4664.55 (Official Build) (x86_64)

To Reproduce

Steps to reproduce the behavior:

  1. Scaffold a create-react-app
  2. Set up MSW with a rest.all() handler (mine was like this: rest.all('/todos', (req, res, ctx) => {...});
  3. Run yarn start
  4. Make a change on one of the files
  5. See error in the console like the ones below:

Expected behavior

Requests not targeted by the rest.all() should still be performed. By temporarily patching the rest-deps changing the return to:

return actual.toString().toLowerCase() === expected.toString().toLowerCase();

solved the issue, but I know that’s not the right fix 😄

Screenshots

HMR broken: image

(also messes requests for the chrome extension) image

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
kettanaitocommented, Dec 8, 2021

The fix has been released in 0.36.2.

1reaction
rmachado-studocucommented, Dec 8, 2021

Hi @kettanaito,

From what I can see, you’re creating all as RegExp handler: https://github.com/mswjs/msw/blob/master/src/rest.ts#L35

I was checking on the integration tests and it hasn’t been caught on them because you always have a fallback - rest.all('*', () => { ... }); - and you check against that generic response.

The issue here is exactly the case where you set up something like:

rest.all('/specific/endpoint', () => { /*...*/ });

And a request is done to say /another/different/endpoint… Not having any generic request should let it pass and request the actual resource… right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - .toLowerCase not working, replacement function?
The .toLowerCase() function only exists on strings. You can call .toString() on anything in JavaScript to get a string representation.
Read more >
[Solved] TypeError: toLowerCase is not a function in JavaScript
TypeError : .toLowerCase is not a function occurs when we call toLowerCase() function on object which is not an string. toLowerCase() function can...
Read more >
parseInt() - JavaScript - MDN Web Docs
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
Read more >
Best Practices for Spies, Stubs and Mocks in Sinon.js
Thankfully, we can use Sinon.js to avoid all the hassles involved. ... the example function, we call two functions in it — toLowerCase...
Read more >
Node.js v19.3.0 Documentation
Iterates through the list of functions passed to tracker.calls() and will throw an error for functions that have not been called the expected...
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