Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withNx function changes broke next/jest (native NextJS feature) #16277

Closed
raphzandrade opened this issue Apr 12, 2023 · 5 comments
Closed

withNx function changes broke next/jest (native NextJS feature) #16277

raphzandrade opened this issue Apr 12, 2023 · 5 comments
Labels
outdated scope: nextjs Issues related to NextJS support for Nx scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@raphzandrade
Copy link

raphzandrade commented Apr 12, 2023

Current Behavior

If you include next/jest [ref] in an application that uses withNx, an error will occur when running the unit tests.

image

Without using withNx, the error won't occur. However, if you add [0] to the withNx function, it will work again.

image

image

Expected Behavior

The exported next configuration should work with any tool native of the NextJS, like the next/jest.

GitHub Repo

https://github.com/raphzandrade/next-errors

Steps to Reproduce

  1. Add next/jest to an application or library's jest.config.ts file (the dir value must points to a valid application).
  2. execute the unit tests for the application or library.

Nx Report

>  NX   Report complete - copy this into the issue template

   Node : 18.13.0
   OS   : darwin x64
   pnpm : 7.26.2
   
   nx                      : 15.9.2
   @nrwl/js                : 15.9.2
   @nrwl/jest              : 15.9.2
   @nrwl/linter            : 15.9.2
   @nrwl/workspace         : 15.9.2
   @nrwl/cypress           : 15.9.2
   @nrwl/eslint-plugin-nx  : 15.9.2
   @nrwl/next              : 15.9.2
   @nrwl/react             : 15.9.2
   @nrwl/nx-cloud          : 15.3.5
   typescript              : 4.9.5

Failure Logs

No response

Additional Information

No response

@raphzandrade raphzandrade changed the title withNx function changes broke next/jest withNx function changes broke some native NextJS features Apr 12, 2023
@raphzandrade raphzandrade changed the title withNx function changes broke some native NextJS features withNx function changes broke next/jest (native NextJS feature) Apr 12, 2023
@AgentEnder AgentEnder added scope: nextjs Issues related to NextJS support for Nx scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx labels Apr 12, 2023
@raphzandrade
Copy link
Author

We are still experiencing the problem in version 16.1. Would it be possible to obtain an estimate for when this issue can be addressed? As it stands, this issue is preventing us from updating our dependencies and addressing other issues with the nxcloud.

@seanparmelee
Copy link
Contributor

We ran into this as well. The issue seems to be that when passing in dir to nextJest, it'll try to load next.config.js, but the withNx plugin (specifically, the getNxContext function) doesn't know what to do when targetConfig.executor === '@nx/jest:jest' and throws the error. I'm not sure if this is as simple as adding a case for @nx/jest:jest here, but we sort of simulated that by updating our jest.config.ts to have

export default async function hack() {
  const prevNxTarget = process.env.NX_TASK_TARGET_TARGET;
  // temporarily override NX_TASK_TARGET_TARGET to be "build" so that withNx() returns the next config
  process.env.NX_TASK_TARGET_TARGET = 'build';
  const jestConfig = await createJestConfig(customJestConfig)();
  process.env.NX_TASK_TARGET_TARGET = prevNxTarget;
  return jestConfig;
}

and that fixes the issue for us.

Aside from loading next.config.js, another thing passing the dir option does is allows nextJest to find and load the tsconfig.json / jsconfig.json file and configure the SWC transformer accordingly (setting options such as paths).

Another, less hacky solution that worked for us was to omit passing dir into nextJest and updating the customJestConfig to have

resolver: '@nx/jest/plugins/resolver'

which @barbados-clemens made reference to in #15315 (comment). This resolver has some fallback logic in it for resolving modules via typescript.

As a final disclaimer, we're just getting starting with Nx as well as our Next.js application so I'm not sure if/how well either of these solutions work with larger, more complex Next.js applications.

@jaysoo
Copy link
Member

jaysoo commented Jun 27, 2023

This has been fixed in the merged PR. It will be in the new release (either this week or next week).

@jaysoo jaysoo closed this as completed Jun 27, 2023
@phil-tutti
Copy link

We're still experiencing the same problem, but we're using withNx like this

const { composePlugins, withNx } = require("@nx/next");

const plugins = [
  // Add more Next.js plugins to this list if needed.
  withNx,
];
...
// some more plugins
...
composePlugins(...plugins)(nextConfig)

As soon as I remove withNx from the plugin list, everything works. We're on the latest version (16.5.5)

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: nextjs Issues related to NextJS support for Nx scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
Development

No branches or pull requests

5 participants