resolution.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import * as babel from "../lib/index";
  2. import path from "path";
  3. describe("addon resolution", function() {
  4. const base = path.join(__dirname, "fixtures", "resolution");
  5. let cwd;
  6. beforeEach(function() {
  7. cwd = process.cwd();
  8. process.chdir(base);
  9. });
  10. afterEach(function() {
  11. process.chdir(cwd);
  12. });
  13. it("should find module: presets", function() {
  14. process.chdir("module-paths");
  15. babel.transform("", {
  16. filename: "filename.js",
  17. babelrc: false,
  18. presets: ["module:preset"],
  19. });
  20. });
  21. it("should find module: plugins", function() {
  22. process.chdir("module-paths");
  23. babel.transform("", {
  24. filename: "filename.js",
  25. babelrc: false,
  26. plugins: ["module:plugin"],
  27. });
  28. });
  29. it("should find standard presets", function() {
  30. process.chdir("standard-paths");
  31. babel.transform("", {
  32. filename: "filename.js",
  33. babelrc: false,
  34. presets: ["mod"],
  35. });
  36. });
  37. it("should find standard plugins", function() {
  38. process.chdir("standard-paths");
  39. babel.transform("", {
  40. filename: "filename.js",
  41. babelrc: false,
  42. plugins: ["mod"],
  43. });
  44. });
  45. it("should find standard presets with an existing prefix", function() {
  46. process.chdir("standard-paths");
  47. babel.transform("", {
  48. filename: "filename.js",
  49. babelrc: false,
  50. presets: ["babel-preset-mod"],
  51. });
  52. });
  53. it("should find standard plugins with an existing prefix", function() {
  54. process.chdir("standard-paths");
  55. babel.transform("", {
  56. filename: "filename.js",
  57. babelrc: false,
  58. plugins: ["babel-plugin-mod"],
  59. });
  60. });
  61. it("should find @babel scoped presets", function() {
  62. process.chdir("babel-org-paths");
  63. babel.transform("", {
  64. filename: "filename.js",
  65. babelrc: false,
  66. presets: ["@babel/foo"],
  67. });
  68. });
  69. it("should find @babel scoped plugins", function() {
  70. process.chdir("babel-org-paths");
  71. babel.transform("", {
  72. filename: "filename.js",
  73. babelrc: false,
  74. plugins: ["@babel/foo"],
  75. });
  76. });
  77. it("should find @babel scoped presets with an existing prefix", function() {
  78. process.chdir("babel-org-paths");
  79. babel.transform("", {
  80. filename: "filename.js",
  81. babelrc: false,
  82. presets: ["@babel/preset-foo"],
  83. });
  84. });
  85. it("should find @babel scoped plugins", function() {
  86. process.chdir("babel-org-paths");
  87. babel.transform("", {
  88. filename: "filename.js",
  89. babelrc: false,
  90. plugins: ["@babel/plugin-foo"],
  91. });
  92. });
  93. it("should find @foo scoped presets", function() {
  94. process.chdir("foo-org-paths");
  95. babel.transform("", {
  96. filename: "filename.js",
  97. babelrc: false,
  98. presets: ["@foo/mod"],
  99. });
  100. });
  101. it("should find @foo scoped plugins", function() {
  102. process.chdir("foo-org-paths");
  103. babel.transform("", {
  104. filename: "filename.js",
  105. babelrc: false,
  106. plugins: ["@foo/mod"],
  107. });
  108. });
  109. it("should find @foo scoped presets with an existing prefix", function() {
  110. process.chdir("foo-org-paths");
  111. babel.transform("", {
  112. filename: "filename.js",
  113. babelrc: false,
  114. presets: ["@foo/babel-preset-mod"],
  115. });
  116. });
  117. it("should find @foo scoped plugins with an existing prefix", function() {
  118. process.chdir("foo-org-paths");
  119. babel.transform("", {
  120. filename: "filename.js",
  121. babelrc: false,
  122. plugins: ["@foo/babel-plugin-mod"],
  123. });
  124. });
  125. it("should find relative path presets", function() {
  126. process.chdir("relative-paths");
  127. babel.transform("", {
  128. filename: "filename.js",
  129. babelrc: false,
  130. presets: ["./dir/preset.js"],
  131. });
  132. });
  133. it("should find relative path plugins", function() {
  134. process.chdir("relative-paths");
  135. babel.transform("", {
  136. filename: "filename.js",
  137. babelrc: false,
  138. plugins: ["./dir/plugin.js"],
  139. });
  140. });
  141. it("should find module file presets", function() {
  142. process.chdir("nested-module-paths");
  143. babel.transform("", {
  144. filename: "filename.js",
  145. babelrc: false,
  146. presets: ["mod/preset"],
  147. });
  148. });
  149. it("should find module file plugins", function() {
  150. process.chdir("nested-module-paths");
  151. babel.transform("", {
  152. filename: "filename.js",
  153. babelrc: false,
  154. plugins: ["mod/plugin"],
  155. });
  156. });
  157. it("should find @foo scoped module file presets", function() {
  158. process.chdir("scoped-nested-module-paths");
  159. babel.transform("", {
  160. filename: "filename.js",
  161. babelrc: false,
  162. presets: ["@foo/mod/preset"],
  163. });
  164. });
  165. it("should find @foo scoped module file plugins", function() {
  166. process.chdir("scoped-nested-module-paths");
  167. babel.transform("", {
  168. filename: "filename.js",
  169. babelrc: false,
  170. plugins: ["@foo/mod/plugin"],
  171. });
  172. });
  173. it("should find @babel scoped module file presets", function() {
  174. process.chdir("babel-scoped-nested-module-paths");
  175. babel.transform("", {
  176. filename: "filename.js",
  177. babelrc: false,
  178. presets: ["@babel/mod/preset"],
  179. });
  180. });
  181. it("should find @babel scoped module file plugins", function() {
  182. process.chdir("babel-scoped-nested-module-paths");
  183. babel.transform("", {
  184. filename: "filename.js",
  185. babelrc: false,
  186. plugins: ["@babel/mod/plugin"],
  187. });
  188. });
  189. it("should throw about module: usage for presets", function() {
  190. process.chdir("throw-module-paths");
  191. expect(() => {
  192. babel.transform("", {
  193. filename: "filename.js",
  194. babelrc: false,
  195. presets: ["foo"],
  196. });
  197. }).toThrow(
  198. /Cannot find module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/,
  199. );
  200. });
  201. it("should throw about module: usage for plugins", function() {
  202. process.chdir("throw-module-paths");
  203. expect(() => {
  204. babel.transform("", {
  205. filename: "filename.js",
  206. babelrc: false,
  207. plugins: ["foo"],
  208. });
  209. }).toThrow(
  210. /Cannot find module 'babel-plugin-foo'.*\n- If you want to resolve "foo", use "module:foo"/,
  211. );
  212. });
  213. it("should throw about @babel usage for presets", function() {
  214. process.chdir("throw-babel-paths");
  215. expect(() => {
  216. babel.transform("", {
  217. filename: "filename.js",
  218. babelrc: false,
  219. presets: ["foo"],
  220. });
  221. }).toThrow(
  222. /Cannot find module 'babel-preset-foo'.*\n- Did you mean "@babel\/foo"\?/,
  223. );
  224. });
  225. it("should throw about @babel usage for plugins", function() {
  226. process.chdir("throw-babel-paths");
  227. expect(() => {
  228. babel.transform("", {
  229. filename: "filename.js",
  230. babelrc: false,
  231. plugins: ["foo"],
  232. });
  233. }).toThrow(
  234. /Cannot find module 'babel-plugin-foo'.*\n- Did you mean "@babel\/foo"\?/,
  235. );
  236. });
  237. it("should throw about passing a preset as a plugin", function() {
  238. process.chdir("throw-opposite-paths");
  239. expect(() => {
  240. babel.transform("", {
  241. filename: "filename.js",
  242. babelrc: false,
  243. presets: ["testplugin"],
  244. });
  245. }).toThrow(
  246. /Cannot find module 'babel-preset-testplugin'.*\n- Did you accidentally pass a preset as a plugin\?/,
  247. );
  248. });
  249. it("should throw about passing a plugin as a preset", function() {
  250. process.chdir("throw-opposite-paths");
  251. expect(() => {
  252. babel.transform("", {
  253. filename: "filename.js",
  254. babelrc: false,
  255. plugins: ["testpreset"],
  256. });
  257. }).toThrow(
  258. /Cannot find module 'babel-plugin-testpreset'.*\n- Did you accidentally pass a plugin as a preset\?/,
  259. );
  260. });
  261. it("should throw about missing presets", function() {
  262. process.chdir("throw-missing-paths");
  263. expect(() => {
  264. babel.transform("", {
  265. filename: "filename.js",
  266. babelrc: false,
  267. presets: ["foo"],
  268. });
  269. }).toThrow(/Cannot find module 'babel-preset-foo'/);
  270. });
  271. it("should throw about missing plugins", function() {
  272. process.chdir("throw-missing-paths");
  273. expect(() => {
  274. babel.transform("", {
  275. filename: "filename.js",
  276. babelrc: false,
  277. plugins: ["foo"],
  278. });
  279. }).toThrow(/Cannot find module 'babel-plugin-foo'/);
  280. });
  281. });