OWASP / OWASP/Nest

Tests: Scope jest.useFakeTimers usage in frontend unit tests

Open
#3,240 3 comments 0 reactions 0 assignees View on GitHub
bug enhancement
Dominant language
Python
Stars
451
Forks
702
Avg merge
22h 59m
Merged PRs (30d)
91

Description

## Describe the bug

Some frontend unit tests use `jest.useFakeTimers()` at a **global scope** (for example, inside `beforeAll`) instead of scoping it per test or per test suite.
This can cause timer state to leak across tests, leading to flaky behavior, unexpected interactions, and additional cleanup complexity.

This pattern appears in multiple frontend unit test files and makes test isolation harder to reason about and maintain.

---

## To Reproduce

1. Open any of the following test files:
- `frontend/__tests__/unit/pages/ApiKeysPage.test.tsx`
- `frontend/__tests__/unit/pages/EditModule.test.tsx`
- `frontend/__tests__/unit/components/Search.test.tsx`
2. Observe that `jest.useFakeTimers()` is initialized globally (e.g., in `beforeAll`)
3. Run the test suite or individual tests
4. Notice that tests rely on shared timer state and explicit cleanup to avoid interference

---

## Expected behavior

- `jest.useFakeTimers()` should be scoped **per test** or **per describe block**
- Timer mocking should be limited to tests that explicitly depend on it
- Tests should remain isolated without relying on global timer state or broad cleanup logic

This would improve test clarity, isolation, and long-term maintainability.

---

## Are you going to work on fixing this?

- [x] Yes
- [ ] No

---

## Screenshots

Not applicable (test code–level issue).

---

## Desktop (please complete the following information):

- OS: Linux (WSL)
- Browser: N/A
- Version: N/A

---

## Smartphone (please complete the following information):

- Device: N/A
- OS: N/A
- Browser: N/A
- Version: N/A

---

## Additional context

This is a **test hygiene / maintainability** improvement and does not change application behavior.

Scoping fake timers locally aligns with Jest best practices and reduces the risk of flaky or interdependent tests as the test suite grows.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.