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

fix(core): fix missing import #23109

Merged
merged 2 commits into from
May 1, 2024
Merged

Conversation

chabb
Copy link
Contributor

@chabb chabb commented May 1, 2024

Current Behavior

The import for performance is missing, which causes an error to appear

Expected Behavior

No error should occured

Related Issue(s)

Fixes #

@chabb chabb requested a review from a team as a code owner May 1, 2024 10:19
@chabb chabb requested a review from AgentEnder May 1, 2024 10:19
Copy link

vercel bot commented May 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview May 1, 2024 10:59am

Copy link
Member

@AgentEnder AgentEnder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey 👋 - performance is available on Node 16 and higher, which means that its available for all of the node versions we currently target.

I'm going to accept this PR, but for future reference you may encounter other issues if using a lower node version. See: https://nx.dev/nx-api/workspace/documents/nx-nodejs-typescript-version-matrix

@AgentEnder AgentEnder merged commit 877b37d into nrwl:master May 1, 2024
6 checks passed
@chabb
Copy link
Contributor Author

chabb commented May 1, 2024

This was actually breaking with node v20. According to the doc, you need to do the import, see https://nodejs.org/api/perf_hooks.html

@alaingiller
Copy link

I'm using node 20.12.0 and have this problem with the last nx version when I run jest test for my nx angular application plugin.
The problem is probably because 'perf_hooks' is imported in same places but not here and probably related to nx cypress plugin.

@AgentEnder
Copy link
Member

Here's the docs that show perf was added as a global in node v16, if you get an error about it not being defined then there is something causing Nx to be ran with a different node version than you are expecting. https://nodejs.org/docs/latest/api/globals.html#performance

You can test this locally in your terminal with node -p "performance.now()".

image

image

@alaingiller
Copy link

alaingiller commented May 1, 2024

Agree, and I've it in node, and I've only one instance of node, the version 20, but with the last version of nx, I got this problem while running jest.
command: nx run my-project:test --coverage
node: v20.12.2
Error:

    LoadPluginError: Could not load plugin /builds/xxxx/my-nxplugin-jslib/node_modules/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json
      at loadNxPluginAsync (../../node_modules/nx/src/project-graph/plugins/loader.js:207:15)
      at loadNxPlugin (../../node_modules/nx/src/project-graph/plugins/loader.js:180:9)
      at loadNxPlugins (../../node_modules/nx/src/project-graph/plugins/internal-api.js:46:48)
      at buildProjectGraphAndSourceMapsWithoutDaemon (../../node_modules/nx/src/project-graph/project-graph.js:75:32)
      at createProjectGraphAndSourceMapsAsync (../../node_modules/nx/src/project-graph/project-graph.js:201:25)
      at createProjectGraphAsync (../../node_modules/nx/src/project-graph/project-graph.js:190:39)
      at initEsLint (../../node_modules/@nx/eslint/src/generators/init/init.js:41:19)
      at lintInitGenerator (../../node_modules/@nx/eslint/src/generators/init/init.js:80:12)
      at lintProjectGeneratorInternal (../../node_modules/@nx/eslint/src/generators/lint-project/lint-project.js:25:22)
      at addLintingGenerator (../../node_modules/@nx/angular/src/generators/add-linting/add-linting.js:14:22)
      at addLinting (../../node_modules/@nx/angular/src/generators/library/library.js:123:5)
      at libraryGeneratorInternal (../../node_modules/@nx/angular/src/generators/library/library.js:69:5)
      at libraryGenerator (../../node_modules/@nx/angular/src/generators/library/library.js:25:12)
    Cause:
    TypeError: performance.mark is not a function
      at loadNxPluginAsync (../../node_modules/nx/src/project-graph/plugins/loader.js:198:21)
      at loadNxPlugin (../../node_modules/nx/src/project-graph/plugins/loader.js:180:9)
      at loadNxPlugins (../../node_modules/nx/src/project-graph/plugins/internal-api.js:46:48)
      at buildProjectGraphAndSourceMapsWithoutDaemon (../../node_modules/nx/src/project-graph/project-graph.js:75:32)
      at createProjectGraphAndSourceMapsAsync (../../node_modules/nx/src/project-graph/project-graph.js:201:25)
      at createProjectGraphAsync (../../node_modules/nx/src/project-graph/project-graph.js:190:39)
      at initEsLint (../../node_modules/@nx/eslint/src/generators/init/init.js:41:19)
      at lintInitGenerator (../../node_modules/@nx/eslint/src/generators/init/init.js:80:12)
      at lintProjectGeneratorInternal (../../node_modules/@nx/eslint/src/generators/lint-project/lint-project.js:25:22)
      at addLintingGenerator (../../node_modules/@nx/angular/src/generators/add-linting/add-linting.js:14:22)
      at addLinting (../../node_modules/@nx/angular/src/generators/library/library.js:123:5)

But with this patch, it is working

@JamesHenry
Copy link
Collaborator

JamesHenry commented May 1, 2024

@AgentEnder @alaingiller I imagine this is a jest testEnvironment issue. I imagine @alaingiller is using jsdom or a custom one where the global won't exist?

(Providing the snippet from Craigory node -p "performance.now()" is working as expected that is)

@alaingiller
Copy link

Good point, I effectively have jest-environment-jsdom.
BUT This is the standard generated by npx create-nx-plugin my-plugin like describe in https://nx.dev/extending-nx/tutorials/create-plugin, so nothing custom.

@chabb
Copy link
Contributor Author

chabb commented May 2, 2024

@JamesHenry @AgentEnder. npx create-nx-plugin generates a project that uses the nx jest plugin

.The jest plugin uses jsdom under the hood : jsdom/jsdom#3309. Jsdom does not implement the performance API ( e.g jsdom/jsdom#3309 ). This is why we have this error when running the tests.
But IHMO, we should not use an evironment that simulates the browser here. For a plugin, I think it makes more sense to use node as the test environment. Using this jest config solves this issue:

module.exports = { ...nxPreset, testEnvironment: 'node' };

Maybe we can update the generator to use node for tests ( for plugin )

That way, you could remove all those perf-hooks import, and things would work out of the box

Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2024
@JamesHenry
Copy link
Collaborator

@chabb Thanks that makes sense, I'll open an issue to track

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants