next/link legacyBehavior default value in tests incorrect as of v13
Verify canary release
- [X] 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: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 Binaries: Node: 16.13.2 npm: 8.1.2 Yarn: 3.1.1 pnpm: N/A Relevant packages: next: 13.0.2 eslint-config-next: 12.3.1 react: 18.2.0 react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
After upgrading to v13 and replacing all our next/link to use the new syntax, we started to have some tests failing due to an error saying React.Children.only expected to receive a single React element child.
We are indeed passing multiple children to <Link> but it works fine in our app, just not in tests. After digging a bit, the error is thrown at this line. I noticed that this line is inside a if (legacyBehavior) condition which doesn't make sense in our case as we are not using this prop.
I therefore noticed that the default value of legacyBehavior is Boolean(process.env.__NEXT_NEW_LINK_BEHAVIOR) !== true (here). After logging this env value, I saw it was undefined which explains why legacyBehavior ends up being true.
Expected Behavior
As the new link behaviour is the default as of v13, I think this condition should be at least reversed or even removed so that the default behaviour is not the legacy one if __NEXT_NEW_LINK_BEHAVIOR is not set.
Link to reproduction
https://github.com/ValentinH/next13-link-test-issue/blob/main/src/example.test.js
To Reproduce
Clone https://github.com/ValentinH/next13-link-test-issue and run yarn test.
For the moment, I fixed this issue by adding __NEXT_NEW_LINK_BEHAVIOR=true to my .env.test file.
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.