-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(expect): add asymmetric matchers (any, anything, arrayContaining) (
- Loading branch information
1 parent
1e0764d
commit 5d15ab7
Showing
6 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
|
||
class Cat {} | ||
Deno.test("expect.any()", () => { | ||
expect(new Cat()).toEqual(expect.any(Cat)); | ||
expect(new Cat()).toEqual(expect.any(Object)); | ||
expect("Hello").toEqual(expect.any(String)); | ||
expect(1).toEqual(expect.any(Number)); | ||
expect(() => {}).toEqual(expect.any(Function)); | ||
expect(false).toEqual(expect.any(Boolean)); | ||
expect(BigInt(1)).toEqual(expect.any(BigInt)); | ||
expect(Symbol("sym")).toEqual(expect.any(Symbol)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
import { AssertionError, assertThrows } from "../assert/mod.ts"; | ||
import { fn } from "./fn.ts"; | ||
|
||
Deno.test("expect.anything() as a parameter of toEqual()", () => { | ||
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); | ||
}); | ||
|
||
Deno.test("expect.anything() as a parameter of toHaveBeenCalled()", () => { | ||
const mockFn = fn(); | ||
mockFn(3); | ||
expect(mockFn).toHaveBeenCalledWith(expect.anything()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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, | ||
) {} | ||
abstract equals(other: unknown): boolean; | ||
} | ||
|
||
export class Anything extends AsymmetricMatcher<void> { | ||
equals(other: unknown): boolean { | ||
return other !== null && other !== undefined; | ||
} | ||
} | ||
|
||
export function anything(): Anything { | ||
return new Anything(); | ||
} | ||
|
||
export class Any extends AsymmetricMatcher<any> { | ||
constructor(value: unknown) { | ||
if (value === undefined) { | ||
throw TypeError("Expected a constructor function"); | ||
} | ||
super(value); | ||
} | ||
equals(other: unknown): boolean { | ||
if (typeof other === "object") { | ||
return other instanceof this.value; | ||
} else { | ||
if (this.value == Number) { | ||
return typeof other === "number"; | ||
} | ||
|
||
if (this.value == String) { | ||
return typeof other == "string"; | ||
} | ||
|
||
if (this.value == Number) { | ||
return typeof other == "number"; | ||
} | ||
|
||
if (this.value == Function) { | ||
return typeof other == "function"; | ||
} | ||
|
||
if (this.value == Boolean) { | ||
return typeof other == "boolean"; | ||
} | ||
|
||
if (this.value == BigInt) { | ||
return typeof other == "bigint"; | ||
} | ||
|
||
if (this.value == Symbol) { | ||
return typeof other == "symbol"; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters