config-chain.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. import fs from "fs";
  2. import path from "path";
  3. import { loadOptions as loadOptionsOrig } from "../lib";
  4. function fixture(...args) {
  5. return path.join(__dirname, "fixtures", "config", ...args);
  6. }
  7. function loadOptions(opts) {
  8. return loadOptionsOrig({
  9. cwd: __dirname,
  10. ...opts,
  11. });
  12. }
  13. describe("buildConfigChain", function() {
  14. describe("test", () => {
  15. describe("single", () => {
  16. it("should process matching string values", () => {
  17. const opts = loadOptions({
  18. cwd: fixture("nonexistant-fake"),
  19. filename: fixture("nonexistant-fake", "src.js"),
  20. babelrc: false,
  21. test: fixture("nonexistant-fake"),
  22. comments: true,
  23. });
  24. expect(opts.comments).toBe(true);
  25. });
  26. it("should process matching RegExp values", () => {
  27. const opts = loadOptions({
  28. cwd: fixture("nonexistant-fake"),
  29. filename: fixture("nonexistant-fake", "src.js"),
  30. babelrc: false,
  31. test: new RegExp(fixture("nonexistant-fake")),
  32. comments: true,
  33. });
  34. expect(opts.comments).toBe(true);
  35. });
  36. it("should process matching function values", () => {
  37. const opts = loadOptions({
  38. cwd: fixture("nonexistant-fake"),
  39. filename: fixture("nonexistant-fake", "src.js"),
  40. babelrc: false,
  41. test: p => p.indexOf(fixture("nonexistant-fake")) === 0,
  42. comments: true,
  43. });
  44. expect(opts.comments).toBe(true);
  45. });
  46. it("should process non-matching string values", () => {
  47. const opts = loadOptions({
  48. cwd: fixture("nonexistant-fake"),
  49. filename: fixture("nonexistant-fake", "src.js"),
  50. babelrc: false,
  51. test: fixture("nonexistant-fake-unknown"),
  52. comments: true,
  53. });
  54. expect(opts.comments).toBeUndefined();
  55. });
  56. it("should process non-matching RegExp values", () => {
  57. const opts = loadOptions({
  58. cwd: fixture("nonexistant-fake"),
  59. filename: fixture("nonexistant-fake", "src.js"),
  60. babelrc: false,
  61. test: new RegExp(fixture("nonexistant-unknown")),
  62. comments: true,
  63. });
  64. expect(opts.comments).toBeUndefined();
  65. });
  66. it("should process non-matching function values", () => {
  67. const opts = loadOptions({
  68. cwd: fixture("nonexistant-fake"),
  69. filename: fixture("nonexistant-fake", "src.js"),
  70. babelrc: false,
  71. test: p => p.indexOf(fixture("nonexistant-unknown")) === 0,
  72. comments: true,
  73. });
  74. expect(opts.comments).toBeUndefined();
  75. });
  76. });
  77. describe("array", () => {
  78. it("should process matching string values", () => {
  79. const opts = loadOptions({
  80. cwd: fixture("nonexistant-fake"),
  81. filename: fixture("nonexistant-fake", "src.js"),
  82. babelrc: false,
  83. test: [fixture("nonexistant-fake")],
  84. comments: true,
  85. });
  86. expect(opts.comments).toBe(true);
  87. });
  88. it("should process matching RegExp values", () => {
  89. const opts = loadOptions({
  90. cwd: fixture("nonexistant-fake"),
  91. filename: fixture("nonexistant-fake", "src.js"),
  92. babelrc: false,
  93. test: [new RegExp(fixture("nonexistant-fake"))],
  94. comments: true,
  95. });
  96. expect(opts.comments).toBe(true);
  97. });
  98. it("should process matching function values", () => {
  99. const opts = loadOptions({
  100. cwd: fixture("nonexistant-fake"),
  101. filename: fixture("nonexistant-fake", "src.js"),
  102. babelrc: false,
  103. test: [p => p.indexOf(fixture("nonexistant-fake")) === 0],
  104. comments: true,
  105. });
  106. expect(opts.comments).toBe(true);
  107. });
  108. it("should process non-matching string values", () => {
  109. const opts = loadOptions({
  110. cwd: fixture("nonexistant-fake"),
  111. filename: fixture("nonexistant-fake", "src.js"),
  112. babelrc: false,
  113. test: [fixture("nonexistant-fake-unknown")],
  114. comments: true,
  115. });
  116. expect(opts.comments).toBeUndefined();
  117. });
  118. it("should process non-matching RegExp values", () => {
  119. const opts = loadOptions({
  120. cwd: fixture("nonexistant-fake"),
  121. filename: fixture("nonexistant-fake", "src.js"),
  122. babelrc: false,
  123. test: [new RegExp(fixture("nonexistant-unknown"))],
  124. comments: true,
  125. });
  126. expect(opts.comments).toBeUndefined();
  127. });
  128. it("should process non-matching function values", () => {
  129. const opts = loadOptions({
  130. cwd: fixture("nonexistant-fake"),
  131. filename: fixture("nonexistant-fake", "src.js"),
  132. babelrc: false,
  133. test: [p => p.indexOf(fixture("nonexistant-unknown")) === 0],
  134. comments: true,
  135. });
  136. expect(opts.comments).toBeUndefined();
  137. });
  138. });
  139. });
  140. describe("include", () => {
  141. describe("single", () => {
  142. it("should process matching string values", () => {
  143. const opts = loadOptions({
  144. cwd: fixture("nonexistant-fake"),
  145. filename: fixture("nonexistant-fake", "src.js"),
  146. babelrc: false,
  147. include: fixture("nonexistant-fake"),
  148. comments: true,
  149. });
  150. expect(opts.comments).toBe(true);
  151. });
  152. it("should process matching RegExp values", () => {
  153. const opts = loadOptions({
  154. cwd: fixture("nonexistant-fake"),
  155. filename: fixture("nonexistant-fake", "src.js"),
  156. babelrc: false,
  157. include: new RegExp(fixture("nonexistant-fake")),
  158. comments: true,
  159. });
  160. expect(opts.comments).toBe(true);
  161. });
  162. it("should process matching function values", () => {
  163. const opts = loadOptions({
  164. cwd: fixture("nonexistant-fake"),
  165. filename: fixture("nonexistant-fake", "src.js"),
  166. babelrc: false,
  167. include: p => p.indexOf(fixture("nonexistant-fake")) === 0,
  168. comments: true,
  169. });
  170. expect(opts.comments).toBe(true);
  171. });
  172. it("should process non-matching string values", () => {
  173. const opts = loadOptions({
  174. cwd: fixture("nonexistant-fake"),
  175. filename: fixture("nonexistant-fake", "src.js"),
  176. babelrc: false,
  177. include: fixture("nonexistant-fake-unknown"),
  178. comments: true,
  179. });
  180. expect(opts.comments).toBeUndefined();
  181. });
  182. it("should process non-matching RegExp values", () => {
  183. const opts = loadOptions({
  184. cwd: fixture("nonexistant-fake"),
  185. filename: fixture("nonexistant-fake", "src.js"),
  186. babelrc: false,
  187. include: new RegExp(fixture("nonexistant-unknown")),
  188. comments: true,
  189. });
  190. expect(opts.comments).toBeUndefined();
  191. });
  192. it("should process non-matching function values", () => {
  193. const opts = loadOptions({
  194. cwd: fixture("nonexistant-fake"),
  195. filename: fixture("nonexistant-fake", "src.js"),
  196. babelrc: false,
  197. include: p => p.indexOf(fixture("nonexistant-unknown")) === 0,
  198. comments: true,
  199. });
  200. expect(opts.comments).toBeUndefined();
  201. });
  202. });
  203. describe("array", () => {
  204. it("should process matching string values", () => {
  205. const opts = loadOptions({
  206. cwd: fixture("nonexistant-fake"),
  207. filename: fixture("nonexistant-fake", "src.js"),
  208. babelrc: false,
  209. include: [fixture("nonexistant-fake")],
  210. comments: true,
  211. });
  212. expect(opts.comments).toBe(true);
  213. });
  214. it("should process matching RegExp values", () => {
  215. const opts = loadOptions({
  216. cwd: fixture("nonexistant-fake"),
  217. filename: fixture("nonexistant-fake", "src.js"),
  218. babelrc: false,
  219. include: [new RegExp(fixture("nonexistant-fake"))],
  220. comments: true,
  221. });
  222. expect(opts.comments).toBe(true);
  223. });
  224. it("should process matching function values", () => {
  225. const opts = loadOptions({
  226. cwd: fixture("nonexistant-fake"),
  227. filename: fixture("nonexistant-fake", "src.js"),
  228. babelrc: false,
  229. include: [p => p.indexOf(fixture("nonexistant-fake")) === 0],
  230. comments: true,
  231. });
  232. expect(opts.comments).toBe(true);
  233. });
  234. it("should process non-matching string values", () => {
  235. const opts = loadOptions({
  236. cwd: fixture("nonexistant-fake"),
  237. filename: fixture("nonexistant-fake", "src.js"),
  238. babelrc: false,
  239. include: [fixture("nonexistant-fake-unknown")],
  240. comments: true,
  241. });
  242. expect(opts.comments).toBeUndefined();
  243. });
  244. it("should process non-matching RegExp values", () => {
  245. const opts = loadOptions({
  246. cwd: fixture("nonexistant-fake"),
  247. filename: fixture("nonexistant-fake", "src.js"),
  248. babelrc: false,
  249. include: [new RegExp(fixture("nonexistant-unknown"))],
  250. comments: true,
  251. });
  252. expect(opts.comments).toBeUndefined();
  253. });
  254. it("should process non-matching function values", () => {
  255. const opts = loadOptions({
  256. cwd: fixture("nonexistant-fake"),
  257. filename: fixture("nonexistant-fake", "src.js"),
  258. babelrc: false,
  259. include: [p => p.indexOf(fixture("nonexistant-unknown")) === 0],
  260. comments: true,
  261. });
  262. expect(opts.comments).toBeUndefined();
  263. });
  264. });
  265. });
  266. describe("exclude", () => {
  267. describe("single", () => {
  268. it("should process matching string values", () => {
  269. const opts = loadOptions({
  270. cwd: fixture("nonexistant-fake"),
  271. filename: fixture("nonexistant-fake", "src.js"),
  272. babelrc: false,
  273. exclude: fixture("nonexistant-fake"),
  274. comments: true,
  275. });
  276. expect(opts.comments).toBeUndefined();
  277. });
  278. it("should process matching RegExp values", () => {
  279. const opts = loadOptions({
  280. cwd: fixture("nonexistant-fake"),
  281. filename: fixture("nonexistant-fake", "src.js"),
  282. babelrc: false,
  283. exclude: new RegExp(fixture("nonexistant-fake")),
  284. comments: true,
  285. });
  286. expect(opts.comments).toBeUndefined();
  287. });
  288. it("should process matching function values", () => {
  289. const opts = loadOptions({
  290. cwd: fixture("nonexistant-fake"),
  291. filename: fixture("nonexistant-fake", "src.js"),
  292. babelrc: false,
  293. exclude: p => p.indexOf(fixture("nonexistant-fake")) === 0,
  294. comments: true,
  295. });
  296. expect(opts.comments).toBeUndefined();
  297. });
  298. it("should process non-matching string values", () => {
  299. const opts = loadOptions({
  300. cwd: fixture("nonexistant-fake"),
  301. filename: fixture("nonexistant-fake", "src.js"),
  302. babelrc: false,
  303. exclude: fixture("nonexistant-fake-unknown"),
  304. comments: true,
  305. });
  306. expect(opts.comments).toBe(true);
  307. });
  308. it("should process non-matching RegExp values", () => {
  309. const opts = loadOptions({
  310. cwd: fixture("nonexistant-fake"),
  311. filename: fixture("nonexistant-fake", "src.js"),
  312. babelrc: false,
  313. exclude: new RegExp(fixture("nonexistant-unknown")),
  314. comments: true,
  315. });
  316. expect(opts.comments).toBe(true);
  317. });
  318. it("should process non-matching function values", () => {
  319. const opts = loadOptions({
  320. cwd: fixture("nonexistant-fake"),
  321. filename: fixture("nonexistant-fake", "src.js"),
  322. babelrc: false,
  323. exclude: p => p.indexOf(fixture("nonexistant-unknown")) === 0,
  324. comments: true,
  325. });
  326. expect(opts.comments).toBe(true);
  327. });
  328. });
  329. describe("array", () => {
  330. it("should process matching string values", () => {
  331. const opts = loadOptions({
  332. cwd: fixture("nonexistant-fake"),
  333. filename: fixture("nonexistant-fake", "src.js"),
  334. babelrc: false,
  335. exclude: [fixture("nonexistant-fake")],
  336. comments: true,
  337. });
  338. expect(opts.comments).toBeUndefined();
  339. });
  340. it("should process matching RegExp values", () => {
  341. const opts = loadOptions({
  342. cwd: fixture("nonexistant-fake"),
  343. filename: fixture("nonexistant-fake", "src.js"),
  344. babelrc: false,
  345. exclude: [new RegExp(fixture("nonexistant-fake"))],
  346. comments: true,
  347. });
  348. expect(opts.comments).toBeUndefined();
  349. });
  350. it("should process matching function values", () => {
  351. const opts = loadOptions({
  352. cwd: fixture("nonexistant-fake"),
  353. filename: fixture("nonexistant-fake", "src.js"),
  354. babelrc: false,
  355. exclude: [p => p.indexOf(fixture("nonexistant-fake")) === 0],
  356. comments: true,
  357. });
  358. expect(opts.comments).toBeUndefined();
  359. });
  360. it("should process non-matching string values", () => {
  361. const opts = loadOptions({
  362. cwd: fixture("nonexistant-fake"),
  363. filename: fixture("nonexistant-fake", "src.js"),
  364. babelrc: false,
  365. exclude: [fixture("nonexistant-fake-unknown")],
  366. comments: true,
  367. });
  368. expect(opts.comments).toBe(true);
  369. });
  370. it("should process non-matching RegExp values", () => {
  371. const opts = loadOptions({
  372. cwd: fixture("nonexistant-fake"),
  373. filename: fixture("nonexistant-fake", "src.js"),
  374. babelrc: false,
  375. exclude: [new RegExp(fixture("nonexistant-unknown"))],
  376. comments: true,
  377. });
  378. expect(opts.comments).toBe(true);
  379. });
  380. it("should process non-matching function values", () => {
  381. const opts = loadOptions({
  382. cwd: fixture("nonexistant-fake"),
  383. filename: fixture("nonexistant-fake", "src.js"),
  384. babelrc: false,
  385. exclude: [p => p.indexOf(fixture("nonexistant-unknown")) === 0],
  386. comments: true,
  387. });
  388. expect(opts.comments).toBe(true);
  389. });
  390. });
  391. });
  392. describe("ignore", () => {
  393. it("should ignore files that match", () => {
  394. const opts = loadOptions({
  395. cwd: fixture("nonexistant-fake"),
  396. filename: fixture("nonexistant-fake", "src.js"),
  397. babelrc: false,
  398. ignore: [
  399. fixture("nonexistant-fake", "src.js"),
  400. // We had a regression where multiple ignore patterns broke things, so
  401. // we keep some extra random items in here.
  402. fixture("nonexistant-fake", "other.js"),
  403. fixture("nonexistant-fake", "misc.js"),
  404. ],
  405. });
  406. expect(opts).toBeNull();
  407. });
  408. it("should not ignore files that don't match", () => {
  409. const opts = loadOptions({
  410. cwd: fixture("nonexistant-fake"),
  411. filename: fixture("nonexistant-fake", "src.js"),
  412. babelrc: false,
  413. ignore: [
  414. fixture("nonexistant-fake", "other.js"),
  415. fixture("nonexistant-fake", "misc.js"),
  416. ],
  417. });
  418. expect(opts).not.toBeNull();
  419. });
  420. });
  421. describe("only", () => {
  422. it("should ignore files that don't match", () => {
  423. const opts = loadOptions({
  424. cwd: fixture("nonexistant-fake"),
  425. filename: fixture("nonexistant-fake", "src.js"),
  426. babelrc: false,
  427. only: [
  428. fixture("nonexistant-fake", "other.js"),
  429. fixture("nonexistant-fake", "misc.js"),
  430. ],
  431. });
  432. expect(opts).toBeNull();
  433. });
  434. it("should not ignore files that match", () => {
  435. const opts = loadOptions({
  436. cwd: fixture("nonexistant-fake"),
  437. filename: fixture("nonexistant-fake", "src.js"),
  438. babelrc: false,
  439. only: [
  440. fixture("nonexistant-fake", "src.js"),
  441. fixture("nonexistant-fake", "misc.js"),
  442. ],
  443. });
  444. expect(opts).not.toBeNull();
  445. });
  446. });
  447. describe("ignore/only", () => {
  448. it("should ignore files that match ignore and don't match only", () => {
  449. const opts = loadOptions({
  450. cwd: fixture("nonexistant-fake"),
  451. filename: fixture("nonexistant-fake", "src.js"),
  452. babelrc: false,
  453. ignore: [fixture("nonexistant-fake", "src.js")],
  454. only: [],
  455. });
  456. expect(opts).toBeNull();
  457. });
  458. it("should ignore files that match ignore and also only", () => {
  459. const opts = loadOptions({
  460. cwd: fixture("nonexistant-fake"),
  461. filename: fixture("nonexistant-fake", "src.js"),
  462. babelrc: false,
  463. ignore: [fixture("nonexistant-fake", "src.js")],
  464. only: [fixture("nonexistant-fake", "src.js")],
  465. });
  466. expect(opts).toBeNull();
  467. });
  468. it("should not ignore files that match only and not ignore", () => {
  469. const opts = loadOptions({
  470. cwd: fixture("nonexistant-fake"),
  471. filename: fixture("nonexistant-fake", "src.js"),
  472. babelrc: false,
  473. only: [fixture("nonexistant-fake", "src.js")],
  474. });
  475. expect(opts).not.toBeNull();
  476. });
  477. it("should not ignore files when no ignore/only are specified", () => {
  478. const opts = loadOptions({
  479. cwd: fixture("nonexistant-fake"),
  480. filename: fixture("nonexistant-fake", "src.js"),
  481. babelrc: false,
  482. });
  483. expect(opts).not.toBeNull();
  484. });
  485. it("should allow negation of only", () => {
  486. const opts1 = loadOptions({
  487. cwd: fixture("nonexistant-fake"),
  488. filename: fixture("nonexistant-fake", "src.js"),
  489. babelrc: false,
  490. only: [
  491. "!" + fixture("nonexistant-fake"),
  492. fixture("nonexistant-fake", "other.js"),
  493. ],
  494. });
  495. expect(opts1).toBeNull();
  496. const opts2 = loadOptions({
  497. cwd: fixture("nonexistant-fake"),
  498. filename: fixture("nonexistant-fake", "src.js"),
  499. babelrc: false,
  500. only: [
  501. "!" + fixture("nonexistant-fake"),
  502. fixture("nonexistant-fake", "src.js"),
  503. ],
  504. });
  505. expect(opts2).not.toBeNull();
  506. const opts3 = loadOptions({
  507. cwd: fixture("nonexistant-fake"),
  508. filename: fixture("nonexistant-fake", "folder", "src.js"),
  509. babelrc: false,
  510. only: [
  511. "!" + fixture("nonexistant-fake"),
  512. fixture("nonexistant-fake", "folder"),
  513. ],
  514. });
  515. expect(opts3).not.toBeNull();
  516. });
  517. it("should allow negation of ignore", () => {
  518. const opts1 = loadOptions({
  519. cwd: fixture("nonexistant-fake"),
  520. filename: fixture("nonexistant-fake", "src.js"),
  521. babelrc: false,
  522. ignore: [
  523. "!" + fixture("nonexistant-fake", "other.js"),
  524. fixture("nonexistant-fake"),
  525. ],
  526. });
  527. expect(opts1).toBeNull();
  528. // Tests disabled pending https://github.com/babel/babel/issues/6907
  529. // const opts2 = loadOptions({
  530. // cwd: fixture("nonexistant-fake"),
  531. // filename: fixture("nonexistant-fake", "src.js"),
  532. // babelrc: false,
  533. // ignore: [
  534. // "!" + fixture("nonexistant-fake", "src.js"),
  535. // fixture("nonexistant-fake"),
  536. // ],
  537. // });
  538. // expect(opts2).not.toBeNull();
  539. //
  540. // const opts3 = loadOptions({
  541. // cwd: fixture("nonexistant-fake"),
  542. // filename: fixture("nonexistant-fake", "folder", "src.js"),
  543. // babelrc: false,
  544. // ignore: [
  545. // "!" + fixture("nonexistant-fake", "folder"),
  546. // fixture("nonexistant-fake"),
  547. // ],
  548. // });
  549. // expect(opts3).not.toBeNull();
  550. });
  551. });
  552. describe("caching", function() {
  553. describe("programmatic options", function() {
  554. const plugins1 = [() => ({})];
  555. const plugins2 = [() => ({})];
  556. it("should not cache the input options by identity", () => {
  557. const inputOpts = { plugins: plugins1 };
  558. const opts1 = loadOptions(inputOpts);
  559. inputOpts.plugins = plugins2;
  560. const opts2 = loadOptions(inputOpts);
  561. expect(opts1.plugins).toHaveLength(1);
  562. expect(opts2.plugins).toHaveLength(1);
  563. expect(opts1.plugins[0]).not.toBe(opts2.plugins[1]);
  564. });
  565. it("should cache the env plugins by identity", () => {
  566. const plugins = [() => ({})];
  567. const opts1 = loadOptions({
  568. envName: "foo",
  569. env: {
  570. foo: {
  571. plugins,
  572. },
  573. },
  574. });
  575. const opts2 = loadOptions({
  576. envName: "foo",
  577. env: {
  578. foo: {
  579. plugins,
  580. },
  581. },
  582. });
  583. expect(opts1.plugins).toHaveLength(1);
  584. expect(opts2.plugins).toHaveLength(1);
  585. expect(opts1.plugins[0]).toBe(opts2.plugins[0]);
  586. });
  587. it("should cache the env presets by identity", () => {
  588. const presets = [() => ({ plugins: [() => ({})] })];
  589. const opts1 = loadOptions({
  590. envName: "foo",
  591. env: {
  592. foo: {
  593. presets,
  594. },
  595. },
  596. });
  597. const opts2 = loadOptions({
  598. envName: "foo",
  599. env: {
  600. foo: {
  601. presets,
  602. },
  603. },
  604. });
  605. expect(opts1.plugins).toHaveLength(1);
  606. expect(opts2.plugins).toHaveLength(1);
  607. expect(opts1.plugins[0]).toBe(opts2.plugins[0]);
  608. });
  609. it("should cache the plugin options by identity", () => {
  610. const plugins = [() => ({})];
  611. const opts1 = loadOptions({ plugins });
  612. const opts2 = loadOptions({ plugins });
  613. expect(opts1.plugins).toHaveLength(1);
  614. expect(opts2.plugins).toHaveLength(1);
  615. expect(opts1.plugins[0]).toBe(opts2.plugins[0]);
  616. });
  617. it("should cache the presets options by identity", () => {
  618. const presets = [() => ({ plugins: [() => ({})] })];
  619. const opts1 = loadOptions({ presets });
  620. const opts2 = loadOptions({ presets });
  621. expect(opts1.plugins).toHaveLength(1);
  622. expect(opts2.plugins).toHaveLength(1);
  623. expect(opts1.plugins[0]).toBe(opts2.plugins[0]);
  624. });
  625. it("should not cache the presets options with passPerPreset", () => {
  626. const presets = [() => ({ plugins: [() => ({})] })];
  627. const opts1 = loadOptions({ presets });
  628. const opts2 = loadOptions({ presets, passPerPreset: true });
  629. const opts3 = loadOptions({ presets, passPerPreset: false });
  630. expect(opts1.plugins).toHaveLength(1);
  631. expect(opts2.plugins).toHaveLength(0);
  632. expect(opts3.plugins).toHaveLength(1);
  633. expect(opts1.plugins[0]).toBe(opts3.plugins[0]);
  634. });
  635. });
  636. describe("config file options", function() {
  637. function touch(filepath) {
  638. const s = fs.statSync(filepath);
  639. fs.utimesSync(
  640. filepath,
  641. s.atime,
  642. s.mtime + Math.random() > 0.5 ? 1 : -1,
  643. );
  644. }
  645. it("should cache package.json files by mtime", () => {
  646. const filename = fixture(
  647. "complex-plugin-config",
  648. "config-identity",
  649. "pkg",
  650. "src.js",
  651. );
  652. const pkgJSON = fixture(
  653. "complex-plugin-config",
  654. "config-identity",
  655. "pkg",
  656. "package.json",
  657. );
  658. const opts1 = loadOptions({ filename, cwd: path.dirname(filename) });
  659. const opts2 = loadOptions({ filename, cwd: path.dirname(filename) });
  660. touch(pkgJSON);
  661. const opts3 = loadOptions({ filename, cwd: path.dirname(filename) });
  662. const opts4 = loadOptions({ filename, cwd: path.dirname(filename) });
  663. expect(opts1.plugins).toHaveLength(1);
  664. expect(opts2.plugins).toHaveLength(1);
  665. expect(opts1.plugins[0]).toBe(opts2.plugins[0]);
  666. expect(opts1.plugins).toHaveLength(1);
  667. expect(opts2.plugins).toHaveLength(1);
  668. expect(opts3.plugins[0]).toBe(opts4.plugins[0]);
  669. // Identity changed after touch().
  670. expect(opts1.plugins[0]).not.toBe(opts3.plugins[0]);
  671. });
  672. it("should cache .babelrc files by mtime", () => {
  673. const filename = fixture(
  674. "complex-plugin-config",
  675. "config-identity",
  676. "babelrc",
  677. "src.js",
  678. );
  679. const babelrcFile = fixture(
  680. "complex-plugin-config",
  681. "config-identity",
  682. "babelrc",
  683. ".babelrc",
  684. );
  685. const opts1 = loadOptions({ filename, cwd: path.dirname(filename) });
  686. const opts2 = loadOptions({ filename, cwd: path.dirname(filename) });
  687. touch(babelrcFile);
  688. const opts3 = loadOptions({ filename, cwd: path.dirname(filename) });
  689. const opts4 = loadOptions({ filename, cwd: path.dirname(filename) });
  690. expect(opts1.plugins).toHaveLength(1);
  691. expect(opts2.plugins).toHaveLength(1);
  692. expect(opts1.plugins[0]).toBe(opts2.plugins[0]);
  693. expect(opts3.plugins).toHaveLength(1);
  694. expect(opts4.plugins).toHaveLength(1);
  695. expect(opts3.plugins[0]).toBe(opts4.plugins[0]);
  696. // Identity changed after touch().
  697. expect(opts1.plugins[0]).not.toBe(opts3.plugins[0]);
  698. });
  699. it("should cache .babelrc.js files programmable behavior", () => {
  700. const filename = fixture(
  701. "complex-plugin-config",
  702. "config-identity",
  703. "babelrc-js",
  704. "src.js",
  705. );
  706. const opts1 = loadOptions({ filename, cwd: path.dirname(filename) });
  707. const opts2 = loadOptions({ filename, cwd: path.dirname(filename) });
  708. const opts3 = loadOptions({
  709. filename,
  710. envName: "new-env",
  711. cwd: path.dirname(filename),
  712. });
  713. const opts4 = loadOptions({
  714. filename,
  715. envName: "new-env",
  716. cwd: path.dirname(filename),
  717. });
  718. expect(opts1.plugins).toHaveLength(1);
  719. expect(opts2.plugins).toHaveLength(1);
  720. expect(opts1.plugins[0]).toBe(opts2.plugins[0]);
  721. expect(opts3.plugins).toHaveLength(1);
  722. expect(opts4.plugins).toHaveLength(1);
  723. expect(opts3.plugins[0]).toBe(opts4.plugins[0]);
  724. // Identity changed with different .env
  725. expect(opts1.plugins[0]).not.toBe(opts3.plugins[0]);
  726. });
  727. });
  728. });
  729. describe("overrides merging", () => {
  730. it("should apply matching overrides over base configs", () => {
  731. const opts = loadOptions({
  732. cwd: fixture("nonexistant-fake"),
  733. filename: fixture("nonexistant-fake", "src.js"),
  734. babelrc: false,
  735. comments: true,
  736. overrides: [
  737. {
  738. test: fixture("nonexistant-fake"),
  739. comments: false,
  740. },
  741. ],
  742. });
  743. expect(opts.comments).toBe(false);
  744. });
  745. it("should not apply non-matching overrides over base configs", () => {
  746. const opts = loadOptions({
  747. cwd: fixture("nonexistant-fake"),
  748. filename: fixture("nonexistant-fake", "src.js"),
  749. babelrc: false,
  750. comments: true,
  751. overrides: [
  752. {
  753. test: fixture("nonexistant-unknown"),
  754. comments: false,
  755. },
  756. ],
  757. });
  758. expect(opts.comments).toBe(true);
  759. });
  760. });
  761. describe("config files", () => {
  762. const getDefaults = () => ({
  763. babelrc: false,
  764. configFile: false,
  765. cwd: process.cwd(),
  766. root: process.cwd(),
  767. envName: "development",
  768. passPerPreset: false,
  769. plugins: [],
  770. presets: [],
  771. });
  772. const realEnv = process.env.NODE_ENV;
  773. const realBabelEnv = process.env.BABEL_ENV;
  774. beforeAll(() => {
  775. delete process.env.NODE_ENV;
  776. delete process.env.BABEL_ENV;
  777. });
  778. afterAll(() => {
  779. if (realEnv) {
  780. process.env.NODE_ENV = realEnv;
  781. process.env.NODE_ENV = realBabelEnv;
  782. }
  783. });
  784. it("should load .babelrc", () => {
  785. const filename = fixture("config-files", "babelrc", "src.js");
  786. expect(
  787. loadOptions({
  788. filename,
  789. cwd: path.dirname(filename),
  790. }),
  791. ).toEqual({
  792. ...getDefaults(),
  793. filename: filename,
  794. cwd: path.dirname(filename),
  795. root: path.dirname(filename),
  796. comments: true,
  797. });
  798. });
  799. it("should load .babelrc.js", () => {
  800. const filename = fixture("config-files", "babelrc-js", "src.js");
  801. expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
  802. ...getDefaults(),
  803. filename: filename,
  804. cwd: path.dirname(filename),
  805. root: path.dirname(filename),
  806. comments: true,
  807. });
  808. });
  809. it("should load package.json#babel", () => {
  810. const filename = fixture("config-files", "pkg", "src.js");
  811. expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
  812. ...getDefaults(),
  813. filename: filename,
  814. cwd: path.dirname(filename),
  815. root: path.dirname(filename),
  816. comments: true,
  817. });
  818. });
  819. it("should load .babelignore", () => {
  820. const filename = fixture("config-files", "babelignore", "src.js");
  821. expect(loadOptions({ filename, cwd: path.dirname(filename) })).toBeNull();
  822. });
  823. it("should throw if there are both .babelrc and .babelrc.js", () => {
  824. const filename = fixture("config-files", "both-babelrc", "src.js");
  825. expect(() =>
  826. loadOptions({ filename, cwd: path.dirname(filename) }),
  827. ).toThrow(/Multiple configuration files found/);
  828. });
  829. it("should throw if there are both .babelrc and package.json", () => {
  830. const filename = fixture("config-files", "pkg-babelrc", "src.js");
  831. expect(() =>
  832. loadOptions({ filename, cwd: path.dirname(filename) }),
  833. ).toThrow(/Multiple configuration files found/);
  834. });
  835. it("should throw if there are both .babelrc.js and package.json", () => {
  836. const filename = fixture("config-files", "pkg-babelrc-js", "src.js");
  837. expect(() =>
  838. loadOptions({ filename, cwd: path.dirname(filename) }),
  839. ).toThrow(/Multiple configuration files found/);
  840. });
  841. it("should ignore package.json without a 'babel' property", () => {
  842. const filename = fixture("config-files", "pkg-ignored", "src.js");
  843. expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
  844. ...getDefaults(),
  845. filename: filename,
  846. cwd: path.dirname(filename),
  847. root: path.dirname(filename),
  848. comments: true,
  849. });
  850. });
  851. it("should show helpful errors for .babelrc", () => {
  852. const filename = fixture("config-files", "babelrc-error", "src.js");
  853. expect(() =>
  854. loadOptions({ filename, cwd: path.dirname(filename) }),
  855. ).toThrow(/Error while parsing config - /);
  856. });
  857. it("should show helpful errors for .babelrc.js", () => {
  858. const filename = fixture("config-files", "babelrc-js-error", "src.js");
  859. expect(() =>
  860. loadOptions({ filename, cwd: path.dirname(filename) }),
  861. ).toThrow(/Babelrc threw an error/);
  862. });
  863. it("should show helpful errors for package.json", () => {
  864. const filename = fixture("config-files", "pkg-error", "src.js");
  865. expect(() =>
  866. loadOptions({ filename, cwd: path.dirname(filename) }),
  867. ).toThrow(/Error while parsing JSON - /);
  868. });
  869. });
  870. });