Skip to content

Feature Request: .toMatchArray() #231

@kara-ryli

Description

@kara-ryli

I was looking for a shortcut to check if an Array exactly matches the contents including order (but was not necessarily strictly equal). The matcher would have test cases like this:

describe('matchArray', () => {

  it('matches arrays', () => {
    expect(matchArray(['a', 'b'], ['a', 'b'])).toBeTruthy();
    expect(matchArray(['a', 'b', 'c'], ['a', 'b'])).toBeFalsy();
    expect(matchArray(['a', 'b'], ['a', 'b', 'c'])).toBeFalsy();
    expect(matchArray(['a', 'b'], ['b', 'a'])).toBeFalsy();
  });

  it('short-circuits via equality', () => {
    const a = [NaN]; // NaN === NaN returns false
    expect(matchArray(a, a)).toBeTruthy();
  });

  it('uses strict equality', () => {
    expect(matchArray(['1'], [1])).toBeFalsy();
    expect(matchArray([{}], [{}])).toBeFalsy();
    const a = {};
    expect(matchArray([a], [a])).toBeTruthy();
  });

});

I ended up writing this myself but would be happy to contribute it if you'll accept it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions