Support alternative JSON library
See original GitHub issueIs your feature request related to a problem? Please describe.
This is both a feature request and a bug report. Consider the following :
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import axios from 'axios';
import JSONbig from 'json-bigint';
// This is required by Jest to work properly
BigInt.prototype.toJSON = function() { return this.toString(); };
const mockBaseUrl = "http://localhost/services/acme/api";
const customer = {
"username": "Dude",
"balance": BigInt(1597928668063727616) // yes, the Dude is rich
};
const server = setupServer(
rest.get(`${mockBaseUrl}/customers/me`, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json(customer),
)
}),
)
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
const apiAxios = axios.create({
transformResponse: [data => data ? JSONbig.parse(data) : data],
});
describe("Mocks", () => {
it("should mock a basic GET call", () => {
const url = `${mockBaseUrl}/customers/me/`;
return expect(apiAxios.get(url).then(r => r.data)).resolves.toEqual(customer);
});
});
This test result in the following error :
Error: expect(received).resolves.toEqual(expected) // deep equality
- Expected
+ Received
Object {
- "balance": 1597928668063727616n,
+ "balance": "1597928668063727616",
"username": "Dude",
}
Describe the solution you’d like
One simple solution would be to be able to set the JSON library to use while transforming the response, as it looks like you internally transform to json.
In my case the common library to handle BigInt
is json-bigint.
Describe alternatives you’ve considered
I’m still looking into creating my own response transformer to handle BigInt
but there is no documentation about it so it’s quite time consuming.
Issue Analytics
- State:
- Created 3 years ago
- Comments:28 (23 by maintainers)
Top Results From Across the Web
buger/jsonparser: One of the fastest alternative JSON parser ...
One of the fastest alternative JSON parser for Go that does not require schema - GitHub - buger/jsonparser: One of the fastest alternative...
Read more >Top 5 JSON Library Java JEE Developers Should Know
Fortunately, there are so many open source libraries and APIs available for creating, parsing, and processing JSON response in Java, like Jackson, Google...
Read more >Play JSON Alternatives - Awesome Scala - LibHunt
Play JSON alternatives and similar packages ; circe. 9.3 · Play JSON VS circe. Yet another JSON library for Scala ; json4s. 8.8...
Read more >[2021 update] The Ultimate JSON Library: JSON.simple vs ...
GSON is a Java library that converts Java Objects into JSON and vice versa. It provides the added benefit of full support for...
Read more >orjson — A fast json parser for python | by Robby Boney
“orjson is a fast, correct JSON library for Python. It benchmarks as the fastest Python library for JSON and is more correct than...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
yes of course
I think that the default transformers should reside on the
res
composition function. This should be clear, as the defaultres
function stringifies responses usingJSON.stringify
, if you wish to have a custom stringification, create a custom composition function. I think this is the right message to give to the users, as they shouldn’t think of theres
functions as something magical. It’s a plain function you should be able to compose into another function, or replace entirely.I like your approach of creating a custom response function via
createResponseComposition
. We should expose this function publicly and allow users to customize built-in transformers that way. As of whether those transformers should be on theMockedResponse
, or accepted by thecreateResponseComposition
as an argument—I’m not sure.createResponseComposition
createResponseComposition
, while other transformers straight to the createdres
function.Worker and server instances represent interception layer as a whole, they shouldn’t care about how response is stringified. On the implementation level it would be a huge argument drilling chain to get
options.transformers
down to eachres
function, which is a good sign it doesn’t belong toStartOptions
.