Question: How do I mock an empty response?
See original GitHub issueToday is the first time I am using this library. I am trying to mock an API for unit-testing using React Testing Library with CRA in TypeScript. The API takes a PUT request and gives 204 success response with empty payload in the response. So response looks like this in Chrome:
I tried this:
rest.put('/api', (req: MockedRequest<DefaultRequestBodyType, RequestParams>, res: ResponseComposition<any>, ctx: RestContext) => res(ctx.status(204))),
but got this error:
TypeError: Cannot read property 'length' of null
at node_modules/node-request-interceptor/src/interceptors/XMLHttpRequest/XMLHttpRequestOverride.ts:280:37
Apologies if its a silly question to ask. Also apologies if I am posting in wrong format, I have never posted a query before.
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (12 by maintainers)
Top Results From Across the Web
Is it possible to mock an empty response body in Mockito
I am not using Mockito. It depends what you mean by empty response, It may be empty string, empty array or something else....
Read more >Conditionally mocking an empty response body #189 - GitHub
It seems that conditionally mocking an empty response body doesn't always work as expected: import fetchMock from "jest-fetch-mock"; ...
Read more >Mocking with examples - Postman Learning Center
Step 1: Send a request · Step 2: Save the request to a collection · Step 3: Save a response as an example...
Read more >Top 12 Mockito Interview Questions (Mocking Framework ...
Enlisted below are the most commonly asked questions on Mocking Frameworks. Q #1) Why do we need mocking? Answer: There are a lot...
Read more >A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Calling real methods may be useful on partial mocks, but make sure that the called method has no unwanted side effects and doesn't...
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
It worked, thanks for the quick resolution @kettanaito!
@marcosvega91, I’ve addressed that in the fix pull request. There are two separate statements:
this.response.length
was implemented incorrectly. Instead, I suggest we convert any existing mocked response body (basically, a string) into aBuffer
and access its length.This would be truly peculiar, as
responseType
value is received from the request client, we are not setting it anywhere in the XMLHttpRequestOverride class.