[BUG] :hooksRegistry.test.js uses unsafe global mocks of core modules (fs, path) causing cross-test contamination
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:
Later, tests mutate their behavior:
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:
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
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?
- [x] I have read the Contributing Guidelines
Are you willing to work on this issue ?
Yes I am willing to submit a PR!
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.
hey @Adi-204 Please have a look at this.
Will address later.