@sprucelabs/test-utils 5.1.382 → 5.1.383
Sign up to get free protection for your applications and to get access to all the features.
package/build/assert/assert.d.ts
CHANGED
@@ -7,7 +7,7 @@ type IsAny<T> = IfAny<T, true, never>;
|
|
7
7
|
type TypeEqual<T, U> = IsAny<T> extends never ? Exclude<T, U> extends never ? Exclude<U, T> extends never ? true : false : false : false;
|
8
8
|
declare function isExactType<T, E, Pass = TypeEqual<T, E>>(_: Pass): void;
|
9
9
|
export interface ISpruceAssert {
|
10
|
-
isInstanceOf<T>(test: T, Test: new (...props: any[]) => T): void;
|
10
|
+
isInstanceOf<T>(test: T, Test: new (...props: any[]) => T, message?: string): void;
|
11
11
|
isNumber(actual: any, message?: string): asserts actual is number;
|
12
12
|
isType: typeof expectType;
|
13
13
|
isExactType: typeof isExactType;
|
package/build/assert/assert.js
CHANGED
@@ -34,7 +34,7 @@ const assert = {
|
|
34
34
|
isEqualDeep(actual, expected, message, shouldAppendDelta = true) {
|
35
35
|
if (!(0, deep_equal_1.default)(actual, expected, { strict: true })) {
|
36
36
|
let result = (0, variable_diff_1.default)(actual, expected);
|
37
|
-
this.fail(`${
|
37
|
+
this.fail(buildErrorMessage(`${`Deep equal failed.\n\nActual would need the following changes to match expected:`}${shouldAppendDelta ? `\n\n${result.text}` : ``}`, message));
|
38
38
|
}
|
39
39
|
},
|
40
40
|
isNotEqualDeep(actual, expected, message) {
|
@@ -42,23 +42,23 @@ const assert = {
|
|
42
42
|
},
|
43
43
|
isAbove(actual, floor, message) {
|
44
44
|
if (typeof actual !== 'number') {
|
45
|
-
this.fail(
|
45
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not a number!`, message));
|
46
46
|
}
|
47
47
|
if (actual <= floor) {
|
48
|
-
this.fail(
|
48
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not above ${stringify(floor)}`, message));
|
49
49
|
}
|
50
50
|
},
|
51
51
|
isBelow(actual, ceiling, message) {
|
52
52
|
if (typeof actual !== 'number') {
|
53
|
-
this.fail(
|
53
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not a number!`, message));
|
54
54
|
}
|
55
55
|
if (actual >= ceiling) {
|
56
|
-
this.fail(
|
56
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not below ${stringify(ceiling)}`, message));
|
57
57
|
}
|
58
58
|
},
|
59
59
|
isUndefined(actual, message) {
|
60
60
|
if (typeof actual !== 'undefined') {
|
61
|
-
this.fail(
|
61
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not undefined`, message));
|
62
62
|
}
|
63
63
|
},
|
64
64
|
isTruthy(actual, message) {
|
@@ -66,17 +66,17 @@ const assert = {
|
|
66
66
|
actual === null ||
|
67
67
|
typeof actual === 'undefined' ||
|
68
68
|
actual === 0) {
|
69
|
-
this.fail(
|
69
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not truthy`, message));
|
70
70
|
}
|
71
71
|
},
|
72
72
|
isFalsy(actual, message) {
|
73
73
|
if (actual) {
|
74
|
-
this.fail(
|
74
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not falsy`, message));
|
75
75
|
}
|
76
76
|
},
|
77
77
|
isNull(actual, message) {
|
78
78
|
if (actual !== null) {
|
79
|
-
this.fail(
|
79
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not null`, message));
|
80
80
|
}
|
81
81
|
},
|
82
82
|
isString(actual, message) {
|
@@ -95,20 +95,20 @@ const assert = {
|
|
95
95
|
},
|
96
96
|
isObject(actual, message) {
|
97
97
|
if (!(0, isObjectLike_1.default)(actual)) {
|
98
|
-
throw this.fail(
|
98
|
+
throw this.fail(buildErrorMessage(`${stringify(actual)} is not an object`, message));
|
99
99
|
}
|
100
100
|
},
|
101
101
|
isArray(actual, message) {
|
102
102
|
if (!Array.isArray(actual)) {
|
103
|
-
throw this.fail(
|
103
|
+
throw this.fail(buildErrorMessage(`${stringify(actual)} is not an array`, message));
|
104
104
|
}
|
105
105
|
},
|
106
106
|
isLength(actual, expected, message) {
|
107
107
|
if (!actual) {
|
108
|
-
throw this.fail(
|
108
|
+
throw this.fail(buildErrorMessage(`Expected array of length ${expected}, but got ${stringify(actual)}`, message));
|
109
109
|
}
|
110
110
|
//@ts-ignore
|
111
|
-
this.isEqual(actual.length, expected,
|
111
|
+
this.isEqual(actual.length, expected, buildErrorMessage(`Your array is not the expected length!`, message));
|
112
112
|
},
|
113
113
|
doesNotInclude(haystack, needle, message) {
|
114
114
|
let doesInclude = false;
|
@@ -120,14 +120,11 @@ const assert = {
|
|
120
120
|
doesInclude = false;
|
121
121
|
}
|
122
122
|
if (doesInclude) {
|
123
|
-
this.fail(
|
123
|
+
this.fail(buildErrorMessage(`${stringify(haystack)} should not include ${stringify(needle)}, but it does`, message));
|
124
124
|
}
|
125
125
|
},
|
126
126
|
doesInclude(haystack, needle, message) {
|
127
127
|
let msg = `Could not find ${stringify(needle)} in ${stringify(haystack)}`;
|
128
|
-
if (message) {
|
129
|
-
msg = message + `\n\n` + msg;
|
130
|
-
}
|
131
128
|
const isNeedleString = typeof needle === 'string';
|
132
129
|
const isNeedleRegex = needle instanceof RegExp;
|
133
130
|
if (typeof haystack === 'string' &&
|
@@ -198,12 +195,12 @@ const assert = {
|
|
198
195
|
}
|
199
196
|
msg = `Could not find match ${stringify(expected)} at ${stringify(pathAfterFirstArray)} in ${stringify(actualBeforeArray)}.`;
|
200
197
|
}
|
201
|
-
this.fail(msg);
|
198
|
+
this.fail(buildErrorMessage(msg, message));
|
202
199
|
},
|
203
|
-
hasAllFunctions(obj, functionNames) {
|
200
|
+
hasAllFunctions(obj, functionNames, message) {
|
204
201
|
functionNames.forEach((name) => {
|
205
202
|
if (typeof obj[name] !== 'function') {
|
206
|
-
this.fail(`A function named "${name}" does not exist on ${stringify(obj)}
|
203
|
+
this.fail(buildErrorMessage(`A function named "${name}" does not exist on ${stringify(obj)}`, message));
|
207
204
|
}
|
208
205
|
});
|
209
206
|
},
|
@@ -215,7 +212,7 @@ const assert = {
|
|
215
212
|
assert_utility_1.default.checkDoesThrowError(matcher, err, msg);
|
216
213
|
return err;
|
217
214
|
}
|
218
|
-
this.fail(
|
215
|
+
this.fail(buildErrorMessage('Expected a thrown error, but never got one!', msg));
|
219
216
|
},
|
220
217
|
async doesThrowAsync(cb, matcher, msg) {
|
221
218
|
try {
|
@@ -225,11 +222,11 @@ const assert = {
|
|
225
222
|
assert_utility_1.default.checkDoesThrowError(matcher, err, msg);
|
226
223
|
return err;
|
227
224
|
}
|
228
|
-
this.fail(
|
225
|
+
this.fail(buildErrorMessage('Expected a thrown error, but never got one!', msg));
|
229
226
|
},
|
230
227
|
fail: assert_utility_1.default.fail,
|
231
|
-
isInstanceOf(actual, Class) {
|
232
|
-
assert.isTrue(actual instanceof Class, `${assert_utility_1.default.stringify(actual)} is not an instance of:\n\n${Class}
|
228
|
+
isInstanceOf(actual, Class, message) {
|
229
|
+
assert.isTrue(actual instanceof Class, buildErrorMessage(`${assert_utility_1.default.stringify(actual)} is not an instance of:\n\n${Class}`, message));
|
233
230
|
},
|
234
231
|
isBetween(actual, floor, ceiling, message) {
|
235
232
|
assert.isBelow(actual, ceiling, message);
|
@@ -241,7 +238,7 @@ const assert = {
|
|
241
238
|
if (actual >= floor && actual <= ceiling) {
|
242
239
|
return;
|
243
240
|
}
|
244
|
-
assert.fail(
|
241
|
+
assert.fail(buildErrorMessage(`${actual} is not between ${floor} and ${ceiling} (inclusive)`, message));
|
245
242
|
},
|
246
243
|
};
|
247
244
|
exports.default = assert;
|
@@ -117,8 +117,8 @@ const assertUtil = {
|
|
117
117
|
}
|
118
118
|
},
|
119
119
|
checkDoesThrowError(matcher, err, msg) {
|
120
|
-
var _a
|
121
|
-
const message = (
|
120
|
+
var _a;
|
121
|
+
const message = (_a = err.message) !== null && _a !== void 0 ? _a : '';
|
122
122
|
if (typeof matcher === 'string' &&
|
123
123
|
message.search(matcher) === -1 &&
|
124
124
|
!message.includes(matcher)) {
|
@@ -7,7 +7,7 @@ type IsAny<T> = IfAny<T, true, never>;
|
|
7
7
|
type TypeEqual<T, U> = IsAny<T> extends never ? Exclude<T, U> extends never ? Exclude<U, T> extends never ? true : false : false : false;
|
8
8
|
declare function isExactType<T, E, Pass = TypeEqual<T, E>>(_: Pass): void;
|
9
9
|
export interface ISpruceAssert {
|
10
|
-
isInstanceOf<T>(test: T, Test: new (...props: any[]) => T): void;
|
10
|
+
isInstanceOf<T>(test: T, Test: new (...props: any[]) => T, message?: string): void;
|
11
11
|
isNumber(actual: any, message?: string): asserts actual is number;
|
12
12
|
isType: typeof expectType;
|
13
13
|
isExactType: typeof isExactType;
|
@@ -29,7 +29,7 @@ const assert = {
|
|
29
29
|
isEqualDeep(actual, expected, message, shouldAppendDelta = true) {
|
30
30
|
if (!deepEqual(actual, expected, { strict: true })) {
|
31
31
|
let result = diff(actual, expected);
|
32
|
-
this.fail(`${
|
32
|
+
this.fail(buildErrorMessage(`${`Deep equal failed.\n\nActual would need the following changes to match expected:`}${shouldAppendDelta ? `\n\n${result.text}` : ``}`, message));
|
33
33
|
}
|
34
34
|
},
|
35
35
|
isNotEqualDeep(actual, expected, message) {
|
@@ -37,23 +37,23 @@ const assert = {
|
|
37
37
|
},
|
38
38
|
isAbove(actual, floor, message) {
|
39
39
|
if (typeof actual !== 'number') {
|
40
|
-
this.fail(
|
40
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not a number!`, message));
|
41
41
|
}
|
42
42
|
if (actual <= floor) {
|
43
|
-
this.fail(
|
43
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not above ${stringify(floor)}`, message));
|
44
44
|
}
|
45
45
|
},
|
46
46
|
isBelow(actual, ceiling, message) {
|
47
47
|
if (typeof actual !== 'number') {
|
48
|
-
this.fail(
|
48
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not a number!`, message));
|
49
49
|
}
|
50
50
|
if (actual >= ceiling) {
|
51
|
-
this.fail(
|
51
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not below ${stringify(ceiling)}`, message));
|
52
52
|
}
|
53
53
|
},
|
54
54
|
isUndefined(actual, message) {
|
55
55
|
if (typeof actual !== 'undefined') {
|
56
|
-
this.fail(
|
56
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not undefined`, message));
|
57
57
|
}
|
58
58
|
},
|
59
59
|
isTruthy(actual, message) {
|
@@ -61,17 +61,17 @@ const assert = {
|
|
61
61
|
actual === null ||
|
62
62
|
typeof actual === 'undefined' ||
|
63
63
|
actual === 0) {
|
64
|
-
this.fail(
|
64
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not truthy`, message));
|
65
65
|
}
|
66
66
|
},
|
67
67
|
isFalsy(actual, message) {
|
68
68
|
if (actual) {
|
69
|
-
this.fail(
|
69
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not falsy`, message));
|
70
70
|
}
|
71
71
|
},
|
72
72
|
isNull(actual, message) {
|
73
73
|
if (actual !== null) {
|
74
|
-
this.fail(
|
74
|
+
this.fail(buildErrorMessage(`${stringify(actual)} is not null`, message));
|
75
75
|
}
|
76
76
|
},
|
77
77
|
isString(actual, message) {
|
@@ -90,20 +90,20 @@ const assert = {
|
|
90
90
|
},
|
91
91
|
isObject(actual, message) {
|
92
92
|
if (!isObjectLike(actual)) {
|
93
|
-
throw this.fail(
|
93
|
+
throw this.fail(buildErrorMessage(`${stringify(actual)} is not an object`, message));
|
94
94
|
}
|
95
95
|
},
|
96
96
|
isArray(actual, message) {
|
97
97
|
if (!Array.isArray(actual)) {
|
98
|
-
throw this.fail(
|
98
|
+
throw this.fail(buildErrorMessage(`${stringify(actual)} is not an array`, message));
|
99
99
|
}
|
100
100
|
},
|
101
101
|
isLength(actual, expected, message) {
|
102
102
|
if (!actual) {
|
103
|
-
throw this.fail(
|
103
|
+
throw this.fail(buildErrorMessage(`Expected array of length ${expected}, but got ${stringify(actual)}`, message));
|
104
104
|
}
|
105
105
|
//@ts-ignore
|
106
|
-
this.isEqual(actual.length, expected,
|
106
|
+
this.isEqual(actual.length, expected, buildErrorMessage(`Your array is not the expected length!`, message));
|
107
107
|
},
|
108
108
|
doesNotInclude(haystack, needle, message) {
|
109
109
|
let doesInclude = false;
|
@@ -115,14 +115,11 @@ const assert = {
|
|
115
115
|
doesInclude = false;
|
116
116
|
}
|
117
117
|
if (doesInclude) {
|
118
|
-
this.fail(
|
118
|
+
this.fail(buildErrorMessage(`${stringify(haystack)} should not include ${stringify(needle)}, but it does`, message));
|
119
119
|
}
|
120
120
|
},
|
121
121
|
doesInclude(haystack, needle, message) {
|
122
122
|
let msg = `Could not find ${stringify(needle)} in ${stringify(haystack)}`;
|
123
|
-
if (message) {
|
124
|
-
msg = message + `\n\n` + msg;
|
125
|
-
}
|
126
123
|
const isNeedleString = typeof needle === 'string';
|
127
124
|
const isNeedleRegex = needle instanceof RegExp;
|
128
125
|
if (typeof haystack === 'string' &&
|
@@ -193,12 +190,12 @@ const assert = {
|
|
193
190
|
}
|
194
191
|
msg = `Could not find match ${stringify(expected)} at ${stringify(pathAfterFirstArray)} in ${stringify(actualBeforeArray)}.`;
|
195
192
|
}
|
196
|
-
this.fail(msg);
|
193
|
+
this.fail(buildErrorMessage(msg, message));
|
197
194
|
},
|
198
|
-
hasAllFunctions(obj, functionNames) {
|
195
|
+
hasAllFunctions(obj, functionNames, message) {
|
199
196
|
functionNames.forEach((name) => {
|
200
197
|
if (typeof obj[name] !== 'function') {
|
201
|
-
this.fail(`A function named "${name}" does not exist on ${stringify(obj)}
|
198
|
+
this.fail(buildErrorMessage(`A function named "${name}" does not exist on ${stringify(obj)}`, message));
|
202
199
|
}
|
203
200
|
});
|
204
201
|
},
|
@@ -210,7 +207,7 @@ const assert = {
|
|
210
207
|
assertUtil.checkDoesThrowError(matcher, err, msg);
|
211
208
|
return err;
|
212
209
|
}
|
213
|
-
this.fail(
|
210
|
+
this.fail(buildErrorMessage('Expected a thrown error, but never got one!', msg));
|
214
211
|
},
|
215
212
|
async doesThrowAsync(cb, matcher, msg) {
|
216
213
|
try {
|
@@ -220,11 +217,11 @@ const assert = {
|
|
220
217
|
assertUtil.checkDoesThrowError(matcher, err, msg);
|
221
218
|
return err;
|
222
219
|
}
|
223
|
-
this.fail(
|
220
|
+
this.fail(buildErrorMessage('Expected a thrown error, but never got one!', msg));
|
224
221
|
},
|
225
222
|
fail: assertUtil.fail,
|
226
|
-
isInstanceOf(actual, Class) {
|
227
|
-
assert.isTrue(actual instanceof Class, `${assertUtil.stringify(actual)} is not an instance of:\n\n${Class}
|
223
|
+
isInstanceOf(actual, Class, message) {
|
224
|
+
assert.isTrue(actual instanceof Class, buildErrorMessage(`${assertUtil.stringify(actual)} is not an instance of:\n\n${Class}`, message));
|
228
225
|
},
|
229
226
|
isBetween(actual, floor, ceiling, message) {
|
230
227
|
assert.isBelow(actual, ceiling, message);
|
@@ -236,7 +233,7 @@ const assert = {
|
|
236
233
|
if (actual >= floor && actual <= ceiling) {
|
237
234
|
return;
|
238
235
|
}
|
239
|
-
assert.fail(
|
236
|
+
assert.fail(buildErrorMessage(`${actual} is not between ${floor} and ${ceiling} (inclusive)`, message));
|
240
237
|
},
|
241
238
|
};
|
242
239
|
export default assert;
|
@@ -111,8 +111,8 @@ const assertUtil = {
|
|
111
111
|
}
|
112
112
|
},
|
113
113
|
checkDoesThrowError(matcher, err, msg) {
|
114
|
-
var _a
|
115
|
-
const message = (
|
114
|
+
var _a;
|
115
|
+
const message = (_a = err.message) !== null && _a !== void 0 ? _a : '';
|
116
116
|
if (typeof matcher === 'string' &&
|
117
117
|
message.search(matcher) === -1 &&
|
118
118
|
!message.includes(matcher)) {
|