Sync vs Async route changes
mainBy default, next-router-mock handles route changes synchronously. If your code relies on Next.js's asynchronous routing behavior, use next-router-mock/async and ensure your tests use await waitFor to account for the delay.
// Example of testing async behavior
import { waitFor } from '@testing-library/react';
it("next/link can be tested too", async () => {
render(
<NextLink href="/example?foo=bar">
<a>Example Link</a>
</NextLink>
);
fireEvent.click(screen.getByText("Example Link"));
await waitFor(() => {
expect(singletonRouter).toMatchObject({
asPath: "/example?foo=bar",
pathname: "/example",
query: { foo: "bar" },
});
});
});