axios-mock-adapter
axios-mock-adapter copied to clipboard
Is it possible to return the response data as a string?
mock.onGet('/api').reply(200, '{"test": 5}')
response.data is a JS object that looks like {test: 5} instead of a string.
Is there any way to force the response to be a string?
i'd also like to use an array instead of an object. The second parameter of reply should be any type
@ctimmerm any update or fix on how to send an array as a data? I am a bit struggling with it.
nothing news?
The axios default transformResponse parses strings to json objects.
If you don't want this to happen, replace transformResponse in the config.
try this
mock.onGet().replyOnce((config) => { config.transformResponse = [function (data) { return JSON.stringify(data) }] return [ 200, {test: 5} ] })
try this
mock.onGet().replyOnce((config) => { config.transformResponse = [function (data) { return JSON.stringify(data) }] return [ 200, {test: 5} ] })
This won't work if the transformResponse is the thing you want to test.
At the moment, it appears a custom transformResponse on the request is called, but the data isn't passed to it. I'm going to investigate further.