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.

Question: How do I mock an empty response?

See original GitHub issue

Today 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:

Screenshot 2020-12-02 at 9 37 46 PM

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:closed
  • Created 3 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
indiansagecommented, Dec 3, 2020

It worked, thanks for the quick resolution @kettanaito!

1reaction
kettanaitocommented, Dec 3, 2020

@marcosvega91, I’ve addressed that in the fix pull request. There are two separate statements:

  • Response JSON body parsing must be graceful. XHR doesn’t raise an exception if the response body is an invalid JSON. We should retain that behavior.
  • The “progress” event, where we reference this.response.length was implemented incorrectly. Instead, I suggest we convert any existing mocked response body (basically, a string) into a Buffer and access its length.

Yes I think that before that request there is a not handled request that has a responseType equal to json that will persist in the next request

This would be truly peculiar, as responseType value is received from the request client, we are not setting it anywhere in the XMLHttpRequestOverride class.

Read more comments on GitHub >

github_iconTop 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 >

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