Skip to content

Commit

Permalink
Add expect.arrayContaining()
Browse files Browse the repository at this point in the history
  • Loading branch information
javihernant committed Feb 22, 2024
1 parent a962d2f commit 893ac97
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions expect/_array_containing_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect } from "./expect.ts";

Deno.test("expect.arrayContaining() with array of numbers", () => {
const arr = [1, 2, 3];
expect([1, 2, 3, 4]).toEqual(expect.arrayContaining(arr));
expect([4, 5, 6]).not.toEqual(expect.arrayContaining(arr));
expect([1, 2, 3]).toEqual(expect.arrayContaining(arr));
});

Deno.test("expect.arrayContaining() with array of mixed types", () => {
const arr = [1, 2, "hello"];
expect([1, 2, 3, "hello", "bye"]).toEqual(expect.arrayContaining(arr));
expect([4, "bye"]).not.toEqual(expect.arrayContaining(arr));
});
16 changes: 16 additions & 0 deletions expect/_asymmetric_matchers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-explicit-any

abstract class AsymmetricMatcher<T> {
constructor(
protected value: T,
Expand Down Expand Up @@ -61,3 +64,16 @@ export class Any extends AsymmetricMatcher<any> {
export function any(c: unknown): Any {
return new Any(c);
}

export class ArrayContaining extends AsymmetricMatcher<any[]> {
constructor(arr: any[]) {
super(arr);
}
equals(other: any[]): boolean {
return this.value.every((e) => other.includes(e));
}
}

export function arrayContaining(c: any[]): ArrayContaining {
return new ArrayContaining(c);
}
5 changes: 4 additions & 1 deletion expect/_equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is copied from `std/assert`.

import { EqualOptions } from "./_types.ts";
import { Any, Anything } from "./_asymmetric_matchers.ts";
import { Any, Anything, ArrayContaining } from "./_asymmetric_matchers.ts";

function isKeyedCollection(x: unknown): x is Set<unknown> {
return [Symbol.iterator, "size"].every((k) => k in (x as Set<unknown>));
Expand Down Expand Up @@ -59,6 +59,9 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean {
if (b instanceof Any) {
return b.equals(a);
}
if (b instanceof ArrayContaining && a instanceof Array) {
return b.equals(a);
}
if (a instanceof Date && b instanceof Date) {
const aTime = a.getTime();
const bTime = b.getTime();
Expand Down
3 changes: 2 additions & 1 deletion expect/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
toThrow,
} from "./_matchers.ts";
import { isPromiseLike } from "./_utils.ts";
import { any, anything } from "./_asymmetric_matchers.ts";
import { any, anything, arrayContaining } from "./_asymmetric_matchers.ts";

const matchers: Record<MatcherKey, Matcher> = {
lastCalledWith: toHaveBeenLastCalledWith,
Expand Down Expand Up @@ -171,3 +171,4 @@ export function expect(value: unknown, customMessage?: string): Expected {
expect.addEqualityTesters = addCustomEqualityTesters;
expect.anything = anything;
expect.any = any;
expect.arrayContaining = arrayContaining;

0 comments on commit 893ac97

Please sign in to comment.