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

Link component doesn't pass props to anchor in Jest with babel-jest #43508

Closed
1 task done
kirjai opened this issue Nov 29, 2022 · 3 comments
Closed
1 task done

Link component doesn't pass props to anchor in Jest with babel-jest #43508

kirjai opened this issue Nov 29, 2022 · 3 comments
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation. Testing Related to testing with Next.js.

Comments

@kirjai
Copy link

kirjai commented Nov 29, 2022

Verify canary release

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

Provide environment information

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:22 PDT 2022; root:xnu-8020.140.41~1/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.15.1
      npm: 8.11.0
      Yarn: 1.22.18
      pnpm: N/A
    Relevant packages:
      next: 13.0.6-canary.2
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0

What browser are you using? (if relevant)

Happens in Jest

How are you deploying your application? (if relevant)

N/A

Describe the Bug

Props passed to the Link component don't appear on the rendered anchor element when testing with Jest, and specifically only with babel-jest.

The following test:

import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import Link from "next/link";

render(
  <Link href="/abc" className="abc">
    My link
  </Link>
);
    const link = screen.getByRole("link");
    expect(link).toHaveClass("abc");

Fails with

    expect(element).toHaveClass("abc")

    Expected the element to have class:
      abc
    Received:

Testing setup is set up following the Jest with Reach Testing Library guide. Specifically with babel! Rust compiler doesn't have this issue

Expected Behavior

The test should pass, since the anchor element should receive the props passed to it in a testing environment.
The test should pass with babel configuration, as it does with the Rust compiler.

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

https://github.com/kirjai/literate-octo-computing-machine

To Reproduce

run npm test after installing dependencies

@kirjai kirjai added the bug Issue was opened via the bug report template. label Nov 29, 2022
@kirjai
Copy link
Author

kirjai commented Nov 29, 2022

Interestingly, it's as if the behavior of the Next Link is that of Next.js 12 in Jest environment. In addition to props not being forwarded (original issue), i've also come to realize that neither is the anchor element being rendered if you pass any additional elements as children into the Link element, like so:

<Link href="/abc"><span>My link</span></Link>

^ in the above example, the anchor element is never rendered in the tests.

@balazsorban44 balazsorban44 added Navigation Related to Next.js linking (e.g., <Link>) and navigation. Testing Related to testing with Next.js. labels Nov 29, 2022
@balazsorban44
Copy link
Member

Duplicate of #42621

@balazsorban44 balazsorban44 marked this as a duplicate of #42621 Nov 29, 2022
@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 Dec 30, 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. Navigation Related to Next.js linking (e.g., <Link>) and navigation. Testing Related to testing with Next.js.
Projects
None yet
Development

No branches or pull requests

2 participants