@vitest/utils 4.1.5 → 5.0.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/display.d.ts CHANGED
@@ -1,30 +1,18 @@
1
1
  import { PrettyFormatOptions } from '@vitest/pretty-format';
2
2
 
3
- type Inspect = (value: unknown, options: Options) => string;
4
- interface Options {
5
- showHidden: boolean;
6
- depth: number;
7
- colors: boolean;
8
- customInspect: boolean;
9
- showProxy: boolean;
10
- maxArrayLength: number;
11
- breakLength: number;
12
- truncate: number;
13
- seen: unknown[];
14
- inspect: Inspect;
15
- stylize: (value: string, styleType: string) => string;
16
- }
17
- type LoupeOptions = Partial<Options>;
18
3
  interface StringifyOptions extends PrettyFormatOptions {
19
4
  maxLength?: number;
20
5
  filterNode?: string | ((node: any) => boolean);
21
6
  }
22
7
  declare function stringify(object: unknown, maxDepth?: number, { maxLength, filterNode, ...options }?: StringifyOptions): string;
23
8
  declare const formatRegExp: RegExp;
24
- declare function format(...args: unknown[]): string;
25
- declare function browserFormat(...args: unknown[]): string;
26
- declare function inspect(obj: unknown, options?: LoupeOptions): string;
27
- declare function objDisplay(obj: unknown, options?: LoupeOptions): string;
9
+ declare function format(args: unknown[], options?: InspectOptions): string;
10
+ interface InspectOptions extends StringifyOptions {
11
+ truncate?: number;
12
+ multiline?: boolean;
13
+ }
14
+ declare function inspect(obj: unknown, options?: InspectOptions): string;
15
+ declare function truncateString(string: string, maxLength: number): string;
28
16
 
29
- export { browserFormat, format, formatRegExp, inspect, objDisplay, stringify };
30
- export type { LoupeOptions, StringifyOptions };
17
+ export { format, formatRegExp, inspect, stringify, truncateString };
18
+ export type { InspectOptions, StringifyOptions };
package/dist/display.js CHANGED
@@ -1,579 +1,5 @@
1
1
  import { plugins, createDOMElementFilter, format as format$1 } from '@vitest/pretty-format';
2
2
 
