diff --git a/expect/_anything_test.ts b/expect/_anything_test.ts new file mode 100644 index 0000000000000..178d340441944 --- /dev/null +++ b/expect/_anything_test.ts @@ -0,0 +1,20 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { expect } from "./expect.ts"; +import { AssertionError, assertThrows } from "../assert/mod.ts"; + +Deno.test("expect().anything()", () => { + expect(null).not.toEqual(expect.anything()) + expect(undefined).not.toEqual(expect.anything()) + expect(1).toEqual(expect.anything()) + + assertThrows(() => { + expect(null).toEqual(expect.anything()) + }, AssertionError); + assertThrows(() => { + expect(undefined).toEqual(expect.anything()) + }, AssertionError); + assertThrows(() => { + expect(1).not.toEqual(expect.anything()) + }, AssertionError); +}); diff --git a/expect/_asymetric_matchers.ts b/expect/_asymetric_matchers.ts new file mode 100644 index 0000000000000..ffe0fde597ec2 --- /dev/null +++ b/expect/_asymetric_matchers.ts @@ -0,0 +1,9 @@ +export class Anything { + equals(other: unknown): boolean { + return other !== null && other !== undefined; + } +} + +export function generateAnything(): Anything { + return new Anything(); +} diff --git a/expect/_equal.ts b/expect/_equal.ts index 1765949327d86..8f1c732302220 100644 --- a/expect/_equal.ts +++ b/expect/_equal.ts @@ -3,6 +3,7 @@ // This file is copied from `std/assert`. import { EqualOptions } from "./_types.ts"; +import { Anything } from "./_asymetric_matchers.ts"; function isKeyedCollection(x: unknown): x is Set { return [Symbol.iterator, "size"].every((k) => k in (x as Set)); @@ -52,6 +53,9 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean { ) { return String(a) === String(b); } + if (b instanceof Anything) { + return (b as Anything).equals(a); + } if (a instanceof Date && b instanceof Date) { const aTime = a.getTime(); const bTime = b.getTime(); @@ -95,7 +99,7 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean { if (!strictCheck) { if (aLen > 0) { for (let i = 0; i < aKeys.length; i += 1) { - const key = aKeys[i]; + const key = aKeys[i]!; if ( (key in a) && (a[key as keyof typeof a] === undefined) && !(key in b) @@ -107,7 +111,7 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean { if (bLen > 0) { for (let i = 0; i < bKeys.length; i += 1) { - const key = bKeys[i]; + const key = bKeys[i]!; if ( (key in b) && (b[key as keyof typeof b] === undefined) && !(key in a) diff --git a/expect/expect.ts b/expect/expect.ts index 739776a0a3323..4ee147945a9b7 100644 --- a/expect/expect.ts +++ b/expect/expect.ts @@ -47,6 +47,7 @@ import { toThrow, } from "./_matchers.ts"; import { isPromiseLike } from "./_utils.ts"; +import {generateAnything} from "./_asymetric_matchers.ts"; const matchers: Record = { lastCalledWith: toHaveBeenLastCalledWith, @@ -168,3 +169,4 @@ export function expect(value: unknown, customMessage?: string): Expected { } expect.addEqualityTesters = addCustomEqualityTesters; +expect.anything = generateAnything;