# JEST expect function

1. **<mark>Basic Usage</mark>**  
    In the Jest framework, `expect` is a function used to create assertions in tests. Assertions are statements that evaluate whether a given condition is true. If the condition is false, the test fails.  
    The `expect` function is central to writing tests in Jest, providing a wide array of matchers to perform assertions on various data types and structures.  
    Here's a breakdown of how `expect` works and its common usage,
    
    ```bash
    expect(value)
    ```
    
2. **<mark>Common Matchers</mark>**  
    i) **toBe(value)**: Checks for strict equality using [`Object.is`](http://Object.is).
    
    ```javascript
    expect(2 + 2).toBe(4);
    ```
    
    ii) **toEqual(value)**: Checks for deep equality. Useful for comparing objects and arrays.
    
    ```javascript
    const obj = { a: 1, b: 2 };
    expect(obj).toEqual({ a: 1, b: 2 });
    ```
    
    iii) **toBeNull()**: Checks if the value is `null`.
    
    ```javascript
    expect(null).toBeNull();
    ```
    
    iv) **toBeUndefined()**: Checks if the value is `undefined`.
    
    ```javascript
    expect(undefined).toBeUndefined();
    ```
    
    v) **toBeDefined()**: Checks if the value is defined.
    
    ```javascript
    expect(definedValue).toBeDefined();
    ```
    
    vi) **toBeTruthy()**: Checks if the value is truthy.
    
    ```javascript
    expect(true).toBeTruthy();
    ```
    
    vii) **toBeFalsy()**: Checks if the value is falsy.
    
    ```javascript
    expect(false).toBeFalsy();
    ```
    
    viii) **toContain(item)**: Checks if an array or iterable contains the specified item.
    
    ```javascript
    expect([1, 2, 3]).toContain(2);
    ```
    
    ix) **toHaveLength(number)**: Checks the length of an array or string.
    
    ```javascript
    expect('hello').toHaveLength(5);
    ```
    
    x) **toMatch(regexpOrString)**: Checks if a string matches a regular expression or string.
    
    ```javascript
    expect('hello world').toMatch(/world/);
    ```
    
3. **<mark>Asynchronous Code</mark>**  
    i) **Async/Await**
    
    ```javascript
    test('fetches data', async () => {
      const data = await fetchData();
      expect(data).toBeDefined();
    });
    ```
    
    ii) **.resolves / .rejects**
    
    ```javascript
    test('fetches data resolves', () => {
      return expect(fetchData()).resolves.toBe('data');
    });
    
    test('fetches data rejects', () => {
      return expect(fetchData()).rejects.toMatch('error');
    });
    ```
    
4. **<mark>Custom Matchers</mark>**
    
    You can also define custom matchers to extend Jest's functionality
    
    ```javascript
    expect.extend({
      toBeWithinRange(received, floor, ceiling) {
        const pass = received >= floor && received <= ceiling;
        if (pass) {
          return {
            message: () => `expected ${received} not to be within range ${floor} - ${ceiling}`,
            pass: true,
          };
        } else {
          return {
            message: () => `expected ${received} to be within range ${floor} - ${ceiling}`,
            pass: false,
          };
        }
      },
    });
    
    test('numeric ranges', () => {
      expect(100).toBeWithinRange(90, 110);
    });
    ```
