@vitest/utils 3.1.0-beta.1 → 3.1.0-beta.2

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/error.js CHANGED
@@ -8,130 +8,123 @@ import 'loupe';
8
8
  const IS_RECORD_SYMBOL = "@@__IMMUTABLE_RECORD__@@";
9
9
  const IS_COLLECTION_SYMBOL = "@@__IMMUTABLE_ITERABLE__@@";
10
10
  function isImmutable(v) {
11
- return v && (v[IS_COLLECTION_SYMBOL] || v[IS_RECORD_SYMBOL]);
11
+ return v && (v[IS_COLLECTION_SYMBOL] || v[IS_RECORD_SYMBOL]);
12
12
  }
13
13
  const OBJECT_PROTO = Object.getPrototypeOf({});
14
14
  function getUnserializableMessage(err) {
15
- if (err instanceof Error) {
16
- return `<unserializable>: ${err.message}`;
17
- }
18
- if (typeof err === "string") {
19
- return `<unserializable>: ${err}`;
20
- }
21
- return "<unserializable>";
15
+ if (err instanceof Error) {
16
+ return `<unserializable>: ${err.message}`;
17
+ }
18
+ if (typeof err === "string") {
19
+ return `<unserializable>: ${err}`;
20
+ }
21
+ return "<unserializable>";
22
22
  }
