JEST Assignment- 2
Basic Questions
- Create a mock function using jest.fn() and test if it is called once.
- Write a test that mocks a function to return “Hello World”.
- Write a test using jest.fn() to add two numbers and check the result.
- Write a test that checks if a mocked function was called with arguments (10, 20).
- Write a test that resets a mock using mockReset and ensures it has no calls.
- Write a test using jest.spyOn to spy on Math.random.
- Write a test mocking a simple fetch function returning {id:1,name:”Sarthak”}.
- Write a test mocking axios.get to return a fake user list.
- Write a test that mocks Date.now() to always return 123456.
- Write a test that spies on console.log and checks if “Hello” is logged.
- Write a test that mocks a module math.js with jest.mock().
- Write a test that provides a manual mock for a custom function.
- Write a test that checks a button click changes text in a React component.
- Write a test using fireEvent.change to update an input value.
- Write a test using userEvent.click to check checkbox toggle.
- Write a test for a React component that renders with props {title:”Test”}.
- Write a test to check a child component receives updated props.
- Write a test that verifies snapshot of a small component (button).
- Write a test that checks toMatchInlineSnapshot with a string output.
- Write a test that updates a snapshot when component structure changes.
Intermediate Questions
- Write a test that mocks an API call in React and verifies UI updates.
- Write a test that simulates typing text into an input box using userEvent.type.
- Write a test that mocks a context value in a React component.
- Write a test that mocks a custom React hook.
- Write a test that simulates submitting a form and checks validation error.
- Write a test for Vue component emitting “submit” event with a value.
- Write a test for Vue slot rendering “Hello Slot”.
- Write a test that spies on a service method in Angular component.
- Write a test that mocks an Angular dependency injection service.
- Write a test that mocks a Next.js getStaticProps.
- Write a test that mocks a Next.js getServerSideProps.
- Write a test that mocks a Next.js API route returning fake data.
- Write a test that verifies Redux store state is updated after dispatch.
- Write a test that mocks Vuex store state and checks getter values.
- Write a test that checks prop updates trigger re-render in React.
- Write a test that mocks fetch request failure and checks error UI.
- Write a test for checking multiple snapshots of different React states.
- Write a test that uses toMatchSnapshot for a form with 3 fields.
- Write a test for managing large snapshots by splitting into smaller parts.
- Write a test for mocking timers inside React component with setTimeout.
Advanced Questions
- Write a test for a React component that fetches data and updates UI using jest.mock(axios).
- Write a test to mock multiple API calls and check the order of responses.
- Write a test that mocks Redux state + API together for a component.
- Write a test that mocks Vue component with nested emitters.
- Write a test that mocks Angular service returning observable.
- Write a test for a Next.js SSR component with mocked session user.
- Write a test that verifies form validation errors with userEvent + async.
- Write a test using jest.spyOn to intercept a private class method.
- Write a test that compares inline snapshots for multiple React states.
- Write a test that uses –updateSnapshot CLI to refresh outdated snapshots.