JEST Assignment- 3
Basic Questions
- Write a test for a function using setTimeout and check it executes after 1s (use jest.useFakeTimers).
- Write a test for a function using setInterval and ensure it runs 3 times in 3s.
- Write a test that clears an interval using clearInterval.
- Write a test that simulates a delayed log (setTimeout) and flushes it with jest.runAllTimers().
- Write a test that cancels a setTimeout before it executes.
- Write a test that checks a function runs immediately when using jest.runOnlyPendingTimers().Basic Questions
- Write a test that simulates a debounced function firing only once despite multiple calls.
- Write a test that simulates a throttled function firing at intervals.
- Write a test that mocks a REST API with fetch returning {status:”ok”}.
- Write a test that mocks a REST API error response.
- Write a test that mocks an Axios get request returning {id:1,name:”Sarthak”}.
- Write a test that mocks Axios post request and verifies payload.
- Write a test that simulates network timeout error in Axios.
- Write a test that renders a React component with a loading state before API resolves.
- Write a test that renders a React component with an error state when API fails.
- Write a test for a form where required field “username” shows error when empty.
- Write a test that validates email format in a form.
- Write a test that validates password min length in a form.
- Write a test that checks snapshot for a form with 2 fields.
- Write a test that ensures coverage report runs with –coverage.
Intermediate Questions
- Write a test for a debounced search box in React and ensure only last call executes.
- Write a test for a throttled scroll handler and ensure calls are limited.
- Write a test that uses jest.advanceTimersByTime(2000) to simulate time travel.
- Write a test for mocking multiple API calls in sequence (axios.get).
- Write a test that simulates a failed fetch and shows “Network Error”.
- Write a test for mocking a GraphQL query using Apollo MockedProvider.
- Write a test for a GraphQL error response (errors:[…]).
- Write a test for a React component that shows “Loading…” until GraphQL resolves.
- Write a test for a React form submission calling onSubmit with values.
- Write a test that ensures form error message disappears after correcting input.
- Write a test for custom validation that password must contain @.
- Write a test for form submission that triggers API call with values.
- Write a test for form submission failure that shows “Try again” error.
- Write a test that generates coverage and checks at least one branch is covered.
- Write a test that analyzes coverage for a utility function (sum.js).
- Write a test that ensures all component props are covered in snapshot.
- Write a test that uses jest.mock to replace a form validation helper function.
- Write a test for an API call with retries (mock fails once then succeeds).
- Write a test for Axios interceptor mock that modifies headers.
- Write a test that sets coverage threshold in jest.config.js and fails below 90%.
Advanced Questions
- Write a test for a debounced + API call combo (search input triggers API only once).
- Write a test for throttled + scroll event with mock API updates.
- Write a test for mocking GraphQL mutation (e.g., addUser).
- Write a test for handling GraphQL query + mutation together with mocks.
- Write a test for a component that shows loading, success, and error states depending on API.
- Write a test for a multi-field form with conditional validation (email required only if checkbox checked).
- Write a test that covers custom async validation (e.g., username availability check via API).
- Write a test that checks coverage excludes a utility file (using coveragePathIgnorePatterns).
- Write a test that enforces global coverage threshold (line > 90%, branch > 80%).
- Write a test suite that uses jest.runAllTimers + API mocks together to simulate a delayed API call with timeout handling.