Is it possible to mock odata endpoints using mswjs?
I currently use json-server as my temporary backend until the odata endpoint is ready, at which point I’ll migrate my queries to use that. I plan to use mswjs to mock the server in my tests, but would like to migrate now to use for development too.
Is it possible to replicate the odata filter functionality, or will I need to define explicit endpoints for each possible $filter query I want to use for testing?
Eg ‘/users$filter=id in (‘1’, ‘2’)’
I’m unsure how to replicate the $filter functionality that I need.
You can use MSW to intercept OData requests and mock their responses. Since the structure of OData pathnames is unique, I recommend creating a permissive request handler for the entire endpoint and then using some official or third-party tooling to parse that pathname to support things like
$filter.Here's an example of such a setup:
Just by searching the web for a minute, I found
odata-filter-parserandodata/parser. You can find something even better suited for your use case.