23
- function serializeValue(val, seen = /* @__PURE__ */ new WeakMap()) {
24
- if (!val || typeof val === "string") {
25
- return val;
26
- }
27
- if (typeof val === "function") {
28
- return `Function<${val.name || "anonymous"}>`;
29
- }
30
- if (typeof val === "symbol") {
31
- return val.toString();
32
- }
33
- if (typeof val !== "object") {
34
- return val;
35
- }
36
- if (isImmutable(val)) {
37
- return serializeValue(val.toJSON(), seen);
38
- }
39
- if (val instanceof Promise || val.constructor && val.constructor.prototype === "AsyncFunction") {
40
- return "Promise";
41
- }
42
- if (typeof Element !== "undefined" && val instanceof Element) {
43
- return val.tagName;
44
- }
45
- if (typeof val.asymmetricMatch === "function") {
46
- return `${val.toString()} ${format(val.sample)}`;
47
- }
48
- if (typeof val.toJSON === "function") {
49
- return serializeValue(val.toJSON(), seen);
50
- }
51
- if (seen.has(val)) {
52
- return seen.get(val);
53
- }
54
- if (Array.isArray(val)) {
55
- const clone = new Array(val.length);
56
- seen.set(val, clone);
57
- val.forEach((e, i) => {
58
- try {
59
- clone[i] = serializeValue(e, seen);
60
- } catch (err) {
61
- clone[i] = getUnserializableMessage(err);
62
- }
63
- });
64
- return clone;
65
- } else {
66
- const clone = /* @__PURE__ */ Object.create(null);
67
- seen.set(val, clone);
68
- let obj = val;
69
- while (obj && obj !== OBJECT_PROTO) {
70
- Object.getOwnPropertyNames(obj).forEach((key) => {
71
- if (key in clone) {
72
- return;
73
- }
74
- try {
75
- clone[key] = serializeValue(val[key], seen);
76
- } catch (err) {
77
- delete clone[key];
78
- clone[key] = getUnserializableMessage(err);
79
- }
80
- });
81
- obj = Object.getPrototypeOf(obj);
82
- }
83
- return clone;
84
- }
23
+ function serializeValue(val, seen = new WeakMap()) {
24
+ if (!val || typeof val === "string") {
25
+ return val;
26
+ }
27
+ if (typeof val === "function") {
28
+ return `Function<${val.name || "anonymous"}>`;
29
+ }
30
+ if (typeof val === "symbol") {
31
+ return val.toString();
32
+ }
33
+ if (typeof val !== "object") {
34
+ return val;
35
+ }
36
+ if (isImmutable(val)) {
37
+ return serializeValue(val.toJSON(), seen);
38
+ }
39
+ if (val instanceof Promise || val.constructor && val.constructor.prototype === "AsyncFunction") {
40
+ return "Promise";
41
+ }
42
+ if (typeof Element !== "undefined" && val instanceof Element) {
43
+ return val.tagName;
44
+ }
45
+ if (typeof val.asymmetricMatch === "function") {
46
+ return `${val.toString()} ${format(val.sample)}`;
47
+ }
48
+ if (typeof val.toJSON === "function") {
49
+ return serializeValue(val.toJSON(), seen);
50
+ }
51
+ if (seen.has(val)) {
52
+ return seen.get(val);
53
+ }
54
+ if (Array.isArray(val)) {
55
+ const clone = new Array(val.length);
56
+ seen.set(val, clone);
57
+ val.forEach((e, i) => {
58
+ try {
59
+ clone[i] = serializeValue(e, seen);
60
+ } catch (err) {
61
+ clone[i] = getUnserializableMessage(err);
62
+ }
63
+ });
64
+ return clone;
65
+ } else {
66
+ const clone = Object.create(null);
67
+ seen.set(val, clone);
68
+ let obj = val;
69
+ while (obj && obj !== OBJECT_PROTO) {
70
+ Object.getOwnPropertyNames(obj).forEach((key) => {
71
+ if (key in clone) {
72
+ return;
73
+ }
74
+ try {
75
+ clone[key] = serializeValue(val[key], seen);
76
+ } catch (err) {
77
+ delete clone[key];
78
+ clone[key] = getUnserializableMessage(err);
79
+ }
80
+ });
81
+ obj = Object.getPrototypeOf(obj);
82
+ }
83
+ return clone;
84
+ }
85
85
  }
86
86
  function normalizeErrorMessage(message) {
87
- return message.replace(/__(vite_ssr_import|vi_import)_\d+__\./g, "");
87
+ return message.replace(/__(vite_ssr_import|vi_import)_\d+__\./g, "");
88
88
  }
89
- function processError(_err, diffOptions, seen = /* @__PURE__ */ new WeakSet()) {
90
- if (!_err || typeof _err !== "object") {
91
- return { message: String(_err) };
92
- }
93
- const err = _err;
94
- if (err.stack) {
95
- err.stackStr = String(err.stack);
96
- }
97
- if (err.name) {
98
- err.nameStr = String(err.name);
99
- }
100
- if (err.showDiff || err.showDiff === void 0 && err.expected !== void 0 && err.actual !== void 0) {
101
- err.diff = printDiffOrStringify(err.actual, err.expected, {
102
- ...diffOptions,
103
- ...err.diffOptions
104
- });
105
- }
106
- if (typeof err.expected !== "string") {
107
- err.expected = stringify(err.expected, 10);
108
- }
109
- if (typeof err.actual !== "string") {
110
- err.actual = stringify(err.actual, 10);
111
- }
112
- try {
113
- if (typeof err.message === "string") {
114
- err.message = normalizeErrorMessage(err.message);
115
- }
116
- } catch {
117
- }
118
- try {
119
- if (!seen.has(err) && typeof err.cause === "object") {
120
- seen.add(err);
121
- err.cause = processError(err.cause, diffOptions, seen);
122
- }
123
- } catch {
124
- }
125
- try {
126
- return serializeValue(err);
127
- } catch (e) {
128
- return serializeValue(
129
- new Error(
130
- `Failed to fully serialize error: ${e == null ? void 0 : e.message}
131
- Inner error message: ${err == null ? void 0 : err.message}`
132
- )
133
- );
134
- }
89
+ function processError(_err, diffOptions, seen = new WeakSet()) {
90
+ if (!_err || typeof _err !== "object") {
91
+ return { message: String(_err) };
92
+ }
93
+ const err = _err;
94
+ if (err.stack) {
95
+ err.stackStr = String(err.stack);
96
+ }
97
+ if (err.name) {
98
+ err.nameStr = String(err.name);
99
+ }
100
+ if (err.showDiff || err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined) {
101
+ err.diff = printDiffOrStringify(err.actual, err.expected, {
102
+ ...diffOptions,
103
+ ...err.diffOptions
104
+ });
105
+ }
106
+ if (typeof err.expected !== "string") {
107
+ err.expected = stringify(err.expected, 10);
108
+ }
109
+ if (typeof err.actual !== "string") {
110
+ err.actual = stringify(err.actual, 10);
111
+ }
112
+ try {
113
+ if (typeof err.message === "string") {
114
+ err.message = normalizeErrorMessage(err.message);
115
+ }
116
+ } catch {}
117
+ try {
118
+ if (!seen.has(err) && typeof err.cause === "object") {
119
+ seen.add(err);
120
+ err.cause = processError(err.cause, diffOptions, seen);
121
+ }
122
+ } catch {}
123
+ try {
124
+ return serializeValue(err);
125
+ } catch (e) {
126
+ return serializeValue(new Error(`Failed to fully serialize error: ${e === null || e === void 0 ? void 0 : e.message}\nInner error message: ${err === null || err === void 0 ? void 0 : err.message}`));
127
+ }
135
128
  }
136
129
 
137
130
  export { processError, serializeValue as serializeError, serializeValue };
package/dist/helpers.js CHANGED
@@ -1,224 +1,242 @@
1
+ /**
2
+ * Get original stacktrace without source map support the most performant way.
3
+ * - Create only 1 stack frame.
4
+ * - Rewrite prepareStackTrace to bypass "support-stack-trace" (usually takes ~250ms).
5
+ */
1
6
  function createSimpleStackTrace(options) {
2
- const { message = "$$stack trace error", stackTraceLimit = 1 } = options || {};
3
- const limit = Error.stackTraceLimit;
4
- const prepareStackTrace = Error.prepareStackTrace;
5
- Error.stackTraceLimit = stackTraceLimit;
6
- Error.prepareStackTrace = (e) => e.stack;
7
- const err = new Error(message);
8
- const stackTrace = err.stack || "";
9
- Error.prepareStackTrace = prepareStackTrace;
10
- Error.stackTraceLimit = limit;
11
- return stackTrace;
7
+ const { message = "$$stack trace error", stackTraceLimit = 1 } = options || {};
8
+ const limit = Error.stackTraceLimit;
9
+ const prepareStackTrace = Error.prepareStackTrace;
10
+ Error.stackTraceLimit = stackTraceLimit;
11
+ Error.prepareStackTrace = (e) => e.stack;
12
+ const err = new Error(message);
13
+ const stackTrace = err.stack || "";
14
+ Error.prepareStackTrace = prepareStackTrace;
15
+ Error.stackTraceLimit = limit;
16
+ return stackTrace;
12
17
  }
13
18
  function notNullish(v) {
14
- return v != null;
19
+ return v != null;
15
20
  }
16
21
  function assertTypes(value, name, types) {
17
- const receivedType = typeof value;
18
- const pass = types.includes(receivedType);
19
- if (!pass) {
20
- throw new TypeError(
21
- `${name} value must be ${types.join(" or ")}, received "${receivedType}"`
22
- );
23
- }
22
+ const receivedType = typeof value;
23
+ const pass = types.includes(receivedType);
24
+ if (!pass) {
25
+ throw new TypeError(`${name} value must be ${types.join(" or ")}, received "${receivedType}"`);
26
+ }
24
27
  }
25
28
  function isPrimitive(value) {
26
- return value === null || typeof value !== "function" && typeof value !== "object";
29
+ return value === null || typeof value !== "function" && typeof value !== "object";
27
30
  }
28
31
  function slash(path) {
29
- return path.replace(/\\/g, "/");
32
+ return path.replace(/\\/g, "/");
30
33
  }
31
34
  function parseRegexp(input) {
32
- const m = input.match(/(\/?)(.+)\1([a-z]*)/i);
33
- if (!m) {
34
- return /$^/;
35
- }
36
- if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) {
37
- return new RegExp(input);
38
- }
39
- return new RegExp(m[2], m[3]);
35
+ const m = input.match(/(\/?)(.+)\1([a-z]*)/i);
36
+ if (!m) {
37
+ return /$^/;
38
+ }
39
+ if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) {
40
+ return new RegExp(input);
41
+ }
42
+ return new RegExp(m[2], m[3]);
40
43
  }
