@vitest/utils 3.1.0-beta.1 → 3.1.0

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.
@@ -1,158 +1,153 @@
1
1
  import { plugins, format as format$1 } from '@vitest/pretty-format';
2
2
  import * as loupe from 'loupe';
3
3
 
4
- const {
5
- AsymmetricMatcher,
6
- DOMCollection,
7
- DOMElement,
8
- Immutable,
9
- ReactElement,
10
- ReactTestComponent
11
- } = plugins;
4
+ const { AsymmetricMatcher, DOMCollection, DOMElement, Immutable, ReactElement, ReactTestComponent } = plugins;
12
5
  const PLUGINS = [
13
- ReactTestComponent,
14
- ReactElement,
15
- DOMElement,
16
- DOMCollection,
17
- Immutable,
18
- AsymmetricMatcher
6
+ ReactTestComponent,
7
+ ReactElement,
8
+ DOMElement,
9
+ DOMCollection,
10
+ Immutable,
11
+ AsymmetricMatcher
19
12
  ];
20
- function stringify(object, maxDepth = 10, { maxLength, ...options } = {}) {
21
- const MAX_LENGTH = maxLength ?? 1e4;
22
- let result;
23
- try {
24
- result = format$1(object, {
25
- maxDepth,
26
- escapeString: false,
27
- // min: true,
28
- plugins: PLUGINS,
29
- ...options
30
- });
31
- } catch {
32
- result = format$1(object, {
33
- callToJSON: false,
34
- maxDepth,
35
- escapeString: false,
36
- // min: true,
37
- plugins: PLUGINS,
38
- ...options
39
- });
40
- }
41
- return result.length >= MAX_LENGTH && maxDepth > 1 ? stringify(object, Math.floor(Math.min(maxDepth, Number.MAX_SAFE_INTEGER) / 2), { maxLength, ...options }) : result;
13
+ function stringify(object, maxDepth = 10, { maxLength,...options } = {}) {
14
+ const MAX_LENGTH = maxLength ?? 1e4;
15
+ let result;
16
+ try {
17
+ result = format$1(object, {
18
+ maxDepth,
19
+ escapeString: false,
20
+ plugins: PLUGINS,
21
+ ...options
22
+ });
23
+ } catch {
24
+ result = format$1(object, {
25
+ callToJSON: false,
26
+ maxDepth,
27
+ escapeString: false,
28
+ plugins: PLUGINS,
29
+ ...options
30
+ });
31
+ }
32
+ return result.length >= MAX_LENGTH && maxDepth > 1 ? stringify(object, Math.floor(Math.min(maxDepth, Number.MAX_SAFE_INTEGER) / 2), {
33
+ maxLength,
34
+ ...options
35
+ }) : result;
42
36
  }
43
37
  const formatRegExp = /%[sdjifoOc%]/g;
44
38
  function format(...args) {
45
- if (typeof args[0] !== "string") {
46
- const objects = [];
47
- for (let i2 = 0; i2 < args.length; i2++) {
48
- objects.push(inspect(args[i2], { depth: 0, colors: false }));
49
- }
50
- return objects.join(" ");
51
- }
52
- const len = args.length;
53
- let i = 1;
54
- const template = args[0];
55
- let str = String(template).replace(formatRegExp, (x) => {
56
- if (x === "%%") {
57
- return "%";
58
- }
59
- if (i >= len) {
60
- return x;
61
- }
62
- switch (x) {
63
- case "%s": {
64
- const value = args[i++];
65
- if (typeof value === "bigint") {
66
- return `${value.toString()}n`;
67
- }
68
- if (typeof value === "number" && value === 0 && 1 / value < 0) {
69
- return "-0";
70
- }
71
- if (typeof value === "object" && value !== null) {
72
- if (typeof value.toString === "function" && value.toString !== Object.prototype.toString) {
73
- return value.toString();
74
- }
75
- return inspect(value, { depth: 0, colors: false });
76
- }
77
- return String(value);
78
- }
79
- case "%d": {
80
- const value = args[i++];
81
- if (typeof value === "bigint") {
82
- return `${value.toString()}n`;
83
- }
84
- return Number(value).toString();
85
- }
86
- case "%i": {
87
- const value = args[i++];
88
- if (typeof value === "bigint") {
89
- return `${value.toString()}n`;
90
- }
91
- return Number.parseInt(String(value)).toString();
92
- }
93
- case "%f":
94
- return Number.parseFloat(String(args[i++])).toString();
95
- case "%o":
96
- return inspect(args[i++], { showHidden: true, showProxy: true });
97
- case "%O":
98
- return inspect(args[i++]);
99
- case "%c": {
100
- i++;
101
- return "";
102
- }
103
- case "%j":
104
- try {
105
- return JSON.stringify(args[i++]);
106
- } catch (err) {
107
- const m = err.message;
108
- if (
109
- // chromium
110
- m.includes("circular structure") || m.includes("cyclic structures") || m.includes("cyclic object")
111
- ) {
112
- return "[Circular]";
113
- }
114
- throw err;
115
- }
116
- default:
117
- return x;
118
- }
119
- });
120
- for (let x = args[i]; i < len; x = args[++i]) {
121
- if (x === null || typeof x !== "object") {
122
- str += ` ${x}`;
123
- } else {
124
- str += ` ${inspect(x)}`;
125
- }
126
- }
127
- return str;
39
+ if (typeof args[0] !== "string") {
40
+ const objects = [];
41
+ for (let i = 0; i < args.length; i++) {
42
+ objects.push(inspect(args[i], {
43
+ depth: 0,
44
+ colors: false
45
+ }));
46
+ }
47
+ return objects.join(" ");
48
+ }
49
+ const len = args.length;
50
+ let i = 1;
51
+ const template = args[0];
52
+ let str = String(template).replace(formatRegExp, (x) => {
53
+ if (x === "%%") {
54
+ return "%";
55
+ }
56
+ if (i >= len) {
57
+ return x;
58
+ }
59
+ switch (x) {
60
+ case "%s": {
61
+ const value = args[i++];
62
+ if (typeof value === "bigint") {
63
+ return `${value.toString()}n`;
64
+ }
65
+ if (typeof value === "number" && value === 0 && 1 / value < 0) {
66
+ return "-0";
67
+ }
68
+ if (typeof value === "object" && value !== null) {
69
+ if (typeof value.toString === "function" && value.toString !== Object.prototype.toString) {
70
+ return value.toString();
71
+ }
72
+ return inspect(value, {
73
+ depth: 0,
74
+ colors: false
75
+ });
76
+ }
77
+ return String(value);
78
+ }
79
+ case "%d": {
80
+ const value = args[i++];
81
+ if (typeof value === "bigint") {
82
+ return `${value.toString()}n`;
83
+ }
84
+ return Number(value).toString();
85
+ }
86
+ case "%i": {
87
+ const value = args[i++];
88
+ if (typeof value === "bigint") {
89
+ return `${value.toString()}n`;
90
+ }
91
+ return Number.parseInt(String(value)).toString();
92
+ }
93
+ case "%f": return Number.parseFloat(String(args[i++])).toString();
94
+ case "%o": return inspect(args[i++], {
95
+ showHidden: true,
96
+ showProxy: true
97
+ });
98
+ case "%O": return inspect(args[i++]);
99
+ case "%c": {
100
+ i++;
101
+ return "";
102
+ }
103
+ case "%j": try {
104
+ return JSON.stringify(args[i++]);
105
+ } catch (err) {
106
+ const m = err.message;
107
+ if (m.includes("circular structure") || m.includes("cyclic structures") || m.includes("cyclic object")) {
108
+ return "[Circular]";
109
+ }
110
+ throw err;
111
+ }
112
+ default: return x;
113
+ }
114
+ });
115
+ for (let x = args[i]; i < len; x = args[++i]) {
116
+ if (x === null || typeof x !== "object") {
117
+ str += ` ${x}`;
118
+ } else {
119
+ str += ` ${inspect(x)}`;
120
+ }
121
+ }
122
+ return str;
128
123
  }
129
124
  function inspect(obj, options = {}) {
130
- if (options.truncate === 0) {
131
- options.truncate = Number.POSITIVE_INFINITY;
132
- }
133
- return loupe.inspect(obj, options);
125
+ if (options.truncate === 0) {
126
+ options.truncate = Number.POSITIVE_INFINITY;
127
+ }
128
+ return loupe.inspect(obj, options);
134
129
  }
135
130
  function objDisplay(obj, options = {}) {
136
- if (typeof options.truncate === "undefined") {
137
- options.truncate = 40;
138
- }
139
- const str = inspect(obj, options);
140
- const type = Object.prototype.toString.call(obj);
141
- if (options.truncate && str.length >= options.truncate) {
142
- if (type === "[object Function]") {
143
- const fn = obj;
144
- return !fn.name ? "[Function]" : `[Function: ${fn.name}]`;
145
- } else if (type === "[object Array]") {
146
- return `[ Array(${obj.length}) ]`;
147
- } else if (type === "[object Object]") {
148
- const keys = Object.keys(obj);
149
- const kstr = keys.length > 2 ? `${keys.splice(0, 2).join(", ")}, ...` : keys.join(", ");
150
- return `{ Object (${kstr}) }`;
151
- } else {
152
- return str;
153
- }
154
- }
155
- return str;
131
+ if (typeof options.truncate === "undefined") {
132
+ options.truncate = 40;
133
+ }
134
+ const str = inspect(obj, options);
135
+ const type = Object.prototype.toString.call(obj);
136
+ if (options.truncate && str.length >= options.truncate) {
137
+ if (type === "[object Function]") {
138
+ const fn = obj;
139
+ return !fn.name ? "[Function]" : `[Function: ${fn.name}]`;
140
+ } else if (type === "[object Array]") {
141
+ return `[ Array(${obj.length}) ]`;
142
+ } else if (type === "[object Object]") {
143
+ const keys = Object.keys(obj);
144
+ const kstr = keys.length > 2 ? `${keys.splice(0, 2).join(", ")}, ...` : keys.join(", ");
145
+ return `{ Object (${kstr}) }`;
146
+ } else {
147
+ return str;
148
+ }
149
+ }
150
+ return str;
156
151
  }
157
152
 
158
153
  function getDefaultExportFromCjs (x) {