# JEST Asymmetric Matchers

1. **<mark>Introduction</mark>**  
    Asymmetric matchers in Jest are matchers that do not require an exact equality between the actual value and the expected value. Instead, they allow for more flexible matching conditions. This is useful when you want to assert that an object or value meets certain criteria but don't need it to be an exact match.  
    Jest provides a few built-in asymmetric matchers, such as `expect.anything()`, `expect.any()`, `expect.arrayContaining()`, `expect.objectContaining()`, `expect.stringContaining()`, and `expect.stringMatching()`.  
    Asymmetric matchers can be particularly useful when working with complex data structures or when the exact value isn't as important as the presence of certain elements or properties. They provide a powerful way to write more flexible and maintainable tests.
    
2. **<mark>expect.anything()</mark>**
    
    Matches anything except `null` or `undefined`.
    
    ```javascript
    test('matches anything except null or undefined', () => {
      expect(42).toEqual(expect.anything());
      expect('string').toEqual(expect.anything());
      expect(null).not.toEqual(expect.anything());
      expect(undefined).not.toEqual(expect.anything());
    });
    ```
    
3. **<mark>expect.any(constructor)</mark>**
    
    Matches anything that is of the type provided.
    
    ```javascript
    test('matches any number', () => {
      expect(42).toEqual(expect.any(Number));
      expect('string').not.toEqual(expect.any(Number));
    });
    ```
    
4. **<mark>expect.arrayContaining(array)</mark>**
    
    Matches if the received array contains all of the elements in the expected array.
    
    ```javascript
    test('matches arrays that contain the expected elements', () => {
      expect([1, 2, 3]).toEqual(expect.arrayContaining([2, 3]));
      expect([1, 2, 3]).not.toEqual(expect.arrayContaining([4]));
    });
    ```
    
5. **<mark>expect.objectContaining(object)</mark>**
    
    Matches if the received object contains all of the key/value pairs in the expected object.
    
    ```javascript
    test('matches objects that contain the expected key/value pairs', () => {
      expect({a: 1, b: 2, c: 3}).toEqual(expect.objectContaining({a: 1, c: 3}));
      expect({a: 1, b: 2, c: 3}).not.toEqual(expect.objectContaining({d: 4}));
    });
    ```
    
6. **<mark>expect.stringContaining(string)</mark>**
    
    Matches if the received string contains the expected substring.
    
    ```javascript
    test('matches strings that contain the expected substring', () => {
      expect('hello world').toEqual(expect.stringContaining('hello'));
      expect('hello world').not.toEqual(expect.stringContaining('goodbye'));
    });
    ```
    
7. **<mark>expect.stringMatching(regex)</mark>**
    
    Matches if the received string matches the expected regular expression.
    
    ```javascript
    test('matches strings that match the expected regex', () => {
      expect('hello world').toEqual(expect.stringMatching(/hello/));
      expect('hello world').not.toEqual(expect.stringMatching(/goodbye/));
    });
    ```
