cypress: Component testing with Next JS 13.4 no longer works after migrating to the "app router" directory structure
Current behavior
The latest release of Next JS https://nextjs.org/blog/next-13-4 includes a change to the recommended folder structure, and removes the previous “pages” directory. This is no longer compatible with the next framework helper built into Cypress, resulting in compilation errors.
The symptoms seem to be identical to https://github.com/cypress-io/cypress/issues/17898, where Cypress previously failed to correctly locate the pages directory.
Same syntax error:
Syntax error: Using `export * from '...'` in a page is disallowed.
I believe the cause for this is https://github.com/cypress-io/cypress/blob/develop/npm/webpack-dev-server/src/helpers/nextHandler.ts#L264 where Cypress expects the pages directory to exist at one of either /pages or /src/pages. It’s possible that adding /app as a possible option would also resolve the issue, but I haven’t been able to verify this.
The project already had Cypress (specifically component tests) implemented and configured, but it stopped working after migrating to the Next JS app router setup. The workaround I found was to simply create an empty /pages directory in the project, this immediately solved the compilation errors, but obviously shouldn’t be necessary.
Desired behavior
Cypress should correctly handle the lack of a /pages directory within a Next JS application since this is now the recommended folder structure.
Test code to reproduce
I can’t currently provide a repo
Cypress Version
latest
Node version
18.x
Operating System
Ubuntu
Debug Logs
No response
Other
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 15 (4 by maintainers)
Any updates regarding this issue?
I can confirm that this issue is still happening and seems to be related to barrel exports with
import * from ...syntax. Adding an empty/pagesfolder was a temporary work around for us. We’re usingnext 13.5.6andcypress 13.4.0The issue I think has to do with the
loadWebpackConfigfunction andfindPagesDirfunction.findPagesDirshould return undefined if there is no pages directory and there should be an equivalentfindAppDirthat setsappDirfield inloadWebpackConfig. I would try to test this locally but I’m not quite sure where this code actually lives once installedconfirmed adding an empty
pagesdirectory works but an interesting side effect of that is that it impactsnext-env.d.tsas the additional declaration filenext/navigation-types/compat/navigationgets automatically added by next. This causes a bunch of typescript errors to occur for me since it impacts search params. So adding to the layers of workarounds for this, I had to create anext-env-custom.d.tswhere it removes that added declaration filenpx create-next-app@latest.