Skip to content

Conversation

drouian-m
Copy link

Description

What is changing?

  • Add missing node: perf_hooks import to use performance.now() safely.
Is there new documentation needed for these changes?

No

What is the motivation for this change?

There is a bug in timeout.ts related to the usage of performance.now() without the node: perf_hooks import introduced with this commit bd8a9f4 (related Jira : https://jira.mongodb.org/browse/NODE-6090).

I have search for all performance.now() usages and fix all of them in this PR.

Release Highlight

Fill in title or leave empty for no highlight

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@nbbeeken
Copy link
Contributor

Linking:

Hi @drouian-m can you elaborate on what the issue is? performance is global in Node.js in all the versions we support, is there possibly a bundler involved that is misconfigured?

@nbbeeken nbbeeken changed the title fix(performance): add missing perf_hooks import fix(NODE-6617): add missing perf_hooks import Dec 17, 2024
@drouian-m
Copy link
Author

performance is exposed in global by node, but when you have to transpile typescript with tools like swc, esbuild, etc... the globals objects are not always well resolved.
For this reason it is better to be explicit in the imports.

@nbbeeken
Copy link
Contributor

Our approach here has not changed from the previous time this issue was brought up, the addition of imports also has an impact on bundling for those that need to either stub or exempt "node:perf_hooks" from their configs. By using the global we're targeting the common runtime variable available in node, bun, deno, and the web.

This is a good opportunity to capture some knowledge share on what the precise issue is and how it can be resolved either through configuration or permanently as a part of those tools, the question is why is esbuild/swc/etc. causing friction for the widely available API? Do you have an exact error message/trace you could share?

@drouian-m
Copy link
Author

Here the error stacktrace when we open a new mongo connection in a jest test suite :

ReferenceError: performance is not defined at new Timeout (node_modules/mongodb/src/timeout.ts:75:29)

The openConnection code :

async #connect(): Promise<Db> {
  this.#client = await MongoClient.connect(this.#connectionString, this.#options);
  this.#db = this.#client.db();
  ...
  return this.#db;
}

async openConnection(): Promise<Db> {
  return this.retry ? this.connectWithRetry() : this.#connect();
}

The jest config :

module.exports = {
  setupFilesAfterEnv: ['./jest.setup.js'],
  testEnvironment: 'node',
  testResultsProcessor: 'jest-junit',
  verbose: false,
  collectCoverage: true,
  testTimeout: 10000,
  forceExit: true,
  silent: true,
  testMatch: [`${__dirname}/**/*.spec.(t|j)s`],
  transform: {
    '^.+\\.(t|j)sx?$': ['@swc/jest'],
  }
};

And the before hook in the jest setup file :

global.beforeAll(async () => {
  mongoConnManager = new MongoConnectionManager(config.adminMongoUrl, logger);
  global.db = await mongoConnManager.openConnection();
});

With the current version of the driver, we have to add the following lines before the test hook to have performance available in the mongo driver timeout.ts file :

const { performance } = require('node:perf_hooks');
global.performance = performance;

I hope that this information will help to better understand the problem encountered.

@addaleax
Copy link
Contributor

With the current version of the driver, we have to add the following lines before the test hook to have performance available in the mongo driver timeout.ts file :

Have you considered opening a pull request or bug report against Jest? As far as I know, they generally try to provide an environment for tests that is similar to the environment your actual code runs under as well.

@nbbeeken
Copy link
Contributor

It seems like this issue with jest has already been addressed:

Thanks for describing the issue in detail, I'm sure the keywords will help someone else in the future!

@nbbeeken nbbeeken closed this Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants