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

Fix useDataProvider should only log errors in development mode #10515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion packages/ra-core/src/dataProvider/useDataProvider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ describe('useDataProvider', () => {
);
});

it('should call getList and show error', async () => {
it('should call getList and show error in development environment', async () => {
process.env.NODE_ENV = 'development';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid mutating process.env.NODE_ENV like that will leak to other tests and break test independence. Can you mock the result using jest.mock or jest.spy instead (and call jest.restoreMocks at the end)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey 👋
I updated the tests in a slightly different manner than you suggested 🤞. If that's not suitable, I can implement the approach you recommended.

jest.spyOn(console, 'error').mockImplementation(() => {});

const getList = jest.fn(() =>
Expand All @@ -253,6 +254,50 @@ describe('useDataProvider', () => {
);
});

it('should call getList and not show error in test environment', async () => {
process.env.NODE_ENV = 'test';

const getList = jest.fn(() =>
Promise.resolve({ data: [{ id: 1, title: 'foo' }] })
);
const dataProvider = { getList };
const { queryByTestId } = render(
<CoreAdminContext dataProvider={dataProvider}>
<UseGetList />
</CoreAdminContext>
);

expect(queryByTestId('loading')).not.toBeNull();
await act(async () => {
await new Promise(resolve => setTimeout(resolve));
});
expect(getList).toBeCalledTimes(1);
expect(queryByTestId('loading')).toBeNull();
expect(queryByTestId('error')).toBeNull();
});

it('should call getList and not show error in production environment', async () => {
process.env.NODE_ENV = 'production';

const getList = jest.fn(() =>
Promise.resolve({ data: [{ id: 1, title: 'foo' }] })
);
const dataProvider = { getList };
const { queryByTestId } = render(
<CoreAdminContext dataProvider={dataProvider}>
<UseGetList />
</CoreAdminContext>
);

expect(queryByTestId('loading')).not.toBeNull();
await act(async () => {
await new Promise(resolve => setTimeout(resolve));
});
expect(getList).toBeCalledTimes(1);
expect(queryByTestId('loading')).toBeNull();
expect(queryByTestId('error')).toBeNull();
});

it('should call custom and not show error', async () => {
const getCustom = jest.fn(() =>
Promise.resolve({ result: [{ id: 1, title: 'foo' }] })
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/dataProvider/useDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const useDataProvider = <
.apply(dataProvider, args)
.then(response => {
if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV === 'development' &&
reactAdminFetchActions.includes(type)
) {
validateResponseFormat(response, type);
Expand Down