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

Timers mock panic with sub-test #55849

Closed
axetroy opened this issue Nov 14, 2024 · 5 comments · Fixed by #55858
Closed

Timers mock panic with sub-test #55849

axetroy opened this issue Nov 14, 2024 · 5 comments · Fixed by #55858
Labels
confirmed-bug Issues with confirmed bugs. test_runner Issues and PRs related to the test runner subsystem.

Comments

@axetroy
Copy link

axetroy commented Nov 14, 2024

Version

v22.10.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

1. Create a test file a.test.mjs

import test from 'node:test'

test.beforeEach((t) => {
    t.mock.timers.enable({ apis: ['Date'] })
    t.mock.timers.tick(1731488301220) // 2024-11-13 16:58:21.220
})

test('test 1', async (t) => {
    await t.test('sub test 1', async (t) => {
        //
    })
})

2. Run the following command to test

node --test --experimental-default-type=module ./a.test.mjs

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior? Why is that the expected behavior?

There should be not panic

What do you see instead?

▶ test 1
  ✖ sub test 1 (1.0925ms)
    TypeError [Error]: Cannot redefine property: Symbol(MockTimers)
        at defineProperties (<anonymous>)
        at #createDate (node:internal/test_runner/mock/mock_timers:381:5)
        at Object.Date (node:internal/test_runner/mock/mock_timers:614:45)
        at node:internal/test_runner/mock/mock_timers:635:74
        at Array.forEach (<anonymous>)
        at #toggleEnableTimers (node:internal/test_runner/mock/mock_timers:635:5)
        at MockTimers.enable (node:internal/test_runner/mock/mock_timers:720:29)
        at TestContext.<anonymous> (file:///path/to/a.test.mjs:4:19)
        at TestHook.runInAsyncScope (node:async_hooks:211:14)
        at TestHook.run (node:internal/test_runner/test:934:25)

Additional information

No response

@RedYetiDev RedYetiDev added test_runner Issues and PRs related to the test runner subsystem. confirmed-bug Issues with confirmed bugs. labels Nov 14, 2024
@RedYetiDev
Copy link
Member

RedYetiDev commented Nov 14, 2024

Even simpler reproduction:

import { test, beforeEach } from 'node:test'

beforeEach((t) => t.mock.timers.enable());

test(() => test());
$ node repro.js

It's failing here:

ObjectDefineProperties(MockDate, {
__proto__: null,
[kMock]: {
__proto__: null,
enumerable: false,
configurable: false,
writable: false,
value: this,
},
isMock: {
__proto__: null,
enumerable: true,
configurable: false,
writable: false,
value: true,
},
});

@deokjinkim
Copy link
Contributor

With above example, #createDate is called 2 times. So when I change configurable of [kMock] to true, above example is working. But I'm not sure that this approach is right.

@RedYetiDev
Copy link
Member

I don't think we should set it to true.

IMO The appropriate response should be an error that the mock is already enabled, as is done if you try to enable it twice.

@RedYetiDev
Copy link
Member

A better way to look at this is:

import { test } from 'node:test'

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

When outer changes the Date object, it sets these properties.

Then, when inner wants to change the Date object, it copies those properties, before trying to set them:

ObjectDefineProperties(
MockDate,
dateProps,
);

IMO there are two paths that can be taken here:
A) Throw an error that the object has already been mocked
B) Exclude those properties, and overwrite the mock.

I'm inclined for (A).

@RedYetiDev
Copy link
Member

Opened #55858

RafaelGSS pushed a commit that referenced this issue 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 issue 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>
Ceres6 pushed a commit to Ceres6/node that referenced this issue Nov 26, 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>
ruyadorno pushed a commit that referenced this issue Nov 27, 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>
ruyadorno pushed a commit that referenced this issue Nov 27, 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants