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

Jest mock timer warning without using fake timers #12514

Closed
2 tasks done
sspenst opened this issue Oct 3, 2022 · 3 comments · Fixed by #12527
Closed
2 tasks done

Jest mock timer warning without using fake timers #12514

sspenst opened this issue Oct 3, 2022 · 3 comments · Fixed by #12527
Labels
developer-experience This issue improves error messages, debugging, or reporting
Milestone

Comments

@sspenst
Copy link

sspenst commented Oct 3, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.6.0

Node.js version

18.7.0

MongoDB server version

6.5.4

Description

Using jest.spyOn shows the following warning in the console:

(node:35252) [MONGOOSE] Warning: Mongoose: looks like you're trying to test a Mongoose app with Jest's mock timers enabled. Please make sure you read Mongoose's docs on configuring Jest to test Node.js apps: https://mongoosejs.com/docs/jest.html

Steps to Reproduce

Create a test with the following:

jest.spyOn(logger, 'error').mockImplementation(() => ({} as Logger));

For additional context: https://github.com/sspenst/pathology/blob/main/tests/pages/api/avatar/avatar.test.ts#L23

Expected Behavior

No warning should appear in the console.

@hasezoey
Copy link
Collaborator

hasezoey commented Oct 4, 2022

cannot reproduce, this must mean that mock timers are somehow enabled in your tests OR you are not using NodeJS (like deno) and that function somehow does not include nextTick

the line checking for nextTick is defined like:

if (typeof jest !== 'undefined' && process.nextTick.toString().indexOf('nextTick') === -1) {

which i cant get to output anything other than 9 (process.nextTick.toString().indexOf('nextTick')) when both running in NodeJS REPL or in jest test without enableMockTimers set

MongoDB server version 6.5.4

from what i can tell that version does not exist yet

@hasezoey hasezoey added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Oct 4, 2022
@sspenst
Copy link
Author

sspenst commented Oct 5, 2022

Sorry I was looking at the wrong MongoDB version, I'm using "node_modules/mongodb": { "version": "4.9.1", ...

But thanks for the code reference, I was able to narrow down the root cause:

const newrelicWinstonFormatter = newrelicFormatter(winston);

Using winston logger with this option causes the output of process.nextTick.toString() to be:

function wrappedProcess() {
  const segment = shim.getActiveSegment()
  if (!segment) {
    return fn.apply(this, arguments)
  }

  // Manual copy because helper methods add significant overhead in some usages
  const len = arguments.length
  const args = new Array(len)
  for (let i = 0; i < len; ++i) {
    args[i] = arguments[i]
  }

  shim.bindSegment(args, shim.FIRST, segment)

  return fn.apply(this, args)
}

Is it correct for printJestWarning to always output a warning if nextTick is overwritten? Maybe the check could be updated to only output a warning if nextTick is equal to Jest's mock timer function.

@sspenst
Copy link
Author

sspenst commented Oct 5, 2022

I created a PR for what I think the correct fix might be, but feel free to close it if this is not the right solution.

vkarpov15 added a commit that referenced this issue Oct 5, 2022
vkarpov15 added a commit that referenced this issue Oct 5, 2022
fix: make Jest fake timers check more robust to other libs that overwrite time functions
@vkarpov15 vkarpov15 added this to the 6.6.5 milestone Oct 5, 2022
@vkarpov15 vkarpov15 added developer-experience This issue improves error messages, debugging, or reporting and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
3 participants