question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component testing + Next JS using @cypress/react/plugins/next configures the 'pages' directory wrongly

See original GitHub issue

Current behavior

I believe that the internal configuration from the @cypress/react/plugins/next configures ‘nextjs’ to think that the pages directory is the ‘root folder of the project’ rather than ${rootFolder}/pages.

If you look at this line: https://github.com/cypress-io/cypress/blob/develop/npm/react/plugins/next/findNextWebpackConfig.js#L29 Screen Shot 2021-08-26 at 4 06 35 PM

The findNextWebpackConfig util is setting the pagesDir to the root of the project.

Consequences

Because of that I’m seeing that next js webpack config is treating every folder in my root as a ‘pages’ folder, trying to bundle them as if they were pages. I bumped into this because I started seeing the following error while running component tests:

Syntax error: Using `export * from '...'` in a page is disallowed. Please use `export { default } from '...'` instead.
Read more: https://nextjs.org/docs/messages/export-all-in-page

At first I thought I had some ‘export * from …’ inside a ‘page’ but that was not the case. I found that a file inside ${root}/src/UILibrary/icons was doing export * from .... And because of how the cypress next plugin is configuring webpack my /src/UILibrary/icons is being treated as a page by next+webpack.

Desired behavior

Not fully certain of the solution. Probably allow to pass that configuration instead of assuming that pagesDir is config.projectRoot. So we can do the following inside our plugins config file:

const injectDevServer = require("@cypress/react/plugins/next");

module.exports = (on, config) => {
  if (config.testingType === "component") {
    injectDevServer(on, {...config, pagesDir: 'path to my pages dir' });
  }

  return config;
};

Optionally we could have a better default pointing to ${root}/pages (if it exists) or to ${root}/src/pages (which is the second option that nextjs recognizes as convention.

Test code to reproduce

I forked the webpack example from cypress docs and added a export * from ... to trigger the error. (this happens in webpack 4 or webpack 5 config the same. I used the webpack 4 because I wanted to use the official example published by cypress with the minimum amount of changes).

Here the fork and branch where to try it: https://github.com/rafaelchiti/cypress-component-examples/tree/bug_repro

To see the error make sure you are on that branch bug_repo and run: cd nextjs-webpack-4 to focus on the next example yarn install --pure-lockfile DEBUG=* yarn run cypress run-ct If you don’t set a wide debug filter option then the error gets swollen (probably there is a more limited debug config that still prints the error). Then after running that you should find in the terminal an error like:

Syntax error: Using `export * from '...'` in a page is disallowed. Please use `export { default } from '...'` instead.
Read more: https://nextjs.org/docs/messages/export-all-in-page

But if you look in the code there is no export * from ... inside the pages directory. But there is though an export * from ... inside the src/icons directory (which I added to test the problem).

Cypress Version

7.2.0 also tried on 8.3.0

Other

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
ZachJW34commented, Aug 27, 2021

@rafaelchiti Thanks for all the detail, it’s was super helpful. I tested out your repo and confirmed the issue (and the fix by changing pagesDir). I think the best solution is to do what Next does and look for a ${root}/pages or a ${root}/src/pages directory and use that value rather than adding a configuration option (which they seem to be against). I can get a PR for this early next week.

2reactions
ZachJW34commented, Oct 6, 2021

@AndreSilva1993 @rafaelchiti this change has been released in v5.10.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing | Next.js
Learn how to set up Next.js with three commonly used testing tools — Cypress, Playwright, Jest, and React Testing Library.
Read more >
Component testing with Cypress in Next.js - larsmagnus
Component testing in Cypress speeds you up while working for a broad range of concerns.
Read more >
Next.js 13: Working with the new app directory - LogRocket Blog
Explore the new features introduced in the Next.js 13 app directory, which provide much more flexibility to configure our UI.
Read more >
javascript - Tailwindcss not working with next.js; what is wrong ...
I'm wondering if something is wrong with my settings? Styles folder - tailwind.css. @tailwind base; /* Write your own custom base styles here...
Read more >
What's New in Next.js 13 - AppSignal Blog
js 13 aims to improve routing and layouts by introducing the app directory to the root of your Next.js project. This feature is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found