41
44
  function toArray(array) {
42
- if (array === null || array === void 0) {
43
- array = [];
44
- }
45
- if (Array.isArray(array)) {
46
- return array;
47
- }
48
- return [array];
45
+ if (array === null || array === undefined) {
46
+ array = [];
47
+ }
48
+ if (Array.isArray(array)) {
49
+ return array;
50
+ }
51
+ return [array];
49
52
  }
50
53
  function isObject(item) {
51
- return item != null && typeof item === "object" && !Array.isArray(item);
54
+ return item != null && typeof item === "object" && !Array.isArray(item);
52
55
  }
53
56
  function isFinalObj(obj) {
54
- return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype;
57
+ return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype;
55
58
  }
56
59
  function getType(value) {
57
- return Object.prototype.toString.apply(value).slice(8, -1);
60
+ return Object.prototype.toString.apply(value).slice(8, -1);
58
61
  }
59
62
  function collectOwnProperties(obj, collector) {
60
- const collect = typeof collector === "function" ? collector : (key) => collector.add(key);
61
- Object.getOwnPropertyNames(obj).forEach(collect);
62
- Object.getOwnPropertySymbols(obj).forEach(collect);
63
+ const collect = typeof collector === "function" ? collector : (key) => collector.add(key);
64
+ Object.getOwnPropertyNames(obj).forEach(collect);
65
+ Object.getOwnPropertySymbols(obj).forEach(collect);
63
66
  }
