option-manager.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { loadOptions as loadOptionsOrig } from "../lib";
  2. import path from "path";
  3. function loadOptions(opts) {
  4. return loadOptionsOrig({
  5. cwd: __dirname,
  6. ...opts,
  7. });
  8. }
  9. describe("option-manager", () => {
  10. it("throws for babel 5 plugin", () => {
  11. return expect(() => {
  12. loadOptions({
  13. plugins: [({ Plugin }) => new Plugin("object-assign", {})],
  14. });
  15. }).toThrow(/Babel 5 plugin is being run with an unsupported Babel/);
  16. });
  17. describe("config plugin/preset flattening and overriding", () => {
  18. function makePlugin() {
  19. const calls = [];
  20. const plugin = (api, opts) => {
  21. calls.push(opts);
  22. return {};
  23. };
  24. return { plugin, calls };
  25. }
  26. it("should throw if a plugin is repeated", () => {
  27. const { calls, plugin } = makePlugin();
  28. expect(() => {
  29. loadOptions({
  30. plugins: [plugin, plugin],
  31. });
  32. }).toThrow(/Duplicate plugin\/preset detected/);
  33. expect(calls).toEqual([]);
  34. });
  35. it("should not throw if a repeated plugin has a different name", () => {
  36. const { calls: calls1, plugin: plugin1 } = makePlugin();
  37. const { calls: calls2, plugin: plugin2 } = makePlugin();
  38. loadOptions({
  39. plugins: [[plugin1, { arg: 1 }], [plugin2, { arg: 2 }, "some-name"]],
  40. });
  41. expect(calls1).toEqual([{ arg: 1 }]);
  42. expect(calls2).toEqual([{ arg: 2 }]);
  43. });
  44. it("should merge .env[] plugins with parent presets", () => {
  45. const { calls: calls1, plugin: plugin1 } = makePlugin();
  46. const { calls: calls2, plugin: plugin2 } = makePlugin();
  47. loadOptions({
  48. envName: "test",
  49. plugins: [[plugin1, { arg: 1 }]],
  50. env: {
  51. test: {
  52. plugins: [[plugin1, { arg: 3 }], [plugin2, { arg: 2 }]],
  53. },
  54. },
  55. });
  56. expect(calls1).toEqual([{ arg: 3 }]);
  57. expect(calls2).toEqual([{ arg: 2 }]);
  58. });
  59. it("should throw if a preset is repeated", () => {
  60. const { calls, plugin: preset } = makePlugin();
  61. expect(() => {
  62. loadOptions({
  63. presets: [preset, preset],
  64. }).toThrow(/Duplicate plugin\/preset detected/);
  65. });
  66. expect(calls).toEqual([]);
  67. });
  68. it("should not throw if a repeated preset has a different name", () => {
  69. const { calls: calls1, plugin: preset1 } = makePlugin();
  70. const { calls: calls2, plugin: preset2 } = makePlugin();
  71. loadOptions({
  72. presets: [[preset1, { arg: 1 }], [preset2, { arg: 2 }, "some-name"]],
  73. });
  74. expect(calls1).toEqual([{ arg: 1 }]);
  75. expect(calls2).toEqual([{ arg: 2 }]);
  76. });
  77. it("should merge .env[] presets with parent presets", () => {
  78. const { calls: calls1, plugin: preset1 } = makePlugin();
  79. const { calls: calls2, plugin: preset2 } = makePlugin();
  80. loadOptions({
  81. envName: "test",
  82. presets: [[preset1, { arg: 1 }]],
  83. env: {
  84. test: {
  85. presets: [[preset1, { arg: 3 }], [preset2, { arg: 2 }]],
  86. },
  87. },
  88. });
  89. expect(calls1).toEqual([{ arg: 3 }]);
  90. expect(calls2).toEqual([{ arg: 2 }]);
  91. });
  92. it("should not merge .env[] presets with parent presets when passPerPreset", () => {
  93. const { calls: calls1, plugin: preset1 } = makePlugin();
  94. const { calls: calls2, plugin: preset2 } = makePlugin();
  95. loadOptions({
  96. envName: "test",
  97. passPerPreset: true,
  98. presets: [[preset1, { arg: 1 }]],
  99. env: {
  100. test: {
  101. presets: [[preset1, { arg: 3 }], [preset2, { arg: 2 }]],
  102. },
  103. },
  104. });
  105. expect(calls1).toEqual([{ arg: 1 }, { arg: 3 }]);
  106. expect(calls2).toEqual([{ arg: 2 }]);
  107. });
  108. });
  109. describe("mergeOptions", () => {
  110. it("throws for removed babel 5 options", () => {
  111. return expect(() => {
  112. loadOptions({
  113. randomOption: true,
  114. });
  115. }).toThrow(/Unknown option: .randomOption/);
  116. });
  117. it("throws for removed babel 5 options", () => {
  118. return expect(() => {
  119. loadOptions({
  120. auxiliaryComment: true,
  121. blacklist: true,
  122. });
  123. }).toThrow(
  124. // eslint-disable-next-line max-len
  125. /Using removed Babel 5 option: .auxiliaryComment - Use `auxiliaryCommentBefore` or `auxiliaryCommentAfter`/,
  126. );
  127. });
  128. it("throws for resolved but erroring preset", () => {
  129. return expect(() => {
  130. loadOptions({
  131. presets: [
  132. path.join(__dirname, "fixtures/option-manager/not-a-preset"),
  133. ],
  134. });
  135. }).toThrow(
  136. /While processing: .*option-manager(?:\/|\\\\)not-a-preset\.js/,
  137. );
  138. });
  139. });
  140. describe("presets", function() {
  141. function presetTest(name) {
  142. it(name, function() {
  143. const options = loadOptions({
  144. presets: [
  145. path.join(__dirname, "fixtures/option-manager/presets", name),
  146. ],
  147. });
  148. expect(Array.isArray(options.plugins)).toBe(true);
  149. expect(options.plugins).toHaveLength(1);
  150. expect(options.presets).toHaveLength(0);
  151. });
  152. }
  153. function presetThrowsTest(name, msg) {
  154. it(name, function() {
  155. expect(() =>
  156. loadOptions({
  157. presets: [
  158. path.join(__dirname, "fixtures/option-manager/presets", name),
  159. ],
  160. }),
  161. ).toThrow(msg);
  162. });
  163. }
  164. presetTest("es5_function");
  165. presetTest("es5_object");
  166. presetTest("es2015_default_function");
  167. presetTest("es2015_default_object");
  168. presetThrowsTest(
  169. "es2015_named",
  170. /Must export a default export when using ES6 modules/,
  171. );
  172. presetThrowsTest("es2015_invalid", /Unsupported format: string/);
  173. presetThrowsTest("es5_invalid", /Unsupported format: string/);
  174. });
  175. });