JEST Assignment- 1
Basic Questions
- Write a test that checks if sum(2,3) returns 5 using toBe.
- Write a test for an object {name:”Sarthak”} to match using toEqual.
- Write a test that checks a string “hello world” contains “hello” using toMatch.
- Write a test that an array [1,2,3] has length 3 using toHaveLength.
- Write a test that a value true is toBeTruthy.
- Write a test that null is toBeFalsy.
- Write a test that 10 is greater than 5 using toBeGreaterThan.
- Write a test that “abc” is not equal to “xyz” using not.toBe.
- Write a test that a floating number 0.1+0.2 is close to 0.3 using toBeCloseTo.
- Write a test for checking an array contains the value apple using toContain.
- Write a test that a variable is defined.
- Write a test that a variable is undefined.
- Write a test for a regex match: “openAI123” should contain numbers.
- Write a test with beforeEach to reset a counter before every test.
- Write a test with afterEach to log “test completed”.
- Write a test suite with describe grouping 2 tests for math functions.
- Write a test for checking typeof “hello” is “string”.
- Write a test for checking division by zero returns Infinity.
- Write a test to check if array [2,4,6] contains only even numbers.
- Write a test to check an object {role:”admin”} has property “role”.
Intermediate Questions
- Write a test for a callback function that returns “done” using done().
- Write a test for a promise function that resolves with “success”.
- Write a test for a promise that rejects with “error”.
- Write a test using async/await that fetches “data” from a fake API.
- Write a test that mocks a function and checks if it is called once.
- Write a test that mocks a function and checks the return value.
- Write a test that checks if a mock function is called with specific arguments.
- Write a test using beforeAll to set up a database connection mock.
- Write a test using afterAll to close the mock database connection.
- Write a test for filtering numbers greater than 5 from [1,6,8,2].
- Write a test that spies on console.log and checks if it was called.
- Write a test that throws an error and use toThrow matcher.
- Write a test to check if fetchData() resolves within 2 seconds (timeout test).
- Write a test using jest.fn() that multiplies input by 2.
- Write a test that checks jest.clearAllMocks() resets mock calls.
- Write a test for module alias import using jest.config.js.
- Write a test that runs in watch mode and updates output on file change.
- Write a test that checks coverage report shows 100% for a simple function.
- Write a test that runs only tests matching name “math” using –testNamePattern.
- Write a test that runs only files with name “sum.test.js” using –testPathPattern.
Advanced Questions
- Write a test to mock an HTTP request using jest.mock() and return fake data.
- Write a test that mocks a database query and asserts result length.
- Write a test to simulate user login flow with async/await and mocks.
- Write a test that checks for memory leaks using –detectOpenHandles.
- Write a test for a class Calculator with methods add, subtract, multiply.
- Write a test to mock a timer function using jest.useFakeTimers.
- Write a test that advances timers using jest.advanceTimersByTime.
- Write a test that uses snapshot testing for a React component.
- Write a test that compares 2 JSON outputs using snapshot testing.
- Write a test that checks debug mode logs using –verbose.