64
67
  function getOwnProperties(obj) {
65
- const ownProps = /* @__PURE__ */ new Set();
66
- if (isFinalObj(obj)) {
67
- return [];
68
- }
69
- collectOwnProperties(obj, ownProps);
70
- return Array.from(ownProps);
68
+ const ownProps = new Set();
69
+ if (isFinalObj(obj)) {
70
+ return [];
71
+ }
72
+ collectOwnProperties(obj, ownProps);
73
+ return Array.from(ownProps);
71
74
  }
72
75
  const defaultCloneOptions = { forceWritable: false };
73
76
  function deepClone(val, options = defaultCloneOptions) {
74
- const seen = /* @__PURE__ */ new WeakMap();
75
- return clone(val, seen, options);
77
+ const seen = new WeakMap();
78
+ return clone(val, seen, options);
76
79
  }
77
80
  function clone(val, seen, options = defaultCloneOptions) {
78
- let k, out;
79
- if (seen.has(val)) {
80
- return seen.get(val);
81
- }
82
- if (Array.isArray(val)) {
83
- out = Array.from({ length: k = val.length });
84
- seen.set(val, out);
85
- while (k--) {
86
- out[k] = clone(val[k], seen, options);
87
- }
88
- return out;
89
- }
90
- if (Object.prototype.toString.call(val) === "[object Object]") {
91
- out = Object.create(Object.getPrototypeOf(val));
92
- seen.set(val, out);
93
- const props = getOwnProperties(val);
94
- for (const k2 of props) {
95
- const descriptor = Object.getOwnPropertyDescriptor(val, k2);
96
- if (!descriptor) {
97
- continue;
98
- }
99
- const cloned = clone(val[k2], seen, options);
100
- if (options.forceWritable) {
101
- Object.defineProperty(out, k2, {
102
- enumerable: descriptor.enumerable,
103
- configurable: true,
104
- writable: true,
105
- value: cloned
106
- });
107
- } else if ("get" in descriptor) {
108
- Object.defineProperty(out, k2, {
109
- ...descriptor,
110
- get() {
111
- return cloned;
112
- }
113
- });
114
- } else {
115
- Object.defineProperty(out, k2, {
116
- ...descriptor,
117
- value: cloned
118
- });
119
- }
120
- }
121
- return out;
122
- }
123
- return val;
124
- }
125
- function noop() {
126
- }
127
- function objectAttr(source, path, defaultValue = void 0) {
128
- const paths = path.replace(/\[(\d+)\]/g, ".$1").split(".");
129
- let result = source;
130
- for (const p of paths) {
131
- result = new Object(result)[p];
132
- if (result === void 0) {
133
- return defaultValue;
134
- }
135
- }
136
- return result;
81
+ let k, out;
82
+ if (seen.has(val)) {
83
+ return seen.get(val);
84
+ }
85
+ if (Array.isArray(val)) {
86
+ out = Array.from({ length: k = val.length });
87
+ seen.set(val, out);
88
+ while (k--) {
89
+ out[k] = clone(val[k], seen, options);
90
+ }
91
+ return out;
92
+ }
93
+ if (Object.prototype.toString.call(val) === "[object Object]") {
94
+ out = Object.create(Object.getPrototypeOf(val));
95
+ seen.set(val, out);
96
+ const props = getOwnProperties(val);
97
+ for (const k of props) {
98
+ const descriptor = Object.getOwnPropertyDescriptor(val, k);
99
+ if (!descriptor) {
100
+ continue;
101
+ }
102
+ const cloned = clone(val[k], seen, options);
103
+ if (options.forceWritable) {
104
+ Object.defineProperty(out, k, {
105
+ enumerable: descriptor.enumerable,
106
+ configurable: true,
107
+ writable: true,
108
+ value: cloned
109
+ });
110
+ } else if ("get" in descriptor) {
111
+ Object.defineProperty(out, k, {
112
+ ...descriptor,
113
+ get() {
114
+ return cloned;
115
+ }
116
+ });
117
+ } else {
118
+ Object.defineProperty(out, k, {
119
+ ...descriptor,
120
+ value: cloned
121
+ });
122
+ }
123
+ }
124
+ return out;
125
+ }
126
+ return val;
127
+ }
128
+ function noop() {}
129
+ function objectAttr(source, path, defaultValue = undefined) {
130
+ const paths = path.replace(/\[(\d+)\]/g, ".$1").split(".");
131
+ let result = source;
132
+ for (const p of paths) {
133
+ result = new Object(result)[p];
134
+ if (result === undefined) {
135
+ return defaultValue;
136
+ }
137
+ }
138
+ return result;
137
139
  }
