| 1 |
import { |
| 2 |
loadOptionsSync, |
| 3 |
loadOptionsAsync, |
| 4 |
loadPartialConfigSync, |
| 5 |
createConfigItemSync, |
| 6 |
} from "../lib/index.js"; |
| 7 |
import path from "path"; |
| 8 |
import { fileURLToPath } from "url"; |
| 9 |
import { createRequire } from "module"; |
| 10 |
|
| 11 |
const require = createRequire(import.meta.url); |
| 12 |
|
| 13 |
describe("@babel/core config loading", () => { |
| 14 |
const FILEPATH = path.join( |
| 15 |
path.dirname(fileURLToPath(import.meta.url)), |
| 16 |
"fixtures", |
| 17 |
"config-loading", |
| 18 |
"folder", |
| 19 |
"example.js", |
| 20 |
); |
| 21 |
|
| 22 |
afterEach(() => { |
| 23 |
delete process.env.INVALIDATE_BABELRC; |
| 24 |
delete process.env.INVALIDATE_PRESET1; |
| 25 |
delete process.env.INVALIDATE_PRESET2; |
| 26 |
delete process.env.INVALIDATE_PRESET3; |
| 27 |
delete process.env.INVALIDATE_PLUGIN1; |
| 28 |
delete process.env.INVALIDATE_PLUGIN2; |
| 29 |
delete process.env.INVALIDATE_PLUGIN3; |
| 30 |
delete process.env.INVALIDATE_PLUGIN4; |
| 31 |
delete process.env.INVALIDATE_PLUGIN5; |
| 32 |
delete process.env.INVALIDATE_PLUGIN6; |
| 33 |
}); |
| 34 |
|
| 35 |
function makeOpts(skipProgrammatic = false) { |
| 36 |
return { |
| 37 |
cwd: path.dirname(FILEPATH), |
| 38 |
filename: FILEPATH, |
| 39 |
presets: skipProgrammatic |
| 40 |
? null |
| 41 |
: [[require("./fixtures/config-loading/preset3.js"), {}]], |
| 42 |
plugins: skipProgrammatic |
| 43 |
? null |
| 44 |
: [[require("./fixtures/config-loading/plugin6.js"), {}]], |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
describe("createConfigItemSync", () => { |
| 49 |
// Windows uses different file paths |
| 50 |
const noWin = process.platform === "win32" ? it.skip : it; |
| 51 |
|
| 52 |
noWin("can be called synchronously with one param", () => { |
| 53 |
function myPlugin() { |
| 54 |
return {}; |
| 55 |
} |
| 56 |
|
| 57 |
expect(createConfigItemSync(myPlugin)).toEqual({ |
| 58 |
dirname: process.cwd(), |
| 59 |
file: undefined, |
| 60 |
name: undefined, |
| 61 |
options: undefined, |
| 62 |
value: myPlugin, |
| 63 |
}); |
| 64 |
}); |
| 65 |
|
| 66 |
noWin("can be called synchronously with two params", () => { |
| 67 |
function myPlugin() { |
| 68 |
return {}; |
| 69 |
} |
| 70 |
|
| 71 |
expect( |
| 72 |
createConfigItemSync(myPlugin, { dirname: "/foo", type: "plugin" }), |
| 73 |
).toEqual({ |
| 74 |
dirname: "/foo", |
| 75 |
file: undefined, |
| 76 |
name: undefined, |
| 77 |
options: undefined, |
| 78 |
value: myPlugin, |
| 79 |
}); |
| 80 |
}); |
| 81 |
}); |
| 82 |
|
| 83 |
describe("loadPartialConfigSync", () => { |
| 84 |
it("should preserve disabled plugins in the partial config", () => { |
| 85 |
const plugin = function () { |
| 86 |
return {}; |
| 87 |
}; |
| 88 |
|
| 89 |
const opts = loadPartialConfigSync({ |
| 90 |
...makeOpts(true), |
| 91 |
babelrc: false, |
| 92 |
configFile: false, |
| 93 |
plugins: [[plugin, false]], |
| 94 |
}); |
| 95 |
|
| 96 |
expect(opts.options.plugins.length).toBe(1); |
| 97 |
const item = opts.options.plugins[0]; |
| 98 |
|
| 99 |
expect(item.value).toBe(plugin); |
| 100 |
expect(item.options).toBe(false); |
| 101 |
}); |
| 102 |
|
| 103 |
it("should preserve disabled presets in the partial config", () => { |
| 104 |
const preset = function () { |
| 105 |
return {}; |
| 106 |
}; |
| 107 |
|
| 108 |
const opts = loadPartialConfigSync({ |
| 109 |
...makeOpts(true), |
| 110 |
babelrc: false, |
| 111 |
configFile: false, |
| 112 |
presets: [[preset, false]], |
| 113 |
}); |
| 114 |
|
| 115 |
expect(opts.options.presets.length).toBe(1); |
| 116 |
const item = opts.options.presets[0]; |
| 117 |
|
| 118 |
expect(item.value).toBe(preset); |
| 119 |
expect(item.options).toBe(false); |
| 120 |
}); |
| 121 |
|
| 122 |
it("should always set 'rootMode' to 'root'", async () => { |
| 123 |
const cwd = path.join( |
| 124 |
path.dirname(fileURLToPath(import.meta.url)), |
| 125 |
"fixtures", |
| 126 |
"config-loading", |
| 127 |
"root", |
| 128 |
"nested", |
| 129 |
); |
| 130 |
|
| 131 |
const { options } = await loadPartialConfigSync({ |
| 132 |
cwd, |
| 133 |
filename: path.join(cwd, "file.js"), |
| 134 |
rootMode: "upward", |
| 135 |
}); |
| 136 |
|
| 137 |
expect(options.root).toBe(path.join(cwd, "..")); |
| 138 |
expect(options.rootMode).toBe("root"); |
| 139 |
}); |
| 140 |
}); |
| 141 |
|
| 142 |
describe("loadOptionsSync", () => { |
| 143 |
it("should load and cache the config with plugins and presets", () => { |
| 144 |
const opts = makeOpts(); |
| 145 |
|
| 146 |
const options1 = loadOptionsSync(opts); |
| 147 |
expect(options1.plugins.map(p => p.key)).toEqual([ |
| 148 |
"plugin1", |
| 149 |
"plugin2", |
| 150 |
"plugin6", |
| 151 |
"plugin5", |
| 152 |
"plugin4", |
| 153 |
"plugin3", |
| 154 |
]); |
| 155 |
|
| 156 |
const options2 = loadOptionsSync(opts); |
| 157 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 158 |
|
| 159 |
for (let i = 0; i < options2.plugins.length; i++) { |
| 160 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 161 |
} |
| 162 |
}); |
| 163 |
|
| 164 |
it("should load and cache the config for unique opts objects", () => { |
| 165 |
const options1 = loadOptionsSync(makeOpts(true)); |
| 166 |
expect(options1.plugins.map(p => p.key)).toEqual([ |
| 167 |
"plugin1", |
| 168 |
"plugin2", |
| 169 |
"plugin4", |
| 170 |
"plugin3", |
| 171 |
]); |
| 172 |
|
| 173 |
const options2 = loadOptionsSync(makeOpts(true)); |
| 174 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 175 |
|
| 176 |
for (let i = 0; i < options2.plugins.length; i++) { |
| 177 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 178 |
} |
| 179 |
}); |
| 180 |
|
| 181 |
it("should invalidate config file plugins", () => { |
| 182 |
const opts = makeOpts(); |
| 183 |
|
| 184 |
const options1 = loadOptionsSync(opts); |
| 185 |
|
| 186 |
process.env.INVALIDATE_PLUGIN1 = true; |
| 187 |
|
| 188 |
const options2 = loadOptionsSync(opts); |
| 189 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 190 |
|
| 191 |
expect(options2.plugins[0]).not.toBe(options1.plugins[0]); |
| 192 |
for (let i = 1; i < options1.plugins.length; i++) { |
| 193 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 194 |
} |
| 195 |
|
| 196 |
process.env.INVALIDATE_PLUGIN3 = true; |
| 197 |
|
| 198 |
const options3 = loadOptionsSync(opts); |
| 199 |
expect(options3.plugins.length).toBe(options1.plugins.length); |
| 200 |
expect(options3.plugins.length).toBe(6); |
| 201 |
expect(options3.plugins[0]).not.toBe(options1.plugins[0]); |
| 202 |
expect(options3.plugins[5]).not.toBe(options1.plugins[5]); |
| 203 |
for (let i = 1; i < 5; i++) { |
| 204 |
expect(options3.plugins[i]).toBe(options1.plugins[i]); |
| 205 |
} |
| 206 |
}); |
| 207 |
|
| 208 |
it("should invalidate config file presets and their children", () => { |
| 209 |
const opts = makeOpts(); |
| 210 |
|
| 211 |
const options1 = loadOptionsSync(opts); |
| 212 |
|
| 213 |
process.env.INVALIDATE_PRESET1 = true; |
| 214 |
|
| 215 |
const options2 = loadOptionsSync(opts); |
| 216 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 217 |
expect(options2.plugins.length).toBe(6); |
| 218 |
expect(options2.plugins[5]).not.toBe(options1.plugins[5]); |
| 219 |
for (let i = 0; i < 5; i++) { |
| 220 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 221 |
} |
| 222 |
|
| 223 |
process.env.INVALIDATE_PRESET2 = true; |
| 224 |
|
| 225 |
const options3 = loadOptionsSync(opts); |
| 226 |
expect(options3.plugins.length).toBe(options1.plugins.length); |
| 227 |
expect(options3.plugins.length).toBe(6); |
| 228 |
expect(options3.plugins[4]).not.toBe(options1.plugins[4]); |
| 229 |
expect(options3.plugins[5]).not.toBe(options1.plugins[5]); |
| 230 |
for (let i = 0; i < 4; i++) { |
| 231 |
expect(options3.plugins[i]).toBe(options1.plugins[i]); |
| 232 |
} |
| 233 |
}); |
| 234 |
|
| 235 |
it("should invalidate the config file and its plugins/presets", () => { |
| 236 |
const opts = makeOpts(); |
| 237 |
|
| 238 |
const options1 = loadOptionsSync(opts); |
| 239 |
|
| 240 |
process.env.INVALIDATE_BABELRC = true; |
| 241 |
|
| 242 |
const options2 = loadOptionsSync(opts); |
| 243 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 244 |
expect(options2.plugins.length).toBe(6); |
| 245 |
expect(options2.plugins[0]).not.toBe(options1.plugins[0]); |
| 246 |
expect(options2.plugins[1]).not.toBe(options1.plugins[1]); |
| 247 |
expect(options2.plugins[4]).not.toBe(options1.plugins[4]); |
| 248 |
expect(options2.plugins[5]).not.toBe(options1.plugins[5]); |
| 249 |
|
| 250 |
expect(options2.plugins[2]).toBe(options1.plugins[2]); |
| 251 |
expect(options2.plugins[3]).toBe(options1.plugins[3]); |
| 252 |
}); |
| 253 |
}); |
| 254 |
|
| 255 |
describe("loadOptionsAsync", () => { |
| 256 |
it("should load and cache the config with plugins and presets when executed in parallel", async () => { |
| 257 |
const opts = makeOpts(); |
| 258 |
|
| 259 |
const [options1, options2] = await Promise.all([ |
| 260 |
loadOptionsAsync(opts), |
| 261 |
loadOptionsAsync(opts), |
| 262 |
]); |
| 263 |
expect(options1.plugins.map(p => p.key)).toEqual([ |
| 264 |
"plugin1", |
| 265 |
"plugin2", |
| 266 |
"plugin6", |
| 267 |
"plugin5", |
| 268 |
"plugin4", |
| 269 |
"plugin3", |
| 270 |
]); |
| 271 |
|
| 272 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 273 |
|
| 274 |
for (let i = 0; i < options2.plugins.length; i++) { |
| 275 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 276 |
} |
| 277 |
}); |
| 278 |
}); |
| 279 |
|
| 280 |
describe("programmatic plugins/presets", () => { |
| 281 |
it("should not invalidate the plugins when given a fresh object", () => { |
| 282 |
const opts = makeOpts(); |
| 283 |
|
| 284 |
const options1 = loadOptionsSync(opts); |
| 285 |
|
| 286 |
const options2 = loadOptionsSync(Object.assign({}, opts)); |
| 287 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 288 |
|
| 289 |
for (let i = 0; i < options2.plugins.length; i++) { |
| 290 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 291 |
} |
| 292 |
}); |
| 293 |
|
| 294 |
it("should not invalidate the plugins when given a fresh arrays", () => { |
| 295 |
const opts = makeOpts(); |
| 296 |
|
| 297 |
const options1 = loadOptionsSync(opts); |
| 298 |
|
| 299 |
const options2 = loadOptionsSync({ |
| 300 |
...opts, |
| 301 |
plugins: opts.plugins.slice(), |
| 302 |
}); |
| 303 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 304 |
|
| 305 |
for (let i = 0; i < options2.plugins.length; i++) { |
| 306 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 307 |
} |
| 308 |
}); |
| 309 |
|
| 310 |
it("should not invalidate the presets when given a fresh arrays", () => { |
| 311 |
const opts = makeOpts(); |
| 312 |
|
| 313 |
const options1 = loadOptionsSync(opts); |
| 314 |
|
| 315 |
const options2 = loadOptionsSync({ |
| 316 |
...opts, |
| 317 |
presets: opts.presets.slice(), |
| 318 |
}); |
| 319 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 320 |
|
| 321 |
for (let i = 0; i < options2.plugins.length; i++) { |
| 322 |
expect(options2.plugins[i]).toBe(options1.plugins[i]); |
| 323 |
} |
| 324 |
}); |
| 325 |
|
| 326 |
it("should invalidate the plugins when given a fresh options", () => { |
| 327 |
const opts = makeOpts(); |
| 328 |
|
| 329 |
const options1 = loadOptionsSync(opts); |
| 330 |
|
| 331 |
const options2 = loadOptionsSync({ |
| 332 |
...opts, |
| 333 |
plugins: opts.plugins.map(([plg, opt]) => [plg, { ...opt }]), |
| 334 |
}); |
| 335 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 336 |
expect(options2.plugins.length).toBe(6); |
| 337 |
|
| 338 |
expect(options2.plugins[2]).not.toBe(options1.plugins[2]); |
| 339 |
|
| 340 |
expect(options2.plugins[0]).toBe(options1.plugins[0]); |
| 341 |
expect(options2.plugins[1]).toBe(options1.plugins[1]); |
| 342 |
expect(options2.plugins[3]).toBe(options1.plugins[3]); |
| 343 |
expect(options2.plugins[4]).toBe(options1.plugins[4]); |
| 344 |
expect(options2.plugins[5]).toBe(options1.plugins[5]); |
| 345 |
}); |
| 346 |
|
| 347 |
it("should invalidate the presets when given a fresh options", () => { |
| 348 |
const opts = makeOpts(); |
| 349 |
|
| 350 |
const options1 = loadOptionsSync(opts); |
| 351 |
|
| 352 |
const options2 = loadOptionsSync({ |
| 353 |
...opts, |
| 354 |
presets: opts.presets.map(([plg, opt]) => [plg, { ...opt }]), |
| 355 |
}); |
| 356 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 357 |
expect(options2.plugins.length).toBe(6); |
| 358 |
|
| 359 |
expect(options2.plugins[3]).not.toBe(options1.plugins[3]); |
| 360 |
|
| 361 |
expect(options2.plugins[0]).toBe(options1.plugins[0]); |
| 362 |
expect(options2.plugins[1]).toBe(options1.plugins[1]); |
| 363 |
expect(options2.plugins[2]).toBe(options1.plugins[2]); |
| 364 |
expect(options2.plugins[4]).toBe(options1.plugins[4]); |
| 365 |
expect(options2.plugins[5]).toBe(options1.plugins[5]); |
| 366 |
}); |
| 367 |
|
| 368 |
it("should invalidate the programmatic plugins", () => { |
| 369 |
const opts = makeOpts(); |
| 370 |
|
| 371 |
const options1 = loadOptionsSync(opts); |
| 372 |
|
| 373 |
process.env.INVALIDATE_PLUGIN6 = true; |
| 374 |
|
| 375 |
const options2 = loadOptionsSync(opts); |
| 376 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 377 |
expect(options2.plugins.length).toBe(6); |
| 378 |
|
| 379 |
expect(options2.plugins[2]).not.toBe(options1.plugins[2]); |
| 380 |
|
| 381 |
expect(options2.plugins[0]).toBe(options1.plugins[0]); |
| 382 |
expect(options2.plugins[1]).toBe(options1.plugins[1]); |
| 383 |
expect(options2.plugins[3]).toBe(options1.plugins[3]); |
| 384 |
expect(options2.plugins[4]).toBe(options1.plugins[4]); |
| 385 |
expect(options2.plugins[5]).toBe(options1.plugins[5]); |
| 386 |
}); |
| 387 |
|
| 388 |
it("should invalidate the programmatic presets and their children", () => { |
| 389 |
const opts = makeOpts(); |
| 390 |
|
| 391 |
const options1 = loadOptionsSync(opts); |
| 392 |
|
| 393 |
process.env.INVALIDATE_PRESET3 = true; |
| 394 |
|
| 395 |
const options2 = loadOptionsSync(opts); |
| 396 |
expect(options2.plugins.length).toBe(options1.plugins.length); |
| 397 |
expect(options2.plugins.length).toBe(6); |
| 398 |
|
| 399 |
expect(options2.plugins[3]).not.toBe(options1.plugins[3]); |
| 400 |
|
| 401 |
expect(options2.plugins[0]).toBe(options1.plugins[0]); |
| 402 |
expect(options2.plugins[1]).toBe(options1.plugins[1]); |
| 403 |
expect(options2.plugins[2]).toBe(options1.plugins[2]); |
| 404 |
expect(options2.plugins[4]).toBe(options1.plugins[4]); |
| 405 |
expect(options2.plugins[5]).toBe(options1.plugins[5]); |
| 406 |
}); |
| 407 |
|
| 408 |
it("should thrown when plugin is not valid", () => { |
| 409 |
const fooPlugin = { |
| 410 |
inherits: "inhertis-should-not-be-string", |
| 411 |
}; |
| 412 |
const opts = { |
| 413 |
cwd: path.dirname(FILEPATH), |
| 414 |
filename: FILEPATH, |
| 415 |
plugins: [fooPlugin], |
| 416 |
}; |
| 417 |
|
| 418 |
expect(() => loadOptionsSync(opts)).toThrow( |
| 419 |
/\.inherits must be a function, or undefined/, |
| 420 |
); |
| 421 |
}); |
| 422 |
|
| 423 |
it("should throw when plugin contains `enter` handler", () => { |
| 424 |
const fooPlugin = { |
| 425 |
visitor: { |
| 426 |
enter() {}, |
| 427 |
}, |
| 428 |
}; |
| 429 |
const opts = { |
| 430 |
cwd: path.dirname(FILEPATH), |
| 431 |
filename: FILEPATH, |
| 432 |
plugins: [fooPlugin], |
| 433 |
}; |
| 434 |
|
| 435 |
expect(() => loadOptionsSync(opts)).toThrow( |
| 436 |
/\.visitor cannot contain catch-all "enter" or "exit" handlers\. Please target individual nodes\./, |
| 437 |
); |
| 438 |
}); |
| 439 |
}); |
| 440 |
|
| 441 |
describe("caller metadata", () => { |
| 442 |
it("should pass caller data through", () => { |
| 443 |
const options1 = loadOptionsSync({ |
| 444 |
...makeOpts(), |
| 445 |
caller: { |
| 446 |
name: "babel-test", |
| 447 |
someFlag: true, |
| 448 |
}, |
| 449 |
}); |
| 450 |
|
| 451 |
expect(options1.caller.name).toBe("babel-test"); |
| 452 |
expect(options1.caller.someFlag).toBe(true); |
| 453 |
}); |
| 454 |
|
| 455 |
it("should pass unknown caller data through", () => { |
| 456 |
const options1 = loadOptionsSync({ |
| 457 |
...makeOpts(), |
| 458 |
caller: undefined, |
| 459 |
}); |
| 460 |
|
| 461 |
expect(options1.caller).toBeUndefined(); |
| 462 |
}); |
| 463 |
|
| 464 |
it("should pass caller data to test functions", () => { |
| 465 |
const options1 = loadOptionsSync({ |
| 466 |
...makeOpts(), |
| 467 |
caller: { |
| 468 |
name: "babel-test", |
| 469 |
someFlag: true, |
| 470 |
}, |
| 471 |
overrides: [ |
| 472 |
{ |
| 473 |
test: (filename, { caller }) => caller.name === "babel-test", |
| 474 |
comments: false, |
| 475 |
}, |
| 476 |
{ |
| 477 |
test: (filename, { caller }) => caller.name !== "babel-test", |
| 478 |
ast: false, |
| 479 |
}, |
| 480 |
], |
| 481 |
}); |
| 482 |
|
| 483 |
expect(options1.comments).toBe(false); |
| 484 |
expect(options1.ast).not.toBe(false); |
| 485 |
}); |
| 486 |
}); |
| 487 |
}); |