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

Help testing WatermelonDB's withObservables #1873

Open
MrCordeiro opened this issue Jan 4, 2025 · 0 comments
Open

Help testing WatermelonDB's withObservables #1873

MrCordeiro opened this issue Jan 4, 2025 · 0 comments

Comments

@MrCordeiro
Copy link

I'm encountering an issue with Jest while running tests for a React Native component called AccountList. This is a "enhanced" component that uses WatermelonDB's withObservables to keep track of a list of accounts:

const enhance = withObservables([], () => ({
  accounts: database.get<Account>("accounts").query(),
}));
export default enhance(AccountList);

I get a message indicates that a worker process has failed to exit gracefully when testing my component.

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  Timeout

      39 |
      40 |   it("renders the items correctly", async () => {
    > 41 |     render(<AccountList />);
         |           ^
      42 |     await waitFor(() => {
      43 |       mockAccounts.forEach((account) => {
      44 |         expect(screen.getByText(account.name)).toBeOnTheScreen();

This is the complete test:

import { render, screen, waitFor } from "@testing-library/react-native";
import database from "@/db";
import Account from "@/model/Account";
import AccountList from "../AccountList";

const mockAccounts = [
  { name: "Test Account 1", balance: 500 },
  { name: "Test Account 2", balance: 1000 },
  { name: "Test Account 3", balance: 1500 },
];

const batchAddAccounts = async (
  accounts: { name: string; balance: number }[],
) => {
  await database.write(async () => {
    const newAccounts = accounts.map((account) =>
      database.collections
        .get<Account>("accounts")
        .prepareCreate((newAccount) => {
          newAccount.name = account.name;
          newAccount.balance = account.balance;
        }),
    );
    await database.batch(...newAccounts);
  });
};

describe("AccountList component", () => {
  beforeAll(async () => {
    await batchAddAccounts(mockAccounts);
  });

  afterEach(async () => {
    await database.write(async () => {
      await database.unsafeResetDatabase();
    });
  });

  it("renders the items correctly", async () => {
    render(<AccountList />);
    await waitFor(() => {
      mockAccounts.forEach((account) => {
        expect(screen.getByText(account.name)).toBeOnTheScreen();
        expect(screen.getByText(account.balance.toString())).toBeOnTheScreen();
      });
    });
  });
});

What I've tried so far:

  • Added jest.clearAllTimers() in the afterAll/afterEach hook to clear any remaining timers.
  • Tried wrapping render with act, only to discover that @testing-library/react-native's render already includes an act wrapper.
  • that all asynchronous operations are properly awaited.
  • Added jest.clearAllMocks() in the afterEach hook to clear any mocks.

What is the recommended way of setting tests using withObservables?

@MrCordeiro MrCordeiro closed this as not planned Won't fix, can't repro, duplicate, stale Jan 4, 2025
@MrCordeiro MrCordeiro reopened this Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant