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 flaky tests #9952

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions packages/create-react-admin/src/generateAppTestFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ test("should pass", async () => {
? `

// Sign in
// Ensure the form is fully loaded before interacting with it
await new Promise((resolve) => setTimeout(resolve, 1000));
Copy link
Member

Choose a reason for hiding this comment

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

You should use an assertion in a waitfor instead of a timeout. Same for the other 2 instances

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We already do. I still had to add this

Copy link
Member

Choose a reason for hiding this comment

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

Instead, add a timeout option to the initial findByLabelText, as it proxies its options to waitFor.


fireEvent.change(await screen.findByLabelText("Username *"), {
target: { value: "janedoe" },
});
Expand All @@ -33,6 +36,9 @@ test("should pass", async () => {
// Open the first post
fireEvent.click(await screen.findByText("Post 1"));
fireEvent.click(await screen.findByText("Edit"));
await screen.findByDisplayValue("Post 1");
// Ensure the form is fully loaded before interacting with it
await new Promise((resolve) => setTimeout(resolve, 1000));
// Update its title
fireEvent.change(await screen.findByDisplayValue("Post 1"), {
target: { value: "Post 1 edited" },
Expand All @@ -45,6 +51,9 @@ test("should pass", async () => {
// Open the first comment
fireEvent.click(await screen.findByText("Comment 1"));
fireEvent.click(await screen.findByText("Edit"));
await screen.findByDisplayValue("Post 1 edited");
// Ensure the form is fully loaded before interacting with it
await new Promise((resolve) => setTimeout(resolve, 1000));
// Edit the comment selected post
fireEvent.click(await screen.findByDisplayValue("Post 1 edited"));
fireEvent.click(await screen.findByText("Post 11"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const SuccessCase = () => {
const index = posts.findIndex(p => p.id === params.id);
posts.splice(index, 1);
resolve({ data: params.previousData });
}, 1000);
}, 500);
});
},
} as any;
Expand Down Expand Up @@ -94,7 +94,7 @@ export const ErrorCase = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('something went wrong'));
}, 1000);
}, 500);
});
},
} as any;
Expand Down
8 changes: 4 additions & 4 deletions packages/ra-core/src/dataProvider/useDelete.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,16 @@ describe('useDelete', () => {
screen.getByText('Delete first post').click();
await waitFor(() => {
expect(screen.queryByText('success')).toBeNull();
expect(screen.queryByText('Hello')).not.toBeNull();
expect(screen.queryByText('World')).not.toBeNull();
expect(screen.queryByText('mutating')).not.toBeNull();
});
expect(screen.queryByText('Hello')).not.toBeNull();
expect(screen.queryByText('World')).not.toBeNull();
await waitFor(() => {
Copy link
Member

Choose a reason for hiding this comment

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

the default timeout for waitFor is 1000ms. The tested story adds a delay of 1000ms to the delete. It's logical that this may fail from time to time.

I advise to reduce the delay to 500 in the story to make it more robust

expect(screen.queryByText('success')).not.toBeNull();
expect(screen.queryByText('Hello')).toBeNull();
expect(screen.queryByText('World')).not.toBeNull();
expect(screen.queryByText('mutating')).toBeNull();
});
expect(screen.queryByText('Hello')).toBeNull();
expect(screen.queryByText('World')).not.toBeNull();
});
it('when pessimistic, displays error and error side effects when dataProvider promise rejects', async () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
Expand Down
Loading