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

next@12.1.6 jest failed tests with dynamic imports #36647

Closed
1 task done
OlesZadorozhnyy opened this issue May 3, 2022 · 9 comments
Closed
1 task done

next@12.1.6 jest failed tests with dynamic imports #36647

OlesZadorozhnyy opened this issue May 3, 2022 · 9 comments
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation

Comments

@OlesZadorozhnyy
Copy link

OlesZadorozhnyy commented May 3, 2022

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information


What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

All components that have dynamic imports started failing in tests. Jest doesn't see it synchronously. Only with Jest API method waitFor it works.

Expected Behavior

Tests with components that have dynamic imports works as before (v.12.1.5)

To Reproduce

  • Use next@12.1.6
  • Create component which dynamically imports another component
  • Create jest test which checks element from dynamic component (without waitFor)
@OlesZadorozhnyy OlesZadorozhnyy added the bug Issue was opened via the bug report template. label May 3, 2022
@timneutkens timneutkens added the please add a complete reproduction The issue lacks information for further investigation label May 3, 2022
@OlesZadorozhnyy
Copy link
Author

From v12.1.6-canary.9 it's started failing

@bossley9
Copy link

I'm also getting this failure with jest 27.4.7.

@balazsorban44
Copy link
Member

balazsorban44 commented May 12, 2022

Does anyone have a simple but complete reproduction? 🙏

@bossley9
Copy link

bossley9 commented May 12, 2022

It's reproducible with the with-jest NextJS example https://github.com/vercel/next.js/tree/canary/examples/with-jest with a few tweaks.

Component.tsx:

export default function () {
  return <h1>Hello world!</h1>;
}

pages/index.tsx:

...
import dynamic from "next/dynamic"
const Component = dynamic(() => import("../Component"));

export default function Home() {
  return (
    ...
    <Component />
  )
}

__tests__/index.test.tsx:

import { render, screen, waitFor } from "@testing-library/react";
import Home from "@/pages/index";

describe("Home", () => {
  it("renders a heading", async () => {
    render(<Home />);

    // this will fail
    expect(
      screen.getByRole("heading", { name: "Hello world!" })
    ).toBeInTheDocument();

    // this will not fail
    await waitFor(() => {
      expect(
        screen.getByRole("heading", { name: "Hello world!" })
      ).toBeInTheDocument();
    });
  });
})

As a side note it seems like jest in the default example can't import css modules so I commented them out.

@timneutkens
Copy link
Member

Looking at the release it might be related to the changes in #36464

@schorfES
Copy link

This applies also for snapshot tests. Sadly, the waitFor approach isn't feasible for snapshot tests when having more than one dynamic inside.

@balazsorban44
Copy link
Member

This issue has been automatically closed because it received no activity for a month and had no reproduction to investigate. If you think this was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you.

@bossley9
Copy link

@OlesZadorozhnyy can you change the description of this issue to include a reproduction? I've written a reproduction in a comment.

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation
Projects
None yet
Development

No branches or pull requests

5 participants