| 1 |
import browserslist from "browserslist"; |
| 2 |
import { join, dirname } from "path"; |
| 3 |
import { fileURLToPath } from "url"; |
| 4 |
|
| 5 |
import _getTargets from "../lib/index.js"; |
| 6 |
const getTargets = _getTargets.default || _getTargets; |
| 7 |
|
| 8 |
describe("getTargets", () => { |
| 9 |
it("parses", () => { |
| 10 |
expect( |
| 11 |
getTargets({ |
| 12 |
chrome: 49, |
| 13 |
firefox: "55", |
| 14 |
ie: "9", |
| 15 |
node: "6.10", |
| 16 |
electron: "1.6", |
| 17 |
}), |
| 18 |
).toEqual({ |
| 19 |
chrome: "49.0.0", |
| 20 |
electron: "1.6.0", |
| 21 |
firefox: "55.0.0", |
| 22 |
ie: "9.0.0", |
| 23 |
node: "6.10.0", |
| 24 |
}); |
| 25 |
}); |
| 26 |
|
| 27 |
it("does not mutate the input", () => { |
| 28 |
const input = Object.freeze({ browsers: "defaults", esmodules: true }); |
| 29 |
const expected = getTargets({ |
| 30 |
browsers: browserslist.defaults, |
| 31 |
esmodules: true, |
| 32 |
}); |
| 33 |
const actual = getTargets(input); |
| 34 |
expect(actual).toEqual(expected); |
| 35 |
expect(input.browsers).toEqual("defaults"); |
| 36 |
expect(input.esmodules).toEqual(true); |
| 37 |
}); |
| 38 |
|
| 39 |
it("allows 'defaults' query", () => { |
| 40 |
const browserslistDefaults = browserslist.defaults; |
| 41 |
|
| 42 |
const expected = getTargets({ browsers: browserslistDefaults }); |
| 43 |
const actual = getTargets({ browsers: "defaults" }); |
| 44 |
|
| 45 |
expect(actual).toEqual(expected); |
| 46 |
}); |
| 47 |
|
| 48 |
it("does not clobber browserslists defaults", () => { |
| 49 |
const browserslistDefaults = browserslist.defaults; |
| 50 |
|
| 51 |
getTargets({ |
| 52 |
browsers: "chrome 56, ie 11, firefox 51, safari 9", |
| 53 |
}); |
| 54 |
|
| 55 |
expect(browserslist.defaults).toEqual(browserslistDefaults); |
| 56 |
}); |
| 57 |
|
| 58 |
it("supports region browserslists query", () => { |
| 59 |
const actual = getTargets({ browsers: "> 0.5% in GB" }); |
| 60 |
|
| 61 |
|
| 62 |
expect(parseFloat(actual.chrome)).toBeGreaterThan(4); |
| 63 |
}); |
| 64 |
|
| 65 |
describe("validation", () => { |
| 66 |
it("throws on invalid target name", () => { |
| 67 |
const invalidTargetName = () => { |
| 68 |
getTargets({ |
| 69 |
unknown: "unknown", |
| 70 |
}); |
| 71 |
}; |
| 72 |
expect(invalidTargetName).toThrow(); |
| 73 |
}); |
| 74 |
|
| 75 |
it("throws on invalid browsers target", () => { |
| 76 |
const invalidBrowsersTarget = () => { |
| 77 |
getTargets({ |
| 78 |
browsers: 59, |
| 79 |
}); |
| 80 |
}; |
| 81 |
expect(invalidBrowsersTarget).toThrow(); |
| 82 |
}); |
| 83 |
|
| 84 |
it("throws on invalid target version", () => { |
| 85 |
const invalidTargetVersion = () => { |
| 86 |
getTargets({ |
| 87 |
chrome: "unknown", |
| 88 |
}); |
| 89 |
}; |
| 90 |
expect(invalidTargetVersion).toThrow(); |
| 91 |
}); |
| 92 |
}); |
| 93 |
|
| 94 |
describe("browser", () => { |
| 95 |
it("merges browser key targets", () => { |
| 96 |
expect( |
| 97 |
getTargets({ |
| 98 |
browsers: "chrome 56, ie 11, firefox 51, safari 9", |
| 99 |
chrome: "49", |
| 100 |
firefox: "55", |
| 101 |
ie: "9", |
| 102 |
}), |
| 103 |
).toEqual({ |
| 104 |
chrome: "49.0.0", |
| 105 |
firefox: "55.0.0", |
| 106 |
ie: "9.0.0", |
| 107 |
safari: "9.0.0", |
| 108 |
}); |
| 109 |
}); |
| 110 |
|
| 111 |
it("works with TP versions", () => { |
| 112 |
expect( |
| 113 |
getTargets({ |
| 114 |
browsers: "safari tp", |
| 115 |
}), |
| 116 |
).toEqual({ |
| 117 |
safari: "tp", |
| 118 |
}); |
| 119 |
}); |
| 120 |
|
| 121 |
it("works with node versions", () => { |
| 122 |
expect( |
| 123 |
getTargets({ |
| 124 |
browsers: "node 8.5", |
| 125 |
}), |
| 126 |
).toEqual({ |
| 127 |
node: "8.5.0", |
| 128 |
}); |
| 129 |
}); |
| 130 |
|
| 131 |
it("works with current node version and string type browsers", () => { |
| 132 |
expect( |
| 133 |
getTargets({ |
| 134 |
browsers: "current node, chrome 55, opera 42", |
| 135 |
}), |
| 136 |
).toEqual({ |
| 137 |
node: process.versions.node, |
| 138 |
chrome: "55.0.0", |
| 139 |
opera: "42.0.0", |
| 140 |
}); |
| 141 |
}); |
| 142 |
|
| 143 |
it("does throws on unsupported versions", () => { |
| 144 |
expect(() => { |
| 145 |
getTargets({ |
| 146 |
browsers: "node 15.0.0, chrome 1000", |
| 147 |
}); |
| 148 |
}).toThrow(); |
| 149 |
}); |
| 150 |
|
| 151 |
it("works with current node version and array type browsers", () => { |
| 152 |
expect( |
| 153 |
getTargets({ |
| 154 |
browsers: ["ie 11", "current node", "chrome 55"], |
| 155 |
}), |
| 156 |
).toEqual({ |
| 157 |
node: process.versions.node, |
| 158 |
chrome: "55.0.0", |
| 159 |
ie: "11.0.0", |
| 160 |
}); |
| 161 |
}); |
| 162 |
|
| 163 |
it("prefers released version over TP", () => { |
| 164 |
expect( |
| 165 |
getTargets({ |
| 166 |
browsers: "safari tp, safari 11", |
| 167 |
}), |
| 168 |
).toEqual({ |
| 169 |
safari: "11.0.0", |
| 170 |
}); |
| 171 |
}); |
| 172 |
|
| 173 |
it("returns TP version in lower case", () => { |
| 174 |
expect( |
| 175 |
getTargets({ |
| 176 |
safari: "TP", |
| 177 |
}), |
| 178 |
).toEqual({ |
| 179 |
safari: "tp", |
| 180 |
}); |
| 181 |
}); |
| 182 |
|
| 183 |
it("works with android", () => { |
| 184 |
expect( |
| 185 |
getTargets({ |
| 186 |
browsers: "Android 4", |
| 187 |
}), |
| 188 |
).toEqual({ |
| 189 |
android: "4.0.0", |
| 190 |
}); |
| 191 |
}); |
| 192 |
|
| 193 |
it("works with inequalities", () => { |
| 194 |
expect( |
| 195 |
getTargets({ |
| 196 |
browsers: "Android >= 4", |
| 197 |
}), |
| 198 |
).toEqual({ |
| 199 |
android: "4.0.0", |
| 200 |
}); |
| 201 |
}); |
| 202 |
}); |
| 203 |
|
| 204 |
describe("esmodules", () => { |
| 205 |
it("returns browsers supporting modules", () => { |
| 206 |
expect( |
| 207 |
getTargets({ |
| 208 |
esmodules: true, |
| 209 |
}), |
| 210 |
).toMatchSnapshot(); |
| 211 |
}); |
| 212 |
|
| 213 |
it("returns browsers supporting modules, ignoring browsers key", () => { |
| 214 |
expect( |
| 215 |
getTargets({ |
| 216 |
esmodules: true, |
| 217 |
browsers: "ie 8", |
| 218 |
}), |
| 219 |
).toMatchSnapshot(); |
| 220 |
}); |
| 221 |
|
| 222 |
it("returns browser supporting modules and keyed browser overrides", () => { |
| 223 |
expect( |
| 224 |
getTargets({ |
| 225 |
esmodules: true, |
| 226 |
ie: 11, |
| 227 |
}), |
| 228 |
).toMatchSnapshot(); |
| 229 |
}); |
| 230 |
|
| 231 |
it("returns browser supporting modules and keyed browser overrides, ignoring browsers field", () => { |
| 232 |
expect( |
| 233 |
getTargets({ |
| 234 |
esmodules: true, |
| 235 |
browsers: "ie 10", |
| 236 |
ie: 11, |
| 237 |
}), |
| 238 |
).toMatchSnapshot(); |
| 239 |
}); |
| 240 |
|
| 241 |
it("can be intersected with the browsers option", () => { |
| 242 |
expect( |
| 243 |
getTargets({ |
| 244 |
esmodules: "intersect", |
| 245 |
browsers: ["chrome >= 70", "firefox >= 30"], |
| 246 |
}), |
| 247 |
).toMatchSnapshot(); |
| 248 |
}); |
| 249 |
|
| 250 |
it("can be intersected with ios browsers option", () => { |
| 251 |
expect( |
| 252 |
getTargets({ |
| 253 |
esmodules: "intersect", |
| 254 |
browsers: ["ios >= 12"], |
| 255 |
}), |
| 256 |
).toMatchSnapshot(); |
| 257 |
}); |
| 258 |
|
| 259 |
it("can be intersected with a .browserslistrc file", () => { |
| 260 |
expect( |
| 261 |
getTargets( |
| 262 |
{ |
| 263 |
esmodules: "intersect", |
| 264 |
}, |
| 265 |
{ |
| 266 |
configPath: join( |
| 267 |
dirname(fileURLToPath(import.meta.url)), |
| 268 |
"fixtures", |
| 269 |
"foo.js", |
| 270 |
), |
| 271 |
}, |
| 272 |
), |
| 273 |
).toMatchSnapshot(); |
| 274 |
}); |
| 275 |
|
| 276 |
it("explicit browser versions have the precedence over 'esmodules'", () => { |
| 277 |
expect( |
| 278 |
getTargets({ |
| 279 |
browsers: "chrome 5, firefox 5", |
| 280 |
esmodules: "intersect", |
| 281 |
chrome: 20, |
| 282 |
firefox: 70, |
| 283 |
}), |
| 284 |
).toMatchSnapshot(); |
| 285 |
}); |
| 286 |
|
| 287 |
(process.env.BABEL_8_BREAKING ? it.skip : it)( |
| 288 |
"'intersect' behaves like 'true' if no browsers are specified - Babel 7", |
| 289 |
() => { |
| 290 |
expect(getTargets({ esmodules: "intersect" })).toEqual( |
| 291 |
getTargets({ esmodules: true }, { ignoreBrowserslistConfig: true }), |
| 292 |
); |
| 293 |
}, |
| 294 |
); |
| 295 |
|
| 296 |
(process.env.BABEL_8_BREAKING ? it.skip : it)( |
| 297 |
"'browsers' option will have no effect if it is an empty array - Babel 7", |
| 298 |
() => { |
| 299 |
expect(getTargets({ esmodules: "intersect", browsers: [] })).toEqual( |
| 300 |
getTargets({ esmodules: "intersect" }), |
| 301 |
); |
| 302 |
}, |
| 303 |
); |
| 304 |
|
| 305 |
it("The final 'browsers' handled variable will have no effect if it is an empty array", () => { |
| 306 |
expect(getTargets({ esmodules: "intersect", browsers: [] })).toEqual( |
| 307 |
getTargets( |
| 308 |
{ esmodules: "intersect" }, |
| 309 |
{ ignoreBrowserslistConfig: true }, |
| 310 |
), |
| 311 |
); |
| 312 |
}); |
| 313 |
|
| 314 |
it("'resolveTargets' will be called rightly if 'browsers' is an array with some value", () => { |
| 315 |
|
| 316 |
expect(() => |
| 317 |
getTargets({ esmodules: "intersect", browsers: ["test"] }), |
| 318 |
).toThrow(); |
| 319 |
}); |
| 320 |
|
| 321 |
(process.env.BABEL_8_BREAKING ? it : it.skip)( |
| 322 |
"'intersect' behaves like no-op if no browsers are specified", |
| 323 |
() => { |
| 324 |
expect(getTargets({ esmodules: "intersect" })).toEqual(getTargets({})); |
| 325 |
}, |
| 326 |
); |
| 327 |
|
| 328 |
it("'intersect' behaves like 'true' if no browsers are specified and the browserslist config is ignored", () => { |
| 329 |
expect( |
| 330 |
getTargets( |
| 331 |
{ esmodules: "intersect" }, |
| 332 |
{ ignoreBrowserslistConfig: true }, |
| 333 |
), |
| 334 |
).toEqual( |
| 335 |
getTargets({ esmodules: true }, { ignoreBrowserslistConfig: true }), |
| 336 |
); |
| 337 |
}); |
| 338 |
}); |
| 339 |
|
| 340 |
describe("node", () => { |
| 341 |
it("should return the current node version with option 'current'", () => { |
| 342 |
expect( |
| 343 |
getTargets({ |
| 344 |
node: true, |
| 345 |
}), |
| 346 |
).toEqual({ |
| 347 |
node: process.versions.node, |
| 348 |
}); |
| 349 |
}); |
| 350 |
}); |
| 351 |
|
| 352 |
describe("electron", () => { |
| 353 |
it("should be its own target", () => { |
| 354 |
expect( |
| 355 |
getTargets({ |
| 356 |
chrome: "46", |
| 357 |
electron: "0.34", |
| 358 |
}), |
| 359 |
).toEqual({ |
| 360 |
chrome: "46.0.0", |
| 361 |
electron: "0.34.0", |
| 362 |
}); |
| 363 |
}); |
| 364 |
}); |
| 365 |
|
| 366 |
describe("exception", () => { |
| 367 |
it("throws when version is not a semver", () => { |
| 368 |
expect(() => |
| 369 |
getTargets({ chrome: "seventy-two" }), |
| 370 |
).toThrowErrorMatchingSnapshot(); |
| 371 |
}); |
| 372 |
}); |
| 373 |
}); |