Skip to content

Commit

Permalink
Add expect.anything() (denoland#3964)
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
javihernant committed Feb 21, 2024
1 parent ac82d73 commit ea17f72
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
20 changes: 20 additions & 0 deletions expect/_anything_test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
9 changes: 9 additions & 0 deletions expect/_asymetric_matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Anything {
equals(other: unknown): boolean {
return other !== null && other !== undefined;
}
}

export function generateAnything(): Anything {
return new Anything();
}
8 changes: 6 additions & 2 deletions expect/_equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> {
return [Symbol.iterator, "size"].every((k) => k in (x as Set<unknown>));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions expect/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
toThrow,
} from "./_matchers.ts";
import { isPromiseLike } from "./_utils.ts";
import {generateAnything} from "./_asymetric_matchers.ts";

const matchers: Record<MatcherKey, Matcher> = {
lastCalledWith: toHaveBeenLastCalledWith,
Expand Down Expand Up @@ -168,3 +169,4 @@ export function expect(value: unknown, customMessage?: string): Expected {
}

expect.addEqualityTesters = addCustomEqualityTesters;
expect.anything = generateAnything;

0 comments on commit ea17f72

Please sign in to comment.