138
140
  function createDefer() {
139
- let resolve = null;
140
- let reject = null;
141
- const p = new Promise((_resolve, _reject) => {
142
- resolve = _resolve;
143
- reject = _reject;
144
- });
145
- p.resolve = resolve;
146
- p.reject = reject;
147
- return p;
148
- }
141
+ let resolve = null;
142
+ let reject = null;
143
+ const p = new Promise((_resolve, _reject) => {
144
+ resolve = _resolve;
145
+ reject = _reject;
146
+ });
147
+ p.resolve = resolve;
148
+ p.reject = reject;
149
+ return p;
150
+ }
151
+ /**
152
+ * If code starts with a function call, will return its last index, respecting arguments.
153
+ * This will return 25 - last ending character of toMatch ")"
154
+ * Also works with callbacks
155
+ * ```
156
+ * toMatch({ test: '123' });
157
+ * toBeAliased('123')
158
+ * ```
159
+ */
149
160
  function getCallLastIndex(code) {
150
- let charIndex = -1;
151
- let inString = null;
152
- let startedBracers = 0;
153
- let endedBracers = 0;
154
- let beforeChar = null;
155
- while (charIndex <= code.length) {
156
- beforeChar = code[charIndex];
157
- charIndex++;
158
- const char = code[charIndex];
159
- const isCharString = char === '"' || char === "'" || char === "`";
160
- if (isCharString && beforeChar !== "\\") {
161
- if (inString === char) {
162
- inString = null;
163
- } else if (!inString) {
164
- inString = char;
165
- }
166
- }
167
- if (!inString) {
168
- if (char === "(") {
169
- startedBracers++;
170
- }
171
- if (char === ")") {
172
- endedBracers++;
173
- }
174
- }
175
- if (startedBracers && endedBracers && startedBracers === endedBracers) {
176
- return charIndex;
177
- }
178
- }
179
- return null;
161
+ let charIndex = -1;
162
+ let inString = null;
163
+ let startedBracers = 0;
164
+ let endedBracers = 0;
165
+ let beforeChar = null;
166
+ while (charIndex <= code.length) {
167
+ beforeChar = code[charIndex];
168
+ charIndex++;
169
+ const char = code[charIndex];
170
+ const isCharString = char === "\"" || char === "'" || char === "`";
171
+ if (isCharString && beforeChar !== "\\") {
172
+ if (inString === char) {
173
+ inString = null;
174
+ } else if (!inString) {
175
+ inString = char;
176
+ }
177
+ }
178
+ if (!inString) {
179
+ if (char === "(") {
180
+ startedBracers++;
181
+ }
182
+ if (char === ")") {
183
+ endedBracers++;
184
+ }
185
+ }
186
+ if (startedBracers && endedBracers && startedBracers === endedBracers) {
187
+ return charIndex;
188
+ }
189
+ }
190
+ return null;
180
191
  }
