jest


1. Testing a Simple Function

const add = (a, b) => a + b;

test('Adds two numbers', () => {
  expect(add(1, 2)).toBe(3);
});

2. Testing an Object Method

class Person {
  constructor(name) {
    this.name = name;
  }

  greet() {
    return `Hello, ${this.name}!`;
  }
}

test('Greets a person', () => {
  const person = new Person('Jane');
  expect(person.greet()).toBe('Hello, Jane!');
});

3. Testing a Promise

4. Testing an HTTP Request

5. Testing a React Component

6. Testing a React Component with Props

7. Mocking a Function

8. Mocking an Object Method

9. Spying on a Function

10. Stubbing a Function with a Return Value

11. Throwing an Error from a Stubbed Function

12. Testing Async Code with Jest's Fake Timers

13. Testing DOM Events

14. Testing Class Constructors

15. Testing Class Methods

16. Testing Static Class Methods

17. Testing Getters and Setters

18. Testing Inheritance

19. Testing Multiple Assertions

20. Testing Snapshots

21. Testing Arrays and Objects with toEqual

22. Testing Floating Point Numbers with toCloseTo

23. Testing Exceptions

24. Testing Jest's Custom Matchers

25. Testing Async Code with Async/Await