JEST Assignment- 2

Basic Questions

  1. Create a mock function using jest.fn() and test if it is called once.
  2. Write a test that mocks a function to return “Hello World”.
  3. Write a test using jest.fn() to add two numbers and check the result.
  4. Write a test that checks if a mocked function was called with arguments (10, 20).
  5. Write a test that resets a mock using mockReset and ensures it has no calls.
  6. Write a test using jest.spyOn to spy on Math.random.
  7. Write a test mocking a simple fetch function returning {id:1,name:”Sarthak”}.
  8. Write a test mocking axios.get to return a fake user list.
  9. Write a test that mocks Date.now() to always return 123456.
  10. Write a test that spies on console.log and checks if “Hello” is logged.
  11. Write a test that mocks a module math.js with jest.mock().
  12. Write a test that provides a manual mock for a custom function.
  13. Write a test that checks a button click changes text in a React component.
  14. Write a test using fireEvent.change to update an input value.
  15. Write a test using userEvent.click to check checkbox toggle.
  16. Write a test for a React component that renders with props {title:”Test”}.
  17. Write a test to check a child component receives updated props.
  18. Write a test that verifies snapshot of a small component (button).
  19. Write a test that checks toMatchInlineSnapshot with a string output.
  20. Write a test that updates a snapshot when component structure changes.

Intermediate Questions

  1. Write a test that mocks an API call in React and verifies UI updates.
  2. Write a test that simulates typing text into an input box using userEvent.type.
  3. Write a test that mocks a context value in a React component.
  4. Write a test that mocks a custom React hook.
  5. Write a test that simulates submitting a form and checks validation error.
  6. Write a test for Vue component emitting “submit” event with a value.
  7. Write a test for Vue slot rendering “Hello Slot”.
  8. Write a test that spies on a service method in Angular component.
  9. Write a test that mocks an Angular dependency injection service.
  10. Write a test that mocks a Next.js getStaticProps.
  11. Write a test that mocks a Next.js getServerSideProps.
  12. Write a test that mocks a Next.js API route returning fake data.
  13. Write a test that verifies Redux store state is updated after dispatch.
  14. Write a test that mocks Vuex store state and checks getter values.
  15. Write a test that checks prop updates trigger re-render in React.
  16. Write a test that mocks fetch request failure and checks error UI.
  17. Write a test for checking multiple snapshots of different React states.
  18. Write a test that uses toMatchSnapshot for a form with 3 fields.
  19. Write a test for managing large snapshots by splitting into smaller parts.
  20. Write a test for mocking timers inside React component with setTimeout.

Advanced Questions

  1. Write a test for a React component that fetches data and updates UI using jest.mock(axios).
  2. Write a test to mock multiple API calls and check the order of responses.
  3. Write a test that mocks Redux state + API together for a component.
  4. Write a test that mocks Vue component with nested emitters.
  5. Write a test that mocks Angular service returning observable.
  6. Write a test for a Next.js SSR component with mocked session user.
  7. Write a test that verifies form validation errors with userEvent + async.
  8. Write a test using jest.spyOn to intercept a private class method.
  9. Write a test that compares inline snapshots for multiple React states.
  10. Write a test that uses –updateSnapshot CLI to refresh outdated snapshots.