Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper for creating an error response #1193

Closed
kumar303 opened this issue Nov 1, 2019 · 5 comments
Closed

Add helper for creating an error response #1193

kumar303 opened this issue Nov 1, 2019 · 5 comments

Comments

@kumar303
Copy link
Contributor

@kumar303 kumar303 commented Nov 1, 2019

Some API code deals with an ErrorResponseObject. Here's how we write tests to simulate this:

const errorResponse = { 
  error: new Error('API Error') 
};
const getSomethingFromApi = jest.fn()
  .mockResolvedValue(errorResponse);

We should replace this with a helper similar to createFakeApiPage(). Maybe something like:

const errorResponse = createErrorResponse({ 
  error: new Error('API Error') 
});

This will help us evolve the ErrorResponseObject in the future if we need to. Currently, it would be hard to evolve this type because any time jest.fn() is used, it returns the type unknown which stops TypeScript from type checking anything further.

@ragnarokatz

This comment has been minimized.

Copy link
Contributor

@ragnarokatz ragnarokatz commented Nov 15, 2019

@kumar303 hello, I would like to work on this issue please.

@ragnarokatz

This comment has been minimized.

Copy link
Contributor

@ragnarokatz ragnarokatz commented Nov 16, 2019

@kumar303 a question for clarification, due to the aforementioned behaviour of jest.fn(), do you want the entire lines of

const getSomethingFromApi = jest.fn()
  .mockResolvedValue(errorResponse);

to be replaced with something else?

@kumar303

This comment has been minimized.

Copy link
Contributor Author

@kumar303 kumar303 commented Nov 18, 2019

@ragnarokatz hello, thanks for working on this issue. Yes, it would look like this:

// A definition of createErrorResponse() needs to be added here:
import { createErrorResponse } from '../test-helpers';

const getSomethingFromApi = jest.fn()
  .mockResolvedValue(createErrorResponse({ 
    error: new Error('API Error') 
  }));
@ragnarokatz

This comment has been minimized.

Copy link
Contributor

@ragnarokatz ragnarokatz commented Nov 20, 2019

@kumar303 PR is ready for review. Quite a few changes here and there. Please take a look when you have time.

@kumar303

This comment has been minimized.

Copy link
Contributor Author

@kumar303 kumar303 commented Nov 20, 2019

@ragnarokatz thanks again!

This was fixed in #1255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.