3
- const ansiColors = {
4
- bold: ['1', '22'],
5
- dim: ['2', '22'],
6
- italic: ['3', '23'],
7
- underline: ['4', '24'],
8
- // 5 & 6 are blinking
9
- inverse: ['7', '27'],
10
- hidden: ['8', '28'],
11
- strike: ['9', '29'],
12
- // 10-20 are fonts
13
- // 21-29 are resets for 1-9
14
- black: ['30', '39'],
15
- red: ['31', '39'],
16
- green: ['32', '39'],
17
- yellow: ['33', '39'],
18
- blue: ['34', '39'],
19
- magenta: ['35', '39'],
20
- cyan: ['36', '39'],
21
- white: ['37', '39'],
22
- brightblack: ['30;1', '39'],
23
- brightred: ['31;1', '39'],
24
- brightgreen: ['32;1', '39'],
25
- brightyellow: ['33;1', '39'],
26
- brightblue: ['34;1', '39'],
27
- brightmagenta: ['35;1', '39'],
28
- brightcyan: ['36;1', '39'],
29
- brightwhite: ['37;1', '39'],
30
- grey: ['90', '39'],
31
- };
32
- const styles = {
33
- special: 'cyan',
34
- number: 'yellow',
35
- bigint: 'yellow',
36
- boolean: 'yellow',
37
- undefined: 'grey',
38
- null: 'bold',
39
- string: 'green',
40
- symbol: 'green',
41
- date: 'magenta',
42
- regexp: 'red',
43
- };
44
- const truncator = '…';
45
- function colorise(value, styleType) {
46
- const color = ansiColors[styles[styleType]] || ansiColors[styleType] || '';
47
- if (!color) {
48
- return String(value);
49
- }
50
- return `\u001b[${color[0]}m${String(value)}\u001b[${color[1]}m`;
51
- }
52
- function normaliseOptions({ showHidden = false, depth = 2, colors = false, customInspect = true, showProxy = false, maxArrayLength = Infinity, breakLength = Infinity, seen = [],
53
- // eslint-disable-next-line no-shadow
54
- truncate = Infinity, stylize = String, } = {}, inspect) {
55
- const options = {
56
- showHidden: Boolean(showHidden),
57
- depth: Number(depth),
58
- colors: Boolean(colors),
59
- customInspect: Boolean(customInspect),
60
- showProxy: Boolean(showProxy),
61
- maxArrayLength: Number(maxArrayLength),
62
- breakLength: Number(breakLength),
63
- truncate: Number(truncate),
64
- seen,
65
- inspect,
66
- stylize,
67
- };
68
- if (options.colors) {
69
- options.stylize = colorise;
70
- }
71
- return options;
72
- }
73
- function isHighSurrogate(char) {
74
- return char >= '\ud800' && char <= '\udbff';
75
- }
76
- function truncate(string, length, tail = truncator) {
77
- string = String(string);
78
- const tailLength = tail.length;
79
- const stringLength = string.length;
80
- if (tailLength > length && stringLength > tailLength) {
81
- return tail;
82
- }
83
- if (stringLength > length && stringLength > tailLength) {
84
- let end = length - tailLength;
85
- if (end > 0 && isHighSurrogate(string[end - 1])) {
86
- end = end - 1;
87
- }
88
- return `${string.slice(0, end)}${tail}`;
89
- }
90
- return string;
91
- }
92
- // eslint-disable-next-line complexity
93
- function inspectList(list, options, inspectItem, separator = ', ') {
94
- inspectItem = inspectItem || options.inspect;
95
- const size = list.length;
96
- if (size === 0)
97
- return '';
98
- const originalLength = options.truncate;
99
- let output = '';
100
- let peek = '';
101
- let truncated = '';
102
- for (let i = 0; i < size; i += 1) {
103
- const last = i + 1 === list.length;
104
- const secondToLast = i + 2 === list.length;
105
- truncated = `${truncator}(${list.length - i})`;
106
- const value = list[i];
107
- // If there is more than one remaining we need to account for a separator of `, `
108
- options.truncate = originalLength - output.length - (last ? 0 : separator.length);
109
- const string = peek || inspectItem(value, options) + (last ? '' : separator);
110
- const nextLength = output.length + string.length;
111
- const truncatedLength = nextLength + truncated.length;
112
- // If this is the last element, and adding it would
113
- // take us over length, but adding the truncator wouldn't - then break now
114
- if (last && nextLength > originalLength && output.length + truncated.length <= originalLength) {
115
- break;
116
- }
117
- // If this isn't the last or second to last element to scan,
118
- // but the string is already over length then break here
119
- if (!last && !secondToLast && truncatedLength > originalLength) {
120
- break;
121
- }
122
- // Peek at the next string to determine if we should
123
- // break early before adding this item to the output
124
- peek = last ? '' : inspectItem(list[i + 1], options) + (secondToLast ? '' : separator);
125
- // If we have one element left, but this element and
126
- // the next takes over length, the break early
127
- if (!last && secondToLast && truncatedLength > originalLength && nextLength + peek.length > originalLength) {
128
- break;
129
- }
130
- output += string;
131
- // If the next element takes us to length -
132
- // but there are more after that, then we should truncate now
133
- if (!last && !secondToLast && nextLength + peek.length >= originalLength) {
134
- truncated = `${truncator}(${list.length - i - 1})`;
135
- break;
136
- }
137
- truncated = '';
138
- }
139
- return `${output}${truncated}`;
140
- }
141
- function quoteComplexKey(key) {
142
- if (key.match(/^[a-zA-Z_][a-zA-Z_0-9]*$/)) {
143
- return key;
144
- }
145
- return JSON.stringify(key)
146
- .replace(/'/g, "\\'")
147
- .replace(/\\"/g, '"')
148
- .replace(/(^"|"$)/g, "'");
149
- }
150
- function inspectProperty([key, value], options) {
151
- options.truncate -= 2;
152
- if (typeof key === 'string') {
153
- key = quoteComplexKey(key);
154
- }
155
- else if (typeof key !== 'number') {
156
- key = `[${options.inspect(key, options)}]`;
157
- }
158
- options.truncate -= key.length;
159
- value = options.inspect(value, options);
160
- return `${key}: ${value}`;
161
- }
162
-
163
- function inspectArray(array, options) {
164
- // Object.keys will always output the Array indices first, so we can slice by
165
- // `array.length` to get non-index properties
166
- const nonIndexProperties = Object.keys(array).slice(array.length);
167
- if (!array.length && !nonIndexProperties.length)
168
- return '[]';
169
- options.truncate -= 4;
170
- const listContents = inspectList(array, options);
171
- options.truncate -= listContents.length;
172
- let propertyContents = '';
173
- if (nonIndexProperties.length) {
174
- propertyContents = inspectList(nonIndexProperties.map(key => [key, array[key]]), options, inspectProperty);
175
- }
176
- return `[ ${listContents}${propertyContents ? `, ${propertyContents}` : ''} ]`;
177
- }
178
-
179
- const getArrayName = (array) => {
180
- // We need to special case Node.js' Buffers, which report to be Uint8Array
181
- // @ts-ignore
182
- if (typeof Buffer === 'function' && array instanceof Buffer) {
183
- return 'Buffer';
184
- }
185
- if (array[Symbol.toStringTag]) {
186
- return array[Symbol.toStringTag];
187
- }
188
- return array.constructor.name;
189
- };
190
- function inspectTypedArray(array, options) {
191
- const name = getArrayName(array);
192
- options.truncate -= name.length + 4;
193
- // Object.keys will always output the Array indices first, so we can slice by
194
- // `array.length` to get non-index properties
195
- const nonIndexProperties = Object.keys(array).slice(array.length);
196
- if (!array.length && !nonIndexProperties.length)
197
- return `${name}[]`;
198
- // As we know TypedArrays only contain Unsigned Integers, we can skip inspecting each one and simply
199
- // stylise the toString() value of them
200
- let output = '';
201
- for (let i = 0; i < array.length; i++) {
202
- const string = `${options.stylize(truncate(array[i], options.truncate), 'number')}${i === array.length - 1 ? '' : ', '}`;
203
- options.truncate -= string.length;
204
- if (array[i] !== array.length && options.truncate <= 3) {
205
- output += `${truncator}(${array.length - array[i] + 1})`;
206
- break;
207
- }
208
- output += string;
209
- }
210
- let propertyContents = '';
211
- if (nonIndexProperties.length) {
212
- propertyContents = inspectList(nonIndexProperties.map(key => [key, array[key]]), options, inspectProperty);
213
- }
214
- return `${name}[ ${output}${propertyContents ? `, ${propertyContents}` : ''} ]`;
215
- }
216
-
217
- function inspectDate(dateObject, options) {
218
- const stringRepresentation = dateObject.toJSON();
219
- if (stringRepresentation === null) {
220
- return 'Invalid Date';
221
- }
222
- const split = stringRepresentation.split('T');
223
- const date = split[0];
224
- // If we need to - truncate the time portion, but never the date
225
- return options.stylize(`${date}T${truncate(split[1], options.truncate - date.length - 1)}`, 'date');
226
- }
227
-
228
- function inspectFunction(func, options) {
229
- const functionType = func[Symbol.toStringTag] || 'Function';
230
- const name = func.name;
231
- if (!name) {
232
- return options.stylize(`[${functionType}]`, 'special');
233
- }
234
- return options.stylize(`[${functionType} ${truncate(name, options.truncate - 11)}]`, 'special');
235
- }
236
-
237
- function inspectMapEntry([key, value], options) {
238
- options.truncate -= 4;
239
- key = options.inspect(key, options);
240
- options.truncate -= key.length;
241
- value = options.inspect(value, options);
242
- return `${key} => ${value}`;
243
- }
244
- // IE11 doesn't support `map.entries()`
245
- function mapToEntries(map) {
246
- const entries = [];
247
- map.forEach((value, key) => {
248
- entries.push([key, value]);
249
- });
250
- return entries;
251
- }
252
- function inspectMap(map, options) {
253
- if (map.size === 0)
254
- return 'Map{}';
255
- options.truncate -= 7;
256
- return `Map{ ${inspectList(mapToEntries(map), options, inspectMapEntry)} }`;
257
- }
258
-
259
- const isNaN = Number.isNaN || (i => i !== i); // eslint-disable-line no-self-compare
260
- function inspectNumber(number, options) {
261
- if (isNaN(number)) {
262
- return options.stylize('NaN', 'number');
263
- }
264
- if (number === Infinity) {
265
- return options.stylize('Infinity', 'number');
266
- }
267
- if (number === -Infinity) {
268
- return options.stylize('-Infinity', 'number');
269
- }
270
- if (number === 0) {
271
- return options.stylize(1 / number === Infinity ? '+0' : '-0', 'number');
272
- }
273
- return options.stylize(truncate(String(number), options.truncate), 'number');
274
- }
275
-
276
- function inspectBigInt(number, options) {
277
- let nums = truncate(number.toString(), options.truncate - 1);
278
- if (nums !== truncator)
279
- nums += 'n';
280
- return options.stylize(nums, 'bigint');
281
- }
282
-
283
- function inspectRegExp(value, options) {
284
- const flags = value.toString().split('/')[2];
285
- const sourceLength = options.truncate - (2 + flags.length);
286
- const source = value.source;
287
- return options.stylize(`/${truncate(source, sourceLength)}/${flags}`, 'regexp');
288
- }
289
-
290
- // IE11 doesn't support `Array.from(set)`
291
- function arrayFromSet(set) {
292
- const values = [];
293
- set.forEach(value => {
294
- values.push(value);
295
- });
296
- return values;
297
- }
298
- function inspectSet(set, options) {
299
- if (set.size === 0)
300
- return 'Set{}';
301
- options.truncate -= 7;
302
- return `Set{ ${inspectList(arrayFromSet(set), options)} }`;
303
- }
304
-
305
- const stringEscapeChars = new RegExp("['\\u0000-\\u001f\\u007f-\\u009f\\u00ad\\u0600-\\u0604\\u070f\\u17b4\\u17b5" +
306
- '\\u200c-\\u200f\\u2028-\\u202f\\u2060-\\u206f\\ufeff\\ufff0-\\uffff]', 'g');
307
- const escapeCharacters = {
308
- '\b': '\\b',
309
- '\t': '\\t',
310
- '\n': '\\n',
311
- '\f': '\\f',
312
- '\r': '\\r',
313
- "'": "\\'",
314
- '\\': '\\\\',
315
- };
316
- const hex = 16;
317
- function escape(char) {
318
- return (escapeCharacters[char] ||
319
- `\\u${`0000${char.charCodeAt(0).toString(hex)}`.slice(-4)}`);
320
- }
321
- function inspectString(string, options) {
322
- if (stringEscapeChars.test(string)) {
323
- string = string.replace(stringEscapeChars, escape);
324
- }
325
- return options.stylize(`'${truncate(string, options.truncate - 2)}'`, 'string');
326
- }
327
-
328
- function inspectSymbol(value) {
329
- if ('description' in Symbol.prototype) {
330
- return value.description ? `Symbol(${value.description})` : 'Symbol()';
331
- }
332
- return value.toString();
333
- }
334
-
335
- const getPromiseValue = () => 'Promise{…}';
336
-
337
- function inspectObject$1(object, options) {
338
- const properties = Object.getOwnPropertyNames(object);
339
- const symbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(object) : [];
340
- if (properties.length === 0 && symbols.length === 0) {
341
- return '{}';
342
- }
343
- options.truncate -= 4;
344
- options.seen = options.seen || [];
345
- if (options.seen.includes(object)) {
346
- return '[Circular]';
347
- }
348
- options.seen.push(object);
349
- const propertyContents = inspectList(properties.map(key => [key, object[key]]), options, inspectProperty);
350
- const symbolContents = inspectList(symbols.map(key => [key, object[key]]), options, inspectProperty);
351
- options.seen.pop();
352
- let sep = '';
353
- if (propertyContents && symbolContents) {
354
- sep = ', ';
355
- }
356
- return `{ ${propertyContents}${sep}${symbolContents} }`;
357
- }
358
-
359
- const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag ? Symbol.toStringTag : false;
360
- function inspectClass(value, options) {
361
- let name = '';
362
- if (toStringTag && toStringTag in value) {
363
- name = value[toStringTag];
364
- }
365
- name = name || value.constructor.name;
366
- // Babel transforms anonymous classes to the name `_class`
367
- if (!name || name === '_class') {
368
- name = '<Anonymous Class>';
369
- }
370
- options.truncate -= name.length;
371
- return `${name}${inspectObject$1(value, options)}`;
372
- }
373
-
374
- function inspectArguments(args, options) {
375
- if (args.length === 0)
376
- return 'Arguments[]';
377
- options.truncate -= 13;
378
- return `Arguments[ ${inspectList(args, options)} ]`;
379
- }
380
-
381
- const errorKeys = [
382
- 'stack',
383
- 'line',
384
- 'column',
385
- 'name',
386
- 'message',
387
- 'fileName',
388
- 'lineNumber',
389
- 'columnNumber',
390
- 'number',
391
- 'description',
392
- 'cause',
393
- ];
394
- function inspectObject(error, options) {
395
- const properties = Object.getOwnPropertyNames(error).filter(key => errorKeys.indexOf(key) === -1);
396
- const name = error.name;
397
- options.truncate -= name.length;
398
- let message = '';
399
- if (typeof error.message === 'string') {
400
- message = truncate(error.message, options.truncate);
401
- }
402
- else {
403
- properties.unshift('message');
404
- }
405
- message = message ? `: ${message}` : '';
406
- options.truncate -= message.length + 5;
407
- options.seen = options.seen || [];
408
- if (options.seen.includes(error)) {
409
- return '[Circular]';
410
- }
411
- options.seen.push(error);
412
- const propertyContents = inspectList(properties.map(key => [key, error[key]]), options, inspectProperty);
413
- return `${name}${message}${propertyContents ? ` { ${propertyContents} }` : ''}`;
414
- }
415
-
416
- function inspectAttribute([key, value], options) {
417
- options.truncate -= 3;
418
- if (!value) {
419
- return `${options.stylize(String(key), 'yellow')}`;
420
- }
421
- return `${options.stylize(String(key), 'yellow')}=${options.stylize(`"${value}"`, 'string')}`;
422
- }
423
- function inspectNodeCollection(collection, options) {
424
- return inspectList(collection, options, inspectNode, '\n');
425
- }
426
- function inspectNode(node, options) {
427
- switch (node.nodeType) {
428
- case 1:
429
- return inspectHTML(node, options);
430
- case 3:
431
- return options.inspect(node.data, options);
432
- default:
433
- return options.inspect(node, options);
434
- }
435
- }
436
- // @ts-ignore (Deno doesn't have Element)
437
- function inspectHTML(element, options) {
438
- const properties = element.getAttributeNames();
439
- const name = element.tagName.toLowerCase();
440
- const head = options.stylize(`<${name}`, 'special');
441
- const headClose = options.stylize(`>`, 'special');
442
- const tail = options.stylize(`</${name}>`, 'special');
443
- options.truncate -= name.length * 2 + 5;
444
- let propertyContents = '';
445
- if (properties.length > 0) {
446
- propertyContents += ' ';
447
- propertyContents += inspectList(properties.map((key) => [key, element.getAttribute(key)]), options, inspectAttribute, ' ');
448
- }
449
- options.truncate -= propertyContents.length;
450
- const truncate = options.truncate;
451
- let children = inspectNodeCollection(element.children, options);
452
- if (children && children.length > truncate) {
453
- children = `${truncator}(${element.children.length})`;
454
- }
455
- return `${head}${propertyContents}${headClose}${children}${tail}`;
456
- }
457
-
458
- /* !
459
- * loupe
460
- * Copyright(c) 2013 Jake Luer <jake@alogicalparadox.com>
461
- * MIT Licensed
462
- */
463
- const symbolsSupported = typeof Symbol === 'function' && typeof Symbol.for === 'function';
464
- const chaiInspect = symbolsSupported ? Symbol.for('chai/inspect') : '@@chai/inspect';
465
- const nodeInspect = Symbol.for('nodejs.util.inspect.custom');
466
- const constructorMap = new WeakMap();
467
- const stringTagMap = {};
468
- const baseTypesMap = {
469
- undefined: (value, options) => options.stylize('undefined', 'undefined'),
470
- null: (value, options) => options.stylize('null', 'null'),
471
- boolean: (value, options) => options.stylize(String(value), 'boolean'),
472
- Boolean: (value, options) => options.stylize(String(value), 'boolean'),
473
- number: inspectNumber,
474
- Number: inspectNumber,
475
- bigint: inspectBigInt,
476
- BigInt: inspectBigInt,
477
- string: inspectString,
478
- String: inspectString,
479
- function: inspectFunction,
480
- Function: inspectFunction,
481
- symbol: inspectSymbol,
482
- // A Symbol polyfill will return `Symbol` not `symbol` from typedetect
483
- Symbol: inspectSymbol,
484
- Array: inspectArray,
485
- Date: inspectDate,
486
- Map: inspectMap,
487
- Set: inspectSet,
488
- RegExp: inspectRegExp,
489
- Promise: getPromiseValue,
490
- // WeakSet, WeakMap are totally opaque to us
491
- WeakSet: (value, options) => options.stylize('WeakSet{…}', 'special'),
492
- WeakMap: (value, options) => options.stylize('WeakMap{…}', 'special'),
493
- Arguments: inspectArguments,
494
- Int8Array: inspectTypedArray,
495
- Uint8Array: inspectTypedArray,
496
- Uint8ClampedArray: inspectTypedArray,
497
- Int16Array: inspectTypedArray,
498
- Uint16Array: inspectTypedArray,
499
- Int32Array: inspectTypedArray,
500
- Uint32Array: inspectTypedArray,
501
- Float32Array: inspectTypedArray,
502
- Float64Array: inspectTypedArray,
503
- Generator: () => '',
504
- DataView: () => '',
505
- ArrayBuffer: () => '',
506
- Error: inspectObject,
507
- HTMLCollection: inspectNodeCollection,
508
- NodeList: inspectNodeCollection,
509
- };
510
- // eslint-disable-next-line complexity
511
- const inspectCustom = (value, options, type, inspectFn) => {
512
- if (chaiInspect in value && typeof value[chaiInspect] === 'function') {
513
- return value[chaiInspect](options);
514
- }
515
- if (nodeInspect in value && typeof value[nodeInspect] === 'function') {
516
- return value[nodeInspect](options.depth, options, inspectFn);
517
- }
518
- if ('inspect' in value && typeof value.inspect === 'function') {
519
- return value.inspect(options.depth, options);
520
- }
521
- if ('constructor' in value && constructorMap.has(value.constructor)) {
522
- return constructorMap.get(value.constructor)(value, options);
523
- }
524
- if (stringTagMap[type]) {
525
- return stringTagMap[type](value, options);
526
- }
527
- return '';
528
- };
529
- const toString = Object.prototype.toString;
530
- // eslint-disable-next-line complexity
531
- function inspect$1(value, opts = {}) {
532
- const options = normaliseOptions(opts, inspect$1);
533
- const { customInspect } = options;
534
- let type = value === null ? 'null' : typeof value;
535
- if (type === 'object') {
536
- type = toString.call(value).slice(8, -1);
537
- }
538
- // If it is a base value that we already support, then use Loupe's inspector
539
- if (type in baseTypesMap) {
540
- return baseTypesMap[type](value, options);
541
- }
542
- // If `options.customInspect` is set to true then try to use the custom inspector
543
- if (customInspect && value) {
544
- const output = inspectCustom(value, options, type, inspect$1);
545
- if (output) {
546
- if (typeof output === 'string')
547
- return output;
548
- return inspect$1(output, options);
549
- }
550
- }
551
- const proto = value ? Object.getPrototypeOf(value) : false;
552
- // If it's a plain Object then use Loupe's inspector
553
- if (proto === Object.prototype || proto === null) {
554
- return inspectObject$1(value, options);
555
- }
556
- // Specifically account for HTMLElements
557
- // @ts-ignore
558
- if (value && typeof HTMLElement === 'function' && value instanceof HTMLElement) {
559
- return inspectHTML(value, options);
560
- }
561
- if ('constructor' in value) {
562
- // If it is a class, inspect it like an object but add the constructor name
563
- if (value.constructor !== Object) {
564
- return inspectClass(value, options);
565
- }
566
- // If it is an object with an anonymous prototype, display it as an object.
567
- return inspectObject$1(value, options);
568
- }
569
- // last chance to check if it's an object
570
- if (value === Object(value)) {
571
- return inspectObject$1(value, options);
572
- }
573
- // We have run out of options! Just stringify the value
574
- return options.stylize(String(value), type);
575
- }
576
-
577
3
  const { AsymmetricMatcher, DOMCollection, DOMElement, Immutable, ReactElement, ReactTestComponent } = plugins;
578
4
  const PLUGINS = [
579
5
  ReactTestComponent,
@@ -639,23 +65,12 @@ function createNodeFilterFromSelector(selector) {
639
65
  };
640
66
  }
641
67
  const formatRegExp = /%[sdjifoOc%]/g;
642
- function baseFormat(args, options = {}) {
643
- const formatArg = (item, inspecOptions) => {
644
- if (options.prettifyObject) {
645
- return stringify(item, undefined, {
646
- printBasicPrototype: false,
647
- escapeString: false
648
- });
649
- }
650
- return inspect(item, inspecOptions);
651
- };
68
+ function format(args, options = {}) {
69
+ const formatArg = (item) => inspect(item, options);
652
70
  if (typeof args[0] !== "string") {
653
71
  const objects = [];
654
72
  for (let i = 0; i < args.length; i++) {
655
- objects.push(formatArg(args[i], {
656
- depth: 0,
657
- colors: false
658
- }));
73
+ objects.push(formatArg(args[i]));
659
74
  }
660
75
  return objects.join(" ");
661
76
  }
@@ -682,10 +97,7 @@ function baseFormat(args, options = {}) {
682
97
  if (typeof value.toString === "function" && value.toString !== Object.prototype.toString) {
683
98
  return value.toString();
684
99
  }
685
- return formatArg(value, {
686
- depth: 0,
687
- colors: false
688
- });
100
+ return formatArg(value);
689
101
  }
690
102
  return String(value);
691
103
  }
@@ -707,10 +119,7 @@ function baseFormat(args, options = {}) {
707
119
  return Number.parseInt(String(value)).toString();
708
120
  }
709
121
  case "%f": return Number.parseFloat(String(args[i++])).toString();
710
- case "%o": return formatArg(args[i++], {
711
- showHidden: true,
712
- showProxy: true
713
- });
122
+ case "%o":
714
123
  case "%O": return formatArg(args[i++]);
715
124
  case "%c": {
716
125
  i++;
@@ -737,39 +146,91 @@ function baseFormat(args, options = {}) {
737
146
  }
738
147
  return str;
739
148
  }
740
- function format(...args) {
741
- return baseFormat(args);
742
- }
743
- function browserFormat(...args) {
744
- return baseFormat(args, { prettifyObject: true });
149
+ function inspect(obj, options) {
150
+ const { truncate, multiline, ...stringifyOptions } = options ?? {};
151
+ const prettyFormatOptions = {
152
+ singleQuote: true,
153
+ quoteKeys: false,
154
+ min: true,
155
+ spacingInner: " ",
156
+ spacingOuter: " ",
157
+ printBasicPrototype: false,
158
+ compareKeys: null,
159
+ ...multiline ? {
160
+ min: false,
161
+ spacingInner: undefined,
162
+ spacingOuter: undefined
163
+ } : {}
164
+ };
165
+ const threshold = truncate ?? 0;
166
+ const formatted = stringify(obj, undefined, {
167
+ ...prettyFormatOptions,
168
+ ...stringifyOptions,
169
+ maxLength: threshold || undefined
170
+ });
171
+ if (threshold === 0 || formatted.length <= threshold) {
172
+ return formatted;
173
+ }
174
+ // if stringify's adaptive maxDepth (down to 1) fails to truncate enough,
175
+ // - for known types (e.g. string, object, array, etc), apply best effort truncation.
176
+ // - for other values, fallback to maxDepth = 0 which should can show minimal output.
177
+ const type = Object.prototype.toString.call(obj);
178
+ if (typeof obj === "string") {
179
+ let end = threshold - 1;
180
+ if (end > 0 && isHighSurrogate(formatted[end - 1])) {
181
+ end = end - 1;
182
+ }
183
+ return `'${formatted.slice(1, end)}…'`;
184
+ }
185
+ if (type === "[object Array]" || type === "[object Object]" || type === "[object Set]" || type === "[object Map]") {
186
+ return stringifyByMaxWidth(obj, threshold, {
187
+ ...prettyFormatOptions,
188
+ ...stringifyOptions,
189
+ maxDepth: 1
190
+ });
191
+ }
192
+ return stringify(obj, undefined, {
193
+ ...prettyFormatOptions,
194
+ ...stringifyOptions,
195
+ maxDepth: 0
196
+ });
745
197
  }
746
- function inspect(obj, options = {}) {
747
- if (options.truncate === 0) {
748
- options.truncate = Number.POSITIVE_INFINITY;
198
+ function truncateString(string, maxLength) {
199
+ if (string.length <= maxLength) {
200
+ return string;
749
201
  }
750
- return inspect$1(obj, options);
202
+ let end = maxLength - 1;
203
+ if (isHighSurrogate(string[end - 1])) {
204
+ end = end - 1;
205
+ }
206
+ return `${string.slice(0, end)}…`;
751
207
  }
752
- function objDisplay(obj, options = {}) {
753
- if (typeof options.truncate === "undefined") {
754
- options.truncate = 40;
208
+ function stringifyByMaxWidth(object, threshold, options) {
209
+ function evaluate(x) {
210
+ return stringify(object, undefined, {
211
+ ...options,
212
+ maxWidth: x
213
+ });
755
214
  }
756
- const str = inspect(obj, options);
757
- const type = Object.prototype.toString.call(obj);
758
- if (options.truncate && str.length >= options.truncate) {
759
- if (type === "[object Function]") {
760
- const fn = obj;
761
- return !fn.name ? "[Function]" : `[Function: ${fn.name}]`;
762
- } else if (type === "[object Array]") {
763
- return `[ Array(${obj.length}) ]`;
764
- } else if (type === "[object Object]") {
765
- const keys = Object.keys(obj);
766
- const kstr = keys.length > 2 ? `${keys.splice(0, 2).join(", ")}, ...` : keys.join(", ");
767
- return `{ Object (${kstr}) }`;
215
+ const opt = binarySearch(0, threshold, (x) => evaluate(x).length <= threshold);
216
+ return evaluate(opt);
217
+ }
218
+ // find max(x \in [x, y) | f(x) = true)
219
+ // if f(x0) is false, then returns x0.
220
+ function binarySearch(x0, x1, f) {
221
+ while (x0 + 1 < x1) {
222
+ const x = Math.floor((x0 + x1) / 2);
223
+ if (f(x)) {
224
+ x0 = x;
768
225
  } else {
769
- return str;
226
+ x1 = x;
770
227
  }
771
228
  }
772
- return str;
229
+ return x0;
230
+ }
231
+ // https://github.com/chaijs/loupe/pull/79
232
+ function isHighSurrogate(char) {
233
+ return char >= "\ud800" && char <= "\udbff";
773
234
  }
774
235
 
775
- export { browserFormat, format, formatRegExp, inspect, objDisplay, stringify };
236
+ export { format, formatRegExp, inspect, stringify, truncateString };
package/dist/helpers.d.ts CHANGED
@@ -75,6 +75,7 @@ declare function ordinal(i: number): string;
75
75
  */
76
76
  declare function deepMerge<T extends object = object>(target: T, ...sources: any[]): T;
77
77
  declare function unique<T>(array: T[]): T[];
78
+ declare function sanitizeFilePath(s: string): string;
78
79
 
79
- export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, ordinal, shuffle, slash, splitFileAndPostfix, toArray, unique, unwrapId, withTrailingSlash, wrapId };
80
+ export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, ordinal, sanitizeFilePath, shuffle, slash, splitFileAndPostfix, toArray, unique, unwrapId, withTrailingSlash, wrapId };
80
81
  export type { DeferPromise };
package/dist/helpers.js CHANGED
@@ -340,5 +340,9 @@ function deepMerge(target, ...sources) {
340
340
  function unique(array) {
341
341
  return Array.from(new Set(array));
342
342
  }
343
+ function sanitizeFilePath(s) {
344
+ // eslint-disable-next-line no-control-regex
345
+ return s.replace(/[\x00-\x2C\x2E\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, "-");
346
+ }
343
347
 
344
- export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, ordinal, shuffle, slash, splitFileAndPostfix, toArray, unique, unwrapId, withTrailingSlash, wrapId };
348
+ export { assertTypes, cleanUrl, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, filterOutComments, getCallLastIndex, getOwnProperties, getType, isBareImport, isExternalUrl, isNegativeNaN, isObject, isPrimitive, nanoid, noop, notNullish, objectAttr, ordinal, sanitizeFilePath, shuffle, slash, splitFileAndPostfix, toArray, unique, unwrapId, withTrailingSlash, wrapId };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { LoupeOptions, StringifyOptions } from './display.js';
1
+ export { StringifyOptions } from './display.js';
2
2
  export { DeferPromise } from './helpers.js';
3
3
  export { SafeTimers } from './timers.js';
4
4
  export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, MergeInsertions, Nullable, ParsedStack, SerializedError, TestError } from './types.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/utils",
3
3
  "type": "module",
4
- "version": "4.1.5",
4
+ "version": "5.0.0-beta.2",
5
5
  "description": "Shared Vitest utility functions",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -86,14 +86,13 @@
86
86
  "dependencies": {
87
87
  "convert-source-map": "^2.0.0",
88
88
  "tinyrainbow": "^3.1.0",
89
- "@vitest/pretty-format": "4.1.5"
89
+ "@vitest/pretty-format": "5.0.0-beta.2"
90
90
  },
91
91
  "devDependencies": {
92
92
  "@jridgewell/trace-mapping": "0.3.31",
93
93
  "@types/convert-source-map": "^2.0.3",
94
94
  "@types/estree": "^1.0.8",
95
- "diff-sequences": "^29.6.3",
96
- "loupe": "^3.2.1"
95
+ "diff-sequences": "^29.6.3"
97
96
  },
98
97
  "scripts": {
99
98
  "build": "premove dist && rollup -c",