forked from smooth80/webpack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckArrayExpectation.js
147 lines (143 loc) · 3.48 KB
/
checkArrayExpectation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
const fs = require("graceful-fs");
const path = require("path");
const check = (expected, actual) => {
if (expected instanceof RegExp) {
expected = { message: expected };
}
if (Array.isArray(expected)) {
return expected.every(e => check(e, actual));
}
return Object.keys(expected).every(key => {
let value = actual[key];
if (typeof value === "object") {
value = JSON.stringify(value);
}
return expected[key].test(value);
});
};
const explain = object => {
if (object instanceof RegExp) {
object = { message: object };
}
return Object.keys(object)
.map(key => {
let value = object[key];
if (typeof value === "object" && !(value instanceof RegExp)) {
value = JSON.stringify(value);
}
let msg = `${key} = ${value}`;
if (key !== "stack" && key !== "details" && msg.length > 100)
msg = msg.slice(0, 97) + "...";
return msg;
})
.join("; ");
};
const diffItems = (actual, expected, kind) => {
const tooMuch = actual.slice();
const missing = expected.slice();
for (let i = 0; i < missing.length; i++) {
const current = missing[i];
for (let j = 0; j < tooMuch.length; j++) {
if (check(current, tooMuch[j])) {
tooMuch.splice(j, 1);
missing.splice(i, 1);
i--;
break;
}
}
}
const diff = [];
if (missing.length > 0) {
diff.push(`The following expected ${kind}s are missing:
${missing.map(item => `${item}`).join("\n\n")}`);
}
if (tooMuch.length > 0) {
diff.push(`The following ${kind}s are unexpected:
${tooMuch.map(item => `${explain(item)}`).join("\n\n")}`);
}
return diff.join("\n\n");
};
module.exports = function checkArrayExpectation(
testDirectory,
object,
kind,
filename,
upperCaseKind,
done
) {
if (!done) {
done = upperCaseKind;
upperCaseKind = filename;
filename = `${kind}s`;
}
let array = object[`${kind}s`];
if (Array.isArray(array)) {
if (kind === "warning") {
array = array.filter(item => !/from Terser/.test(item));
}
}
if (fs.existsSync(path.join(testDirectory, `${filename}.js`))) {
const expectedFilename = path.join(testDirectory, `${filename}.js`);
const expected = require(expectedFilename);
const diff = diffItems(array, expected, kind);
if (expected.length < array.length) {
return (
done(
new Error(
`More ${kind}s (${array.length} instead of ${expected.length}) while compiling than expected:\n\n${diff}\n\nCheck expected ${kind}s: ${expectedFilename}`
)
),
true
);
} else if (expected.length > array.length) {
return (
done(
new Error(
`Less ${kind}s (${array.length} instead of ${expected.length}) while compiling than expected:\n\n${diff}\n\nCheck expected ${kind}s: ${expectedFilename}`
)
),
true
);
}
for (let i = 0; i < array.length; i++) {
if (Array.isArray(expected[i])) {
for (let j = 0; j < expected[i].length; j++) {
if (!check(expected[i][j], array[i])) {
return (
done(
new Error(
`${upperCaseKind} ${i}: ${explain(
array[i]
)} doesn't match ${explain(expected[i][j])}`
)
),
true
);
}
}
} else if (!check(expected[i], array[i]))
return (
done(
new Error(
`${upperCaseKind} ${i}: ${explain(
array[i]
)} doesn't match ${explain(expected[i])}`
)
),
true
);
}
} else if (array.length > 0) {
return (
done(
new Error(
`${upperCaseKind}s while compiling:\n\n${array
.map(explain)
.join("\n\n")}`
)
),
true
);
}
};