Consolidate Jest arrayContaining assertions

JavaScript pattern

expect.arrayContaining can be used to validate an array containing multiple different elements, so multiple statements are not required.


Apply with the Grit CLI
grit apply jest_array_containing

Basic example

BEFORE
describe('test', () => {
  it('consolidates', async () => {
    const values = ['console.log($9o)', 'console.log($x)', 'PatternWithArgs($arg)'];
    const anotherValues = ['nine'];
    expect(values).toEqual(expect.arrayContaining([expect.stringContaining('console.log($9o)')]));
    expect(anotherValues).toEqual(expect.arrayContaining([expect.stringContaining('nine')]));
    expect(values).toEqual(expect.arrayContaining([expect.stringContaining('console.log($x)')]));
    expect(values).toEqual(
      expect.arrayContaining([expect.stringContaining('PatternWithArgs($arg)')]),
    );
  });
});
AFTER
describe('test', () => {
  it('consolidates', async () => {
    const values = ['console.log($9o)', 'console.log($x)', 'PatternWithArgs($arg)'];
    const anotherValues = ['nine'];
    expect(anotherValues).toEqual(expect.arrayContaining([expect.stringContaining('nine')]));
    expect(values).toEqual(
      expect.arrayContaining([
        expect.stringContaining('console.log($9o)'),
        expect.stringContaining('console.log($x)'),
        expect.stringContaining('PatternWithArgs($arg)'),
      ]),
    );
  });
});