Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added arrayEqualsWithoutOrder #15421

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
any,
anything,
arrayContaining,
arrayEqualsWithoutOrder,
arrayNotContaining,
arrayNotEqualsWithoutOrder,
closeTo,
notCloseTo,
objectContaining,
Expand Down Expand Up @@ -137,6 +139,50 @@
jestExpect(anything().toAsymmetricMatcher()).toBe('Anything');
});

test('ArrayEqualsWithoutOrder matches', () => {
for (const test of [
arrayEqualsWithoutOrder([]).asymmetricMatch('jest'),
arrayEqualsWithoutOrder(['foo']).asymmetricMatch(['foo']),
arrayEqualsWithoutOrder(['foo']).asymmetricMatch(['foo', 'bar']),
arrayEqualsWithoutOrder([]).asymmetricMatch({}),
]) {
jestExpect(test).toEqual(true);
}
});

test('ArrayEqualsWithoutOrder does not match', () => {
jestExpect(arrayEqualsWithoutOrder(['foo']).asymmetricMatch(['bar'])).toBe(false);

Check failure on line 154 in packages/expect/src/__tests__/asymmetricMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `false` with `⏎····false,⏎··`
});

test('ArrayEqualsWithoutOrder throws for non-arrays', () => {
jestExpect(() => {
// @ts-expect-error: Testing runtime error
arrayEqualsWithoutOrder('foo').asymmetricMatch([]);
}).toThrow("You must provide an array to ArrayEqualsWithoutOrder, not 'string'.");

Check failure on line 161 in packages/expect/src/__tests__/asymmetricMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `"You·must·provide·an·array·to·ArrayEqualsWithoutOrder,·not·'string'."` with `⏎····"You·must·provide·an·array·to·ArrayEqualsWithoutOrder,·not·'string'.",⏎··`
});

test('ArrayNotEqualsWithoutOrder matches', () => {
jestExpect(arrayNotEqualsWithoutOrder(['foo']).asymmetricMatch(['bar'])).toBe(true);

Check failure on line 165 in packages/expect/src/__tests__/asymmetricMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `true` with `⏎····true,⏎··`
});

test('ArrayNotEqualsWithoutOrder does not match', () => {
for (const test of [
arrayNotEqualsWithoutOrder([]).asymmetricMatch('jest'),
arrayNotEqualsWithoutOrder(['foo']).asymmetricMatch(['foo']),
arrayNotEqualsWithoutOrder(['foo']).asymmetricMatch(['foo', 'bar']),
arrayNotEqualsWithoutOrder([]).asymmetricMatch({}),
]) {
jestExpect(test).toEqual(false);
}
});

test('ArrayNotEqualsWithoutOrder throws for non-arrays', () => {
jestExpect(() => {
// @ts-expect-error: Testing runtime error
arrayNotEqualsWithoutOrder('foo').asymmetricMatch([]);
}).toThrow("You must provide an array to ArrayNotEqualsWithoutOrder, not 'string'.");

Check failure on line 183 in packages/expect/src/__tests__/asymmetricMatchers.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `"You·must·provide·an·array·to·ArrayNotEqualsWithoutOrder,·not·'string'."` with `⏎····"You·must·provide·an·array·to·ArrayNotEqualsWithoutOrder,·not·'string'.",⏎··`
});

test('ArrayContaining matches', () => {
for (const test of [
arrayContaining([]).asymmetricMatch('jest'),
Expand Down
44 changes: 44 additions & 0 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,46 @@
}
}

class ArrayEqualsWithoutOrder extends AsymmetricMatcher<Array<unknown>> {
constructor(sample: Array<unknown>, inverse = false) {
super(sample, inverse);
}

asymmetricMatch(other: unknown) {
if (!Array.isArray(this.sample)) {
throw new TypeError(
`You must provide an array to ${this.toString()}, not '${typeof this
.sample}'.`,
);
}

const matcherContext = this.getMatcherContext();
const result =
this.sample.length === 0 ||
(Array.isArray(other) &&

Check failure on line 238 in packages/expect/src/asymmetricMatchers.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
this.sample.every(item =>
other.some(another =>
equals(item, another, matcherContext.customTesters),
),
) &&

Check failure on line 243 in packages/expect/src/asymmetricMatchers.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
other.every(item =>

Check failure on line 244 in packages/expect/src/asymmetricMatchers.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
this.sample.some(another =>

Check failure on line 245 in packages/expect/src/asymmetricMatchers.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
equals(item, another, matcherContext.customTesters),
),
));

return this.inverse ? !result : result;
}

toString() {
return `Array${this.inverse ? 'Not' : ''}Containing`;
}

override getExpectedType() {
return 'array';
}
}

class ObjectContaining extends AsymmetricMatcher<
Record<string | symbol, unknown>
> {
Expand Down Expand Up @@ -377,6 +417,10 @@
new ArrayContaining(sample);
export const arrayNotContaining = (sample: Array<unknown>): ArrayContaining =>
new ArrayContaining(sample, true);
export const arrayEqualsWithoutOrder = (sample: Array<unknown>): ArrayEqualsWithoutOrder =>

Check failure on line 420 in packages/expect/src/asymmetricMatchers.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `sample:·Array<unknown>):·ArrayEqualsWithoutOrder·=>·⏎·` with `⏎··sample:·Array<unknown>,⏎):·ArrayEqualsWithoutOrder·=>`
new ArrayEqualsWithoutOrder(sample);
export const arrayNotEqualsWithoutOrder = (sample: Array<unknown>): ArrayEqualsWithoutOrder =>

Check failure on line 422 in packages/expect/src/asymmetricMatchers.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `sample:·Array<unknown>):·ArrayEqualsWithoutOrder·=>·⏎·` with `⏎··sample:·Array<unknown>,⏎):·ArrayEqualsWithoutOrder·=>`
new ArrayEqualsWithoutOrder(sample, true);
export const objectContaining = (
sample: Record<string, unknown>,
): ObjectContaining => new ObjectContaining(sample);
Expand Down
1 change: 1 addition & 0 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export interface AsymmetricMatchers {
any(sample: unknown): AsymmetricMatcher;
anything(): AsymmetricMatcher;
arrayContaining(sample: Array<unknown>): AsymmetricMatcher;
arrayEqualsWithoutOrder(sample: Array<unknown>): AsymmetricMatcher;
closeTo(sample: number, precision?: number): AsymmetricMatcher;
objectContaining(sample: Record<string, unknown>): AsymmetricMatcher;
stringContaining(sample: string): AsymmetricMatcher;
Expand Down
Loading