deno.land / std@0.224.0 / expect / _extend_test.ts

_extend_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { expect } from "./expect.ts";import type { MatcherContext, Tester } from "./_types.ts";
declare module "./_types.ts" { interface Expected { toEqualBook: (expected: unknown) => ExtendMatchResult; }}
class Author { public name: string;
constructor(name: string) { this.name = name; }}
class Book { public name: string; public authors: Array<Author>;
constructor(name: string, authors: Array<Author>) { this.name = name; this.authors = authors; }}
const areAuthorsEqual: Tester = (a: unknown, b: unknown) => { const isAAuthor = a instanceof Author; const isBAuthor = b instanceof Author;
if (isAAuthor && isBAuthor) { return a.name === b.name; } else if (isAAuthor === isBAuthor) { return undefined; } else { return false; }};
const areBooksEqual: Tester = function ( this: MatcherContext, a: unknown, b: unknown, customTesters: Tester[],) { const isABook = a instanceof Book; const isBBook = b instanceof Book;
if (isABook && isBBook) { return (a.name === b.name && this.equal(a.authors, b.authors, { customTesters: customTesters })); } else if (isABook === isBBook) { return undefined; } else { return false; }};
expect.addEqualityTesters([ areAuthorsEqual, areBooksEqual,]);
expect.extend({ toEqualBook(context, expected) { const actual = context.value as Book; const result = context.equal(expected, actual, { customTesters: context.customTesters, });
return { message: () => `Expected Book object: ${expected.name}. Actual Book object: ${actual.name}`, pass: result, }; },});
Deno.test("expect.extend() api test case", () => { const book1a = new Book("Book 1", [ new Author("Author 1"), new Author("Author 2"), ]); const book1b = new Book("Book 1", [ new Author("Author 1"), new Author("Author 2"), ]); const book2 = new Book("Book 2", [ new Author("Author 1"), new Author("Author 2"), ]);
expect(book1a).toEqualBook(book1b); expect(book1a).not.toEqualBook(book2); expect(book1a).not.toEqualBook(1); expect(book1a).not.toEqualBook(null);});
std

Version Info

Tagged at
6 months ago