forked from babel/babel
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
targets.js
166 lines (138 loc) · 4.74 KB
/
targets.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
import { loadOptions as loadOptionsOrig } from "../lib";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const cwd = dirname(fileURLToPath(import.meta.url));
function loadOptions(opts) {
return loadOptionsOrig({ cwd, ...opts });
}
function withTargets(targets) {
return loadOptions({ targets });
}
describe("targets", () => {
it("throws if invalid type", () => {
expect(() => withTargets(2)).toThrow(
".targets must be a string, an array of strings or an object",
);
expect(() => withTargets([2])).toThrow(
".targets must be a string, an array of strings or an object",
);
expect(() => withTargets([{}])).toThrow(
".targets must be a string, an array of strings or an object",
);
expect(() => withTargets([])).not.toThrow();
expect(() => withTargets({})).not.toThrow();
});
it("throws if invalid target", () => {
expect(() => withTargets({ uglify: "2.3" })).toThrow(
/\.targets\["uglify"\] is not a valid target/,
);
expect(() => withTargets({ foo: "bar" })).toThrow(
/\.targets\["foo"\] is not a valid target/,
);
expect(() => withTargets({ firefox: 71 })).not.toThrow();
});
it("throws if invalid version", () => {
expect(() => withTargets({ node: 10.1 /* or 10.10? */ })).toThrow(
`.targets["node"] must be a string or an integer number`,
);
expect(() => withTargets({ node: true })).toThrow(
`.targets["node"] must be a string or an integer number`,
);
expect(() => withTargets({ node: "10.1" })).not.toThrow();
expect(() => withTargets({ node: "current" })).not.toThrow();
});
it("esmodules", () => {
expect(() => withTargets({ esmodules: "7" })).toThrow(
`.targets["esmodules"] must be a boolean, or undefined`,
);
expect(() => withTargets({ esmodules: false })).not.toThrow();
expect(() => withTargets({ esmodules: true })).not.toThrow();
});
it("browsers", () => {
expect(() => withTargets({ browsers: 2 })).toThrow(
`.targets["browsers"] must be undefined, a string or an array of strings`,
);
expect(() => withTargets({ browsers: [2] })).toThrow(
`.targets["browsers"] must be undefined, a string or an array of strings`,
);
expect(() => withTargets({ browsers: {} })).toThrow(
`.targets["browsers"] must be undefined, a string or an array of strings`,
);
expect(() => withTargets({ browsers: [] })).not.toThrow();
});
});
describe("browserslist", () => {
it("loads .browserslistrc by default", () => {
expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
}).targets,
).toEqual({ chrome: "80.0.0" });
});
it("loads .browserslistrc relative to the root", () => {
expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
filename: "./node_modules/dep/test.js",
}).targets,
).toEqual({ chrome: "80.0.0" });
});
// TODO: browserslistConfig is currently resolved starting from the root
// rather than from the config file.
// eslint-disable-next-line jest/no-disabled-tests
it.skip("loads nested .browserslistrc files if explicitly specified", () => {
expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
filename: "./node_modules/dep/test.js",
babelrcRoots: ["./node_modules/dep/"],
}).targets,
).toEqual({ edge: "14.0.0" });
});
describe("browserslistConfigFile", () => {
it("can disable config loading", () => {
expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
browserslistConfigFile: false,
}).targets,
).toEqual({});
});
it("can specify a custom file", () => {
expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
browserslistConfigFile: "./.browserslistrc-firefox",
}).targets,
).toEqual({ firefox: "74.0.0" });
});
it("is relative to the project root", () => {
expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
root: "..",
filename: "./nested/test.js",
browserslistConfigFile: "./targets/.browserslistrc-firefox",
}).targets,
).toEqual({ firefox: "74.0.0" });
});
});
describe("browserslistEnv", () => {
it("is forwarded to browserslist", () => {
expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
browserslistEnv: "browserslist-loading-test",
}).targets,
).toEqual({ chrome: "70.0.0" });
});
});
it("esmodules and browsers are intersected", () => {
expect(
withTargets({
esmodules: true,
browsers: "chrome >= 80, firefox >= 30",
}).targets,
).toEqual({ chrome: "80.0.0", firefox: "60.0.0" });
});
});