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.

Issue unit testing with nextjs and absolute imports

See original GitHub issue

Current behavior

We use a simple tsconfig with baseUrl: "." so all our imports are relative to the root of the project, like import Button from "components/Button". No issues with integration tests. However, when running a unit test and importing a simple function from a file that has other relative imports (like import {anotherUtil} from 'utils/someOtherUtils', Cypress does not understand this relative import.

The error message looks like:

Error: Webpack Compilation Error
./utils/someMath.ts
Module not found: Error: Can't resolve 'utils/addTwoNumbers' in '/Users/bradycaspar/code/test/cypress-next/utils'
resolve 'utils/addTwoNumbers' in '/Users/bradycaspar/code/test/cypress-next/utils'
  Parsed request is a module
  using description file: /Users/bradycaspar/code/test/cypress-next/package.json (relative path: ./utils)
    Field 'browser' doesn't contain a valid alias configuration
    Looked for and couldn't find the file at the following paths:
[/Users/bradycaspar/code/test/cypress-next/utils/node_modules]
[/Users/bradycaspar/code/test/node_modules]
[/Users/bradycaspar/code/node_modules]
[/Users/node_modules]...

The error message makes it sound like it is searching for this import in node_modules and nowhere else.

This works if the import is removed from the file or the import format changes to import Button from './Button'. However if possible I’d prefer to not change the imports across the whole app 😄

Desired behavior

The imports with the format we use are found as expected.

Test code to reproduce

I created a minimal repro here: https://github.com/bscaspar/cypress-next-import-error-example.

Cypress Version

^9.5.1

Other

Am I missing a simple config? Any help is appreciated!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
feRpicoralcommented, Apr 20, 2022

Sorry, I’ve not been tracking this issue. We are in the process of updating our Next.js plugin to work with v11 and v12 with more thorough code around the various edge cases. This should be released w/ Cypress 10 (next few weeks).

In the meantime: it seems you found a workaround? Also, any particular reason to use the browserify plugin when Next.js is using webpack?

Looking forward for this next release with better support for Next 12!

Yes, I did found a workaround - I ended up writing a custom transformer for browserify. Regarding why I’m using it, I have no particular reason - I was following the official docs on how to have coverage reports for both my unit & integration tests.

0reactions
lmiller1990commented, Apr 20, 2022

Sorry, I’ve not been tracking this issue. We are in the process of updating our Next.js plugin to work with v11 and v12 with more thorough code around the various edge cases. This should be released w/ Cypress 10 (next few weeks).

In the meantime: it seems you found a workaround? Also, any particular reason to use the browserify plugin when Next.js is using webpack?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js Setup | ESLint Jest React Testing Library and Absolute ...
Next.js Setup | ESLint Jest React Testing Library and Absolute Imports. A comprehensive step-by-step guide to configuring Jest, React Testing Library ...
Read more >
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 >
Absolute Imports in Next.js - Netlify
Absolute imports can save the day when you have to deal with project organization and multiple levels of folders.
Read more >
How to setup path aliases for development with Next.js & Jest
Step 2 - Create some files to import · Header.js and Button.js components inside a components folder · __tests__ folder with our component...
Read more >
Testing Next.js with Playwright - Frontend Digest
I highly recommend using TypeScript as this gives you access to import statements, which can not be used with test files written in...
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