/
targets-parser.spec.js
348 lines (311 loc) 路 8.2 KB
/
targets-parser.spec.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import browserslist from "browserslist";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import _getTargets from "../lib/index.js";
const getTargets = _getTargets.default;
describe("getTargets", () => {
it("parses", () => {
expect(
getTargets({
chrome: 49,
firefox: "55",
ie: "9",
node: "6.10",
electron: "1.6",
}),
).toEqual({
chrome: "49.0.0",
electron: "1.6.0",
firefox: "55.0.0",
ie: "9.0.0",
node: "6.10.0",
});
});
it("does not mutate the input", () => {
const input = Object.freeze({ browsers: "defaults", esmodules: true });
const expected = getTargets({
browsers: browserslist.defaults,
esmodules: true,
});
const actual = getTargets(input);
expect(actual).toEqual(expected);
expect(input.browsers).toEqual("defaults");
expect(input.esmodules).toEqual(true);
});
it("allows 'defaults' query", () => {
const browserslistDefaults = browserslist.defaults;
const expected = getTargets({ browsers: browserslistDefaults });
const actual = getTargets({ browsers: "defaults" });
expect(actual).toEqual(expected);
});
it("does not clobber browserslists defaults", () => {
const browserslistDefaults = browserslist.defaults;
getTargets({
browsers: "chrome 56, ie 11, firefox 51, safari 9",
});
expect(browserslist.defaults).toEqual(browserslistDefaults);
});
it("supports region browserslists query", () => {
const actual = getTargets({ browsers: "> 0.5% in GB" });
// chrome 4 is the first release of chrome,
// it should never be included in this query
expect(parseFloat(actual.chrome)).toBeGreaterThan(4);
});
describe("validation", () => {
it("throws on invalid target name", () => {
const invalidTargetName = () => {
getTargets({
unknown: "unknown",
});
};
expect(invalidTargetName).toThrow();
});
it("throws on invalid browsers target", () => {
const invalidBrowsersTarget = () => {
getTargets({
browsers: 59,
});
};
expect(invalidBrowsersTarget).toThrow();
});
it("throws on invalid target version", () => {
const invalidTargetVersion = () => {
getTargets({
chrome: "unknown",
});
};
expect(invalidTargetVersion).toThrow();
});
});
describe("browser", () => {
it("merges browser key targets", () => {
expect(
getTargets({
browsers: "chrome 56, ie 11, firefox 51, safari 9",
chrome: "49",
firefox: "55",
ie: "9",
}),
).toEqual({
chrome: "49.0.0",
firefox: "55.0.0",
ie: "9.0.0",
safari: "9.0.0",
});
});
it("works with TP versions", () => {
expect(
getTargets({
browsers: "safari tp",
}),
).toEqual({
safari: "tp",
});
});
it("works with node versions", () => {
expect(
getTargets({
browsers: "node 8.5",
}),
).toEqual({
node: "8.5.0",
});
});
it("works with current node version and string type browsers", () => {
expect(
getTargets({
browsers: "current node, chrome 55, opera 42",
}),
).toEqual({
node: process.versions.node,
chrome: "55.0.0",
opera: "42.0.0",
});
});
it("does throws on unsupported versions", () => {
expect(() => {
getTargets({
browsers: "node 15.0.0, chrome 1000",
});
}).toThrow();
});
it("works with current node version and array type browsers", () => {
expect(
getTargets({
browsers: ["ie 11", "current node", "chrome 55"],
}),
).toEqual({
node: process.versions.node,
chrome: "55.0.0",
ie: "11.0.0",
});
});
it("prefers released version over TP", () => {
expect(
getTargets({
browsers: "safari tp, safari 11",
}),
).toEqual({
safari: "11.0.0",
});
});
it("returns TP version in lower case", () => {
expect(
getTargets({
safari: "TP",
}),
).toEqual({
safari: "tp",
});
});
it("works with android", () => {
expect(
getTargets({
browsers: "Android 4",
}),
).toEqual({
android: "4.0.0",
});
});
it("works with inequalities", () => {
expect(
getTargets({
browsers: "Android >= 4",
}),
).toEqual({
android: "4.0.0",
});
});
});
describe("esmodules", () => {
it("returns browsers supporting modules", () => {
expect(
getTargets({
esmodules: true,
}),
).toMatchSnapshot();
});
it("returns browsers supporting modules, ignoring browsers key", () => {
expect(
getTargets({
esmodules: true,
browsers: "ie 8",
}),
).toMatchSnapshot();
});
it("returns browser supporting modules and keyed browser overrides", () => {
expect(
getTargets({
esmodules: true,
ie: 11,
}),
).toMatchSnapshot();
});
it("returns browser supporting modules and keyed browser overrides, ignoring browsers field", () => {
expect(
getTargets({
esmodules: true,
browsers: "ie 10",
ie: 11,
}),
).toMatchSnapshot();
});
it("can be intersected with the browsers option", () => {
expect(
getTargets({
esmodules: "intersect",
browsers: ["chrome >= 70", "firefox >= 30"],
}),
).toMatchSnapshot();
});
it("can be intersected with ios browsers option", () => {
expect(
getTargets({
esmodules: "intersect",
browsers: ["ios >= 12"],
}),
).toMatchSnapshot();
});
it("can be intersected with a .browserslistrc file", () => {
expect(
getTargets(
{
esmodules: "intersect",
},
{
configPath: join(
dirname(fileURLToPath(import.meta.url)),
"fixtures",
"foo.js",
),
},
),
).toMatchSnapshot();
});
it("explicit browser versions have the precedence over 'esmodules'", () => {
expect(
getTargets({
browsers: "chrome 5, firefox 5",
esmodules: "intersect",
chrome: 20,
firefox: 70,
}),
).toMatchSnapshot();
});
(process.env.BABEL_8_BREAKING ? it.skip : it)(
"'intersect' behaves like 'true' if no browsers are specified - Babel 7",
() => {
expect(getTargets({ esmodules: "intersect" })).toEqual(
getTargets({ esmodules: true }, { ignoreBrowserslistConfig: true }),
);
},
);
(process.env.BABEL_8_BREAKING ? it : it.skip)(
"'intersect' behaves like no-op if no browsers are specified",
() => {
expect(getTargets({ esmodules: "intersect" })).toEqual(getTargets({}));
},
);
it("'intersect' behaves like 'true' if no browsers are specified and the browserslist config is ignored", () => {
expect(
getTargets(
{ esmodules: "intersect" },
{ ignoreBrowserslistConfig: true },
),
).toEqual(
getTargets({ esmodules: true }, { ignoreBrowserslistConfig: true }),
);
});
});
describe("node", () => {
it("should return the current node version with option 'current'", () => {
expect(
getTargets({
node: true,
}),
).toEqual({
node: process.versions.node,
});
});
});
describe("electron", () => {
it("should be its own target", () => {
expect(
getTargets({
chrome: "46",
electron: "0.34",
}),
).toEqual({
chrome: "46.0.0",
electron: "0.34.0",
});
});
});
describe("exception", () => {
it("throws when version is not a semver", () => {
expect(() =>
getTargets({ chrome: "seventy-two" }),
).toThrowErrorMatchingSnapshot();
});
});
});