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)

Most upvoted comments

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 /pages folder was a temporary work around for us. We’re using next 13.5.6 and cypress 13.4.0

The issue I think has to do with the loadWebpackConfig function and findPagesDir function. findPagesDir should return undefined if there is no pages directory and there should be an equivalent findAppDir that sets appDir field in loadWebpackConfig. I would try to test this locally but I’m not quite sure where this code actually lives once installed

confirmed adding an empty pages directory works but an interesting side effect of that is that it impacts next-env.d.ts as the additional declaration file next/navigation-types/compat/navigation gets 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 a next-env-custom.d.ts where it removes that added declaration file