generator icon indicating copy to clipboard operation
generator copied to clipboard

[BUG] :hooksRegistry.test.js uses unsafe global mocks of core modules (fs, path) causing cross-test contamination

Open SHUBHANSHU602 opened this issue 9 months ago • 3 comments

Describe the bug.

While reviewing hooksRegistry.test.js, I found test patterns that break isolation and can affect unrelated tests in the same Jest runtime.

Problem 1: Global mocking of core Node.js modules

At the top of the file:

Image This globally mocks core Node.js modules for the entire test file.

Later, tests mutate their behavior: Image

Why this is a problem

fs and path are shared, foundational modules Mocking them globally affects every import inside the test file Behavior is stateful and mutable Tests can accidentally depend on previous mock state Failures become order-dependent This breaks Jest’s isolation guarantees.

Problem 2: Inconsistent module mocking without reset

The test suite mixes: Image with top-level jest.mock() calls and dynamic jest.mock() inside tests:

Issues:

jest.resetModules() does not undo top-level mocks Some mocked modules persist across test cases Behavior differs depending on test execution order This creates implicit coupling between tests.

Expected behavior

Tests should not globally mock core modules like fs and path

If mocking is required, it should be: Scoped (using jest.spyOn) estored after each test

Each test should start with a clean module state No test should rely on mutated mocks from previous tests Test results should be deterministic and order-independent

Screenshots

Image Image

How to Reproduce

Clone the repo visit:-generator/apps/generator/test/hooksRegistry.test.js Analyse the file

🖥️ Device Information [optional]

  • Operating System (OS):Window11
  • Browser: Brave
  • Browser Version:

👀 Have you checked for similar open issues?

  • [x] I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

SHUBHANSHU602 avatar Feb 07 '26 16:02 SHUBHANSHU602

You're absolutely right—global mocks of core modules like fs and path in hooksRegistry.test.js are causing cross-test contamination and breaking test isolation. The current approach with top-level jest.mock() and only using jest.clearAllMocks()/jest.resetModules() in beforeEach does not fully reset or scope these mocks, so state can leak and tests can become order-dependent. This is a known issue in the repo and has also been observed in other test files, but there are no examples of proper scoped mocking or reset patterns for core modules in the codebase yet (example).

To fix this, avoid top-level jest.mock('fs') and jest.mock('path'). Instead, use jest.spyOn or manual mocks within individual tests or describe blocks, and always restore mocks in afterEach or finally blocks. This ensures each test starts with a clean state and results are deterministic. If you submit a PR with these changes, it would set a great precedent for the rest of the codebase. Happy to review or help with feedback!

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about generator with Dosu Add Dosu to your team

dosubot[bot] avatar Feb 07 '26 16:02 dosubot[bot]

hey @Adi-204 Please have a look at this.

SHUBHANSHU602 avatar Feb 07 '26 16:02 SHUBHANSHU602

Will address later.

SHUBHANSHU602 avatar Aug 26 '26 19:08 SHUBHANSHU602