JEST Assignment- 3

Basic Questions

  1. Write a test for a function using setTimeout and check it executes after 1s (use jest.useFakeTimers).
  2. Write a test for a function using setInterval and ensure it runs 3 times in 3s.
  3. Write a test that clears an interval using clearInterval.
  4. Write a test that simulates a delayed log (setTimeout) and flushes it with jest.runAllTimers().
  5. Write a test that cancels a setTimeout before it executes.
  6. Write a test that checks a function runs immediately when using jest.runOnlyPendingTimers().Basic Questions
  7. Write a test that simulates a debounced function firing only once despite multiple calls.
  8. Write a test that simulates a throttled function firing at intervals.
  9. Write a test that mocks a REST API with fetch returning {status:”ok”}.
  10. Write a test that mocks a REST API error response.
  11. Write a test that mocks an Axios get request returning {id:1,name:”Sarthak”}.
  12. Write a test that mocks Axios post request and verifies payload.
  13. Write a test that simulates network timeout error in Axios.
  14. Write a test that renders a React component with a loading state before API resolves.
  15. Write a test that renders a React component with an error state when API fails.
  16. Write a test for a form where required field “username” shows error when empty.
  17. Write a test that validates email format in a form.
  18. Write a test that validates password min length in a form.
  19. Write a test that checks snapshot for a form with 2 fields.
  20. Write a test that ensures coverage report runs with –coverage.

 Intermediate Questions

  1. Write a test for a debounced search box in React and ensure only last call executes.
  2. Write a test for a throttled scroll handler and ensure calls are limited.
  3. Write a test that uses jest.advanceTimersByTime(2000) to simulate time travel.
  4. Write a test for mocking multiple API calls in sequence (axios.get).
  5. Write a test that simulates a failed fetch and shows “Network Error”.
  6. Write a test for mocking a GraphQL query using Apollo MockedProvider.
  7. Write a test for a GraphQL error response (errors:[…]).
  8. Write a test for a React component that shows “Loading…” until GraphQL resolves.
  9. Write a test for a React form submission calling onSubmit with values.
  10. Write a test that ensures form error message disappears after correcting input.
  11. Write a test for custom validation that password must contain @.
  12. Write a test for form submission that triggers API call with values.
  13. Write a test for form submission failure that shows “Try again” error.
  14. Write a test that generates coverage and checks at least one branch is covered.
  15. Write a test that analyzes coverage for a utility function (sum.js).
  16. Write a test that ensures all component props are covered in snapshot.
  17. Write a test that uses jest.mock to replace a form validation helper function.
  18. Write a test for an API call with retries (mock fails once then succeeds).
  19. Write a test for Axios interceptor mock that modifies headers.
  20. Write a test that sets coverage threshold in jest.config.js and fails below 90%.

 Advanced Questions

  1. Write a test for a debounced + API call combo (search input triggers API only once).
  2. Write a test for throttled + scroll event with mock API updates.
  3. Write a test for mocking GraphQL mutation (e.g., addUser).
  4. Write a test for handling GraphQL query + mutation together with mocks.
  5. Write a test for a component that shows loading, success, and error states depending on API.
  6. Write a test for a multi-field form with conditional validation (email required only if checkbox checked).
  7. Write a test that covers custom async validation (e.g., username availability check via API).
  8. Write a test that checks coverage excludes a utility file (using coveragePathIgnorePatterns).
  9. Write a test that enforces global coverage threshold (line > 90%, branch > 80%).
  10. Write a test suite that uses jest.runAllTimers + API mocks together to simulate a delayed API call with timeout handling.