181
192
  function isNegativeNaN(val) {
182
- if (!Number.isNaN(val)) {
183
- return false;
184
- }
185
- const f64 = new Float64Array(1);
186
- f64[0] = val;
187
- const u32 = new Uint32Array(f64.buffer);
188
- const isNegative = u32[1] >>> 31 === 1;
189
- return isNegative;
193
+ if (!Number.isNaN(val)) {
194
+ return false;
195
+ }
196
+ const f64 = new Float64Array(1);
197
+ f64[0] = val;
198
+ const u32 = new Uint32Array(f64.buffer);
199
+ const isNegative = u32[1] >>> 31 === 1;
200
+ return isNegative;
190
201
  }
191
202
  function toString(v) {
192
- return Object.prototype.toString.call(v);
203
+ return Object.prototype.toString.call(v);
193
204
  }
194
205
  function isPlainObject(val) {
195
- return toString(val) === "[object Object]" && (!val.constructor || val.constructor.name === "Object");
206
+ return toString(val) === "[object Object]" && (!val.constructor || val.constructor.name === "Object");
196
207
  }
197
208
  function isMergeableObject(item) {
198
- return isPlainObject(item) && !Array.isArray(item);
199
- }
209
+ return isPlainObject(item) && !Array.isArray(item);
210
+ }
211
+ /**
212
+ * Deep merge :P
213
+ *
214
+ * Will merge objects only if they are plain
215
+ *
216
+ * Do not merge types - it is very expensive and usually it's better to case a type here
217
+ */
200
218
  function deepMerge(target, ...sources) {
201
- if (!sources.length) {
202
- return target;
203
- }
204
- const source = sources.shift();
205
- if (source === void 0) {
206
- return target;
207
- }
208
- if (isMergeableObject(target) && isMergeableObject(source)) {
209
- Object.keys(source).forEach((key) => {
210
- const _source = source;
211
- if (isMergeableObject(_source[key])) {
212
- if (!target[key]) {
213
- target[key] = {};
214
- }
215
- deepMerge(target[key], _source[key]);
216
- } else {
217
- target[key] = _source[key];
218
- }
219
- });
220
- }
221
- return deepMerge(target, ...sources);
219
+ if (!sources.length) {
220
+ return target;
221
+ }
222
+ const source = sources.shift();
223
+ if (source === undefined) {
224
+ return target;
225
+ }
226
+ if (isMergeableObject(target) && isMergeableObject(source)) {
227
+ Object.keys(source).forEach((key) => {
228
+ const _source = source;
229
+ if (isMergeableObject(_source[key])) {
230
+ if (!target[key]) {
231
+ target[key] = {};
232
+ }
233
+ deepMerge(target[key], _source[key]);
234
+ } else {
235
+ target[key] = _source[key];
236
+ }
237
+ });
238
+ }
239
+ return deepMerge(target, ...sources);
222
240
  }
223
241
 
224
242
  export { assertTypes, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, getCallLastIndex, getOwnProperties, getType, isNegativeNaN, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray };