@plugjs/expect5 0.4.4 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +2 -2
- package/dist/cli.mjs.map +1 -1
- package/dist/execution/executor.cjs.map +1 -1
- package/dist/execution/executor.d.ts +1 -1
- package/dist/execution/executor.mjs +1 -1
- package/dist/execution/executor.mjs.map +1 -1
- package/dist/execution/setup.cjs.map +1 -1
- package/dist/execution/setup.d.ts +1 -1
- package/dist/execution/setup.mjs +1 -1
- package/dist/execution/setup.mjs.map +1 -1
- package/dist/expectation/async.cjs +58 -45
- package/dist/expectation/async.cjs.map +1 -1
- package/dist/expectation/async.d.ts +53 -52
- package/dist/expectation/async.mjs +59 -42
- package/dist/expectation/async.mjs.map +1 -1
- package/dist/expectation/diff.cjs.map +1 -1
- package/dist/expectation/diff.mjs +6 -1
- package/dist/expectation/diff.mjs.map +1 -1
- package/dist/expectation/expect.cjs +11 -165
- package/dist/expectation/expect.cjs.map +2 -2
- package/dist/expectation/expect.d.ts +10 -112
- package/dist/expectation/expect.mjs +12 -207
- package/dist/expectation/expect.mjs.map +2 -2
- package/dist/expectation/expectations.cjs +549 -0
- package/dist/expectation/expectations.cjs.map +6 -0
- package/dist/expectation/expectations.d.ts +454 -0
- package/dist/expectation/expectations.mjs +530 -0
- package/dist/expectation/expectations.mjs.map +6 -0
- package/dist/expectation/include.cjs +43 -41
- package/dist/expectation/include.cjs.map +1 -1
- package/dist/expectation/include.d.ts +3 -19
- package/dist/expectation/include.mjs +43 -41
- package/dist/expectation/include.mjs.map +1 -1
- package/dist/expectation/matchers.cjs +350 -0
- package/dist/expectation/matchers.cjs.map +6 -0
- package/dist/expectation/matchers.d.ts +375 -0
- package/dist/expectation/matchers.mjs +328 -0
- package/dist/expectation/matchers.mjs.map +6 -0
- package/dist/expectation/print.cjs.map +1 -1
- package/dist/expectation/print.d.ts +2 -2
- package/dist/expectation/print.mjs.map +1 -1
- package/dist/expectation/types.cjs +17 -23
- package/dist/expectation/types.cjs.map +1 -1
- package/dist/expectation/types.d.ts +7 -51
- package/dist/expectation/types.mjs +17 -22
- package/dist/expectation/types.mjs.map +1 -1
- package/dist/globals.d.ts +2 -2
- package/dist/index.cjs +5 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.mjs +11 -12
- package/dist/index.mjs.map +1 -1
- package/dist/test.cjs +34 -5
- package/dist/test.cjs.map +1 -1
- package/dist/test.d.ts +2 -2
- package/dist/test.mjs +35 -6
- package/dist/test.mjs.map +1 -1
- package/package.json +2 -2
- package/src/cli.mts +1 -1
- package/src/execution/executor.ts +1 -3
- package/src/execution/setup.ts +1 -3
- package/src/expectation/async.ts +116 -145
- package/src/expectation/diff.ts +7 -3
- package/src/expectation/expect.ts +22 -362
- package/src/expectation/expectations.ts +971 -0
- package/src/expectation/include.ts +59 -75
- package/src/expectation/matchers.ts +698 -0
- package/src/expectation/print.ts +8 -4
- package/src/expectation/types.ts +22 -94
- package/src/globals.ts +2 -2
- package/src/index.ts +17 -10
- package/src/test.ts +34 -10
- package/dist/expectation/basic.cjs +0 -188
- package/dist/expectation/basic.cjs.map +0 -6
- package/dist/expectation/basic.d.ts +0 -90
- package/dist/expectation/basic.mjs +0 -156
- package/dist/expectation/basic.mjs.map +0 -6
- package/dist/expectation/throwing.cjs +0 -58
- package/dist/expectation/throwing.cjs.map +0 -6
- package/dist/expectation/throwing.d.ts +0 -36
- package/dist/expectation/throwing.mjs +0 -32
- package/dist/expectation/throwing.mjs.map +0 -6
- package/dist/expectation/trivial.cjs +0 -96
- package/dist/expectation/trivial.cjs.map +0 -6
- package/dist/expectation/trivial.d.ts +0 -13
- package/dist/expectation/trivial.mjs +0 -61
- package/dist/expectation/trivial.mjs.map +0 -6
- package/src/expectation/basic.ts +0 -413
- package/src/expectation/throwing.ts +0 -106
- package/src/expectation/trivial.ts +0 -107
|
@@ -1,103 +1,77 @@
|
|
|
1
|
-
import { diff } from './diff'
|
|
1
|
+
import { diff, type Diff } from './diff'
|
|
2
|
+
import { type Expectations } from './expectations'
|
|
2
3
|
import {
|
|
3
4
|
ExpectationError,
|
|
4
5
|
stringifyObjectType,
|
|
5
6
|
stringifyValue,
|
|
6
7
|
} from './types'
|
|
7
8
|
|
|
8
|
-
import type { Diff } from './diff'
|
|
9
|
-
import type { Expectations } from './expect'
|
|
10
|
-
import type { ExpectationsContext } from './types'
|
|
11
|
-
|
|
12
9
|
/* === TO INCLUDE =========================================================== */
|
|
13
10
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/** Expect the value to include _all_ values from the specified {@link Set}. */
|
|
19
|
-
function toInclude<T>(this: T, entries: Set<any>): T
|
|
20
|
-
/** Expect the value to include _all_ values in any order from the specified _array_. */
|
|
21
|
-
function toInclude<T>(this: T, values: any[]): T
|
|
22
|
-
|
|
23
|
-
/* Overloaded function implementation */
|
|
24
|
-
function toInclude(
|
|
25
|
-
this: ExpectationsContext,
|
|
26
|
-
expected:
|
|
11
|
+
export function toInclude(
|
|
12
|
+
expectations: Expectations,
|
|
13
|
+
negative: boolean,
|
|
14
|
+
contents:
|
|
27
15
|
| Record<string, any>
|
|
28
16
|
| Map<any, any>
|
|
29
17
|
| Set<any>
|
|
30
18
|
| any [],
|
|
31
|
-
):
|
|
19
|
+
): void {
|
|
32
20
|
// get diff depending on type of "expected"
|
|
33
|
-
if (
|
|
34
|
-
if (
|
|
35
|
-
if (Array.isArray(
|
|
36
|
-
if (
|
|
37
|
-
throw new TypeError(`Invalid type for "toInclude(...)": ${stringifyValue(
|
|
21
|
+
if (contents instanceof Map) return includesMappings(expectations, negative, contents)
|
|
22
|
+
if (contents instanceof Set) return includesValues(expectations, negative, contents)
|
|
23
|
+
if (Array.isArray(contents)) return includesValues(expectations, negative, new Set(contents))
|
|
24
|
+
if (contents instanceof Object) return includesProps(expectations, negative, contents)
|
|
25
|
+
throw new TypeError(`Invalid type for "toInclude(...)": ${stringifyValue(contents)}`)
|
|
38
26
|
}
|
|
39
27
|
|
|
40
28
|
/* === TO MATCH CONTENTS ==================================================== */
|
|
41
29
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
* order) from the specified _array_.
|
|
45
|
-
*/
|
|
46
|
-
function toMatchContents<T>(this: T, contents: any[]): T
|
|
47
|
-
/**
|
|
48
|
-
* Expect the value to include _all_ values (and only those values, in any
|
|
49
|
-
* order) from the specified {@link Set}.
|
|
50
|
-
*/
|
|
51
|
-
function toMatchContents<T>(this: T, contents: Set<any>): T
|
|
52
|
-
|
|
53
|
-
/* Overloaded function implementation */
|
|
54
|
-
function toMatchContents(
|
|
55
|
-
this: ExpectationsContext,
|
|
30
|
+
export function toMatchContents(
|
|
31
|
+
expectations: Expectations,
|
|
56
32
|
contents: any[] | Set<any>,
|
|
57
|
-
):
|
|
33
|
+
): void {
|
|
58
34
|
let actual: Set<any>
|
|
59
35
|
let expected: Set<any>
|
|
60
36
|
try {
|
|
61
|
-
actual = new Set(
|
|
37
|
+
actual = new Set(expectations.value as any)
|
|
62
38
|
expected = new Set(contents)
|
|
63
39
|
} catch (error) {
|
|
64
|
-
throw new ExpectationError(
|
|
40
|
+
throw new ExpectationError(expectations, 'to be an iterable object')
|
|
65
41
|
}
|
|
66
42
|
|
|
67
43
|
const result = diff(actual, expected)
|
|
68
44
|
delete result.error // remove extra error message about size differences...
|
|
69
|
-
if (result.diff
|
|
70
|
-
throw new ExpectationError(this,
|
|
71
|
-
`to match contents of ${stringifyObjectType(contents)}`,
|
|
72
|
-
{ ...result, value: this.value })
|
|
73
|
-
}
|
|
45
|
+
if (! result.diff) return
|
|
74
46
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
toInclude,
|
|
79
|
-
toMatchContents,
|
|
47
|
+
throw new ExpectationError(expectations,
|
|
48
|
+
`to match contents of ${stringifyObjectType(contents)}`,
|
|
49
|
+
{ ...result, value: expectations.value })
|
|
80
50
|
}
|
|
81
51
|
|
|
82
52
|
/* ========================================================================== *
|
|
83
53
|
* INTERNALS *
|
|
84
54
|
* ========================================================================== */
|
|
85
55
|
|
|
86
|
-
function includesProps(
|
|
56
|
+
function includesProps(
|
|
57
|
+
expectations: Expectations,
|
|
58
|
+
negative: boolean,
|
|
59
|
+
expected: Record<string, any>,
|
|
60
|
+
): void {
|
|
87
61
|
// simple include for maps with objects...
|
|
88
|
-
if (
|
|
89
|
-
return includesMappings(
|
|
62
|
+
if (expectations.value instanceof Map) {
|
|
63
|
+
return includesMappings(expectations, negative, new Map(Object.entries(expected)))
|
|
90
64
|
}
|
|
91
65
|
|
|
92
66
|
// we really need an object as actual
|
|
93
|
-
|
|
94
|
-
const actual: Record<string, any> =
|
|
67
|
+
expectations.toBeInstanceOf(Object)
|
|
68
|
+
const actual: Record<string, any> = expectations.value as any
|
|
95
69
|
|
|
96
70
|
// get expected key set and process...
|
|
97
71
|
const keys = new Set(Object.keys(expected))
|
|
98
72
|
const props: Record<string, Diff> = {}
|
|
99
73
|
|
|
100
|
-
if (
|
|
74
|
+
if (negative) {
|
|
101
75
|
// only consider keys... if they exist, fail!
|
|
102
76
|
for (const key of keys) {
|
|
103
77
|
if ((actual[key] !== undefined) || (key in actual)) {
|
|
@@ -122,27 +96,32 @@ function includesProps(context: ExpectationsContext, expected: Record<string, an
|
|
|
122
96
|
}
|
|
123
97
|
|
|
124
98
|
const count = Object.keys(props).length
|
|
125
|
-
if (count === 0) return
|
|
99
|
+
if (count === 0) return // no props? no errors!
|
|
126
100
|
|
|
127
101
|
const type = count === 1 ? 'property' : 'properties'
|
|
128
|
-
|
|
102
|
+
const not = negative ? 'not ' : ''
|
|
103
|
+
throw new ExpectationError(expectations, `${not}to include ${count} ${type}`, {
|
|
129
104
|
diff: true,
|
|
130
105
|
value: actual,
|
|
131
106
|
props,
|
|
132
107
|
})
|
|
133
108
|
}
|
|
134
109
|
|
|
135
|
-
function includesValues(
|
|
110
|
+
function includesValues(
|
|
111
|
+
expectations: Expectations,
|
|
112
|
+
negative: boolean,
|
|
113
|
+
expected: Set<any>,
|
|
114
|
+
): void {
|
|
136
115
|
// we really need an _iterable_ object as actual
|
|
137
|
-
|
|
138
|
-
if (typeof (
|
|
139
|
-
throw new ExpectationError(
|
|
116
|
+
expectations.toBeInstanceOf(Object)
|
|
117
|
+
if (typeof (expectations.value as any)[Symbol.iterator] !== 'function') {
|
|
118
|
+
throw new ExpectationError(expectations, 'to be an iterable object')
|
|
140
119
|
}
|
|
141
|
-
const actual = new Set(
|
|
120
|
+
const actual = new Set(expectations.value as Iterable<any>)
|
|
142
121
|
|
|
143
122
|
// iterate through all the values and see what we can find
|
|
144
123
|
const values: Diff[] = []
|
|
145
|
-
if (
|
|
124
|
+
if (negative) {
|
|
146
125
|
for (const exp of expected) {
|
|
147
126
|
for (const act of actual) {
|
|
148
127
|
const result = diff(act, exp)
|
|
@@ -170,25 +149,29 @@ function includesValues(context: ExpectationsContext, expected: Set<any>): Expec
|
|
|
170
149
|
}
|
|
171
150
|
|
|
172
151
|
const count = values.length
|
|
173
|
-
if (count === 0) return
|
|
152
|
+
if (count === 0) return // no values? no errors!
|
|
174
153
|
|
|
175
154
|
const type = count === 1 ? 'value' : 'values'
|
|
176
|
-
|
|
155
|
+
const not = negative ? 'not ' : ''
|
|
156
|
+
throw new ExpectationError(expectations, `${not}to include ${count} ${type}`, {
|
|
177
157
|
diff: true,
|
|
178
|
-
value:
|
|
158
|
+
value: expectations.value,
|
|
179
159
|
values,
|
|
180
160
|
})
|
|
181
161
|
}
|
|
182
162
|
|
|
183
|
-
function includesMappings(
|
|
184
|
-
|
|
185
|
-
|
|
163
|
+
function includesMappings(
|
|
164
|
+
expectations: Expectations,
|
|
165
|
+
negative: boolean,
|
|
166
|
+
expected: Map<any, any>,
|
|
167
|
+
): void {
|
|
168
|
+
const actual = expectations.toBeInstanceOf(Map).value
|
|
186
169
|
|
|
187
170
|
// Get expected key set and process...
|
|
188
171
|
const keys = new Set(expected.keys())
|
|
189
172
|
const mappings: [ string, Diff ][] = []
|
|
190
173
|
|
|
191
|
-
if (
|
|
174
|
+
if (negative) {
|
|
192
175
|
// only consider keys... if they exist, fail!
|
|
193
176
|
for (const key of keys) {
|
|
194
177
|
if (actual.has(key)) {
|
|
@@ -207,12 +190,13 @@ function includesMappings(context: ExpectationsContext, expected: Map<any, any>)
|
|
|
207
190
|
}
|
|
208
191
|
|
|
209
192
|
const count = mappings.length
|
|
210
|
-
if (count === 0) return
|
|
193
|
+
if (count === 0) return // no mappings? no errors!
|
|
211
194
|
|
|
212
195
|
const type = count === 1 ? 'mapping' : 'mappings'
|
|
213
|
-
|
|
196
|
+
const not = negative ? 'not ' : ''
|
|
197
|
+
throw new ExpectationError(expectations, `${not}to include ${count} ${type}`, {
|
|
214
198
|
diff: true,
|
|
215
|
-
value:
|
|
199
|
+
value: expectations.value,
|
|
216
200
|
mappings,
|
|
217
201
|
})
|
|
218
202
|
}
|