Visitar URL original
test_runner: error on mocking an already mocked date by RedYetiDev · Pull Request #55858 · nodejs/node · GitHub
The Wayback Machine - https://web.archive.org/web/20241122220227/https://github.com/nodejs/node/pull/55858
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

test_runner: error on mocking an already mocked date #55858

Merged
merged 1 commit into from
Nov 17, 2024

Conversation

RedYetiDev
Copy link
Member

Fixes #55849

Previously, the properties from the global Date, including the not-writable isMock and [kMock] values, would be copied if the global Date was already mocked. This became an issue because the mock setup would attempt overwrite these properties.

Now, ERR_INVALID_STATE is thrown, as Date is already being mocked!.

Example

import { test } from 'node:test'

test((outer) => {
    outer.mock.timers.enable()
    test((inner) => inner.mock.timers.enable())
});

Before

✖ <anonymous> (0.536701ms)
  TypeError: Cannot redefine property: Symbol(MockTimers)
      at defineProperties (<anonymous>)
      at #createDate (node:internal/test_runner/mock/mock_timers:396:5)
      at Object.Date (node:internal/test_runner/mock/mock_timers:635:45)
      at node:internal/test_runner/mock/mock_timers:659:74
      at Array.forEach (<anonymous>)
      at #toggleEnableTimers (node:internal/test_runner/mock/mock_timers:659:5)
      at MockTimers.enable (node:internal/test_runner/mock/mock_timers:740:29)
      at TestContext.<anonymous> (file:///repro.js:5:39)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:934:25)

After

✖ <anonymous> (0.573319ms)
  Error [ERR_INVALID_STATE]: Invalid state: Date is already being mocked!
      at #createDate (node:internal/test_runner/mock/mock_timers:332:13)
      at Object.Date (node:internal/test_runner/mock/mock_timers:638:45)
      at node:internal/test_runner/mock/mock_timers:662:74
      at Array.forEach (<anonymous>)
      at #toggleEnableTimers (node:internal/test_runner/mock/mock_timers:662:5)
      at MockTimers.enable (node:internal/test_runner/mock/mock_timers:743:29)
      at TestContext.<anonymous> (file:///repro.js:5:39)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:934:25)
      at Test.start (node:internal/test_runner/test:833:17) {
    code: 'ERR_INVALID_STATE'
  }

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Nov 14, 2024
Copy link

codecov bot commented Nov 15, 2024 •

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.41%. Comparing base (b52a49b) to head (52a34ef).
Report is 19 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #55858   +/-   ##
=======================================
  Coverage   88.41%   88.41%           
=======================================
  Files         654      654           
  Lines      187824   187827    +3     
  Branches    36134    36137    +3     
=======================================
+ Hits       166060   166064    +4     
+ Misses      15005    15003    -2     
- Partials     6759     6760    +1     
Files with missing lines Coverage Δ
lib/internal/test_runner/mock/mock_timers.js 98.74% <100.00%> (+<0.01%) ⬆️

... and 33 files with indirect coverage changes

Copy link
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

Is throwing necessary/helpful? If it's already mocked, why not just silently abort? (don't re-mock, but don't throw)

lib/internal/test_runner/mock/mock_timers.js Show resolved Hide resolved
@RedYetiDev
Copy link
Member Author

IMO throwing is helpful because in this case, we aren't the ones mocking.

If the user attempts to perform any other mocking utilities (such as restoring the mock), it won't work.

@JakobJingleheimer
Copy link
Member

IMO throwing is helpful because in this case, we aren't the ones mocking.

If the user attempts to perform any other mocking utilities (such as restoring the mock), it won't work.

Ahh, good then :)

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

LGTM. I do think we should follow this up with a change to remove the screaming error messages.

@RedYetiDev RedYetiDev added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Nov 16, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 16, 2024
@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 17, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 17, 2024
@nodejs-github-bot nodejs-github-bot merged commit 775a100 into nodejs:main Nov 17, 2024
72 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 775a100

RafaelGSS pushed a commit that referenced this pull request Nov 18, 2024
Fixes #55849

PR-URL: #55858
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
tpoisseau pushed a commit to tpoisseau/node that referenced this pull request Nov 21, 2024
Fixes nodejs#55849

PR-URL: nodejs#55858
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Timers mock panic with sub-test
7 participants