JEST Assignment- 1

Basic Questions

  1. Write a test that checks if sum(2,3) returns 5 using toBe.
  2. Write a test for an object {name:”Sarthak”} to match using toEqual.
  3. Write a test that checks a string “hello world” contains “hello” using toMatch.
  4. Write a test that an array [1,2,3] has length 3 using toHaveLength.
  5. Write a test that a value true is toBeTruthy.
  6. Write a test that null is toBeFalsy.
  7. Write a test that 10 is greater than 5 using toBeGreaterThan.
  8. Write a test that “abc” is not equal to “xyz” using not.toBe.
  9. Write a test that a floating number 0.1+0.2 is close to 0.3 using toBeCloseTo.
  10. Write a test for checking an array contains the value apple using toContain.
  11. Write a test that a variable is defined.
  12. Write a test that a variable is undefined.
  13. Write a test for a regex match: “openAI123” should contain numbers.
  14. Write a test with beforeEach to reset a counter before every test.
  15. Write a test with afterEach to log “test completed”.
  16. Write a test suite with describe grouping 2 tests for math functions.
  17. Write a test for checking typeof “hello” is “string”.
  18. Write a test for checking division by zero returns Infinity.
  19. Write a test to check if array [2,4,6] contains only even numbers.
  20. Write a test to check an object {role:”admin”} has property “role”.

Intermediate Questions

  1. Write a test for a callback function that returns “done” using done().
  2. Write a test for a promise function that resolves with “success”.
  3. Write a test for a promise that rejects with “error”.
  4. Write a test using async/await that fetches “data” from a fake API.
  5. Write a test that mocks a function and checks if it is called once.
  6. Write a test that mocks a function and checks the return value.
  7. Write a test that checks if a mock function is called with specific arguments.
  8. Write a test using beforeAll to set up a database connection mock.
  9. Write a test using afterAll to close the mock database connection.
  10. Write a test for filtering numbers greater than 5 from [1,6,8,2].
  11. Write a test that spies on console.log and checks if it was called.
  12. Write a test that throws an error and use toThrow matcher.
  13. Write a test to check if fetchData() resolves within 2 seconds (timeout test).
  14. Write a test using jest.fn() that multiplies input by 2.
  15. Write a test that checks jest.clearAllMocks() resets mock calls.
  16. Write a test for module alias import using jest.config.js.
  17. Write a test that runs in watch mode and updates output on file change.
  18. Write a test that checks coverage report shows 100% for a simple function.
  19. Write a test that runs only tests matching name “math” using –testNamePattern.
  20. Write a test that runs only files with name “sum.test.js” using –testPathPattern.

 Advanced Questions

  1. Write a test to mock an HTTP request using jest.mock() and return fake data.
  2. Write a test that mocks a database query and asserts result length.
  3. Write a test to simulate user login flow with async/await and mocks.
  4. Write a test that checks for memory leaks using –detectOpenHandles.
  5. Write a test for a class Calculator with methods add, subtract, multiply.
  6. Write a test to mock a timer function using jest.useFakeTimers.
  7. Write a test that advances timers using jest.advanceTimersByTime.
  8. Write a test that uses snapshot testing for a React component.
  9. Write a test that compares 2 JSON outputs using snapshot testing.
  10. Write a test that checks debug mode logs using –verbose.