babel.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Basic smoke tests for @babel/standalone
  2. (process.env.TEST_TYPE === "cov" ? describe.skip : describe)(
  3. "@babel/standalone",
  4. () => {
  5. const Babel = require("../babel");
  6. it("handles the es2015-no-commonjs preset", () => {
  7. const output = Babel.transform('const getMessage = () => "Hello World"', {
  8. presets: ["es2015-no-commonjs"],
  9. }).code;
  10. expect(output).toBe(
  11. "var getMessage = function getMessage() {\n" +
  12. ' return "Hello World";\n' +
  13. "};",
  14. );
  15. });
  16. it("handles the es2015-loose preset", () => {
  17. const output = Babel.transform("class A {}", {
  18. sourceType: "script",
  19. presets: ["es2015-loose"],
  20. }).code;
  21. expect(output).toBe('var A = function A() {\n "use strict";\n};');
  22. });
  23. it("handles the typescript preset", () => {
  24. const output = Babel.transform("var a: string;", {
  25. presets: [["typescript", { allExtensions: true }]],
  26. }).code;
  27. expect(output).toBe("var a;");
  28. });
  29. it("handles the flow preset", () => {
  30. const output = Babel.transform("var a: string;", {
  31. presets: ["flow"],
  32. }).code;
  33. expect(output).toBe("var a;");
  34. });
  35. it("can translate simple ast", () => {
  36. const ast = {
  37. type: "Program",
  38. start: 0,
  39. end: 2,
  40. directives: [],
  41. body: [
  42. {
  43. type: "ExpressionStatement",
  44. start: 0,
  45. end: 1,
  46. expression: {
  47. type: "NumericLiteral",
  48. start: 0,
  49. end: 2,
  50. value: 42,
  51. raw: "42",
  52. },
  53. },
  54. ],
  55. sourceType: "script",
  56. };
  57. const output = Babel.transformFromAst(ast, "42", { presets: ["es2015"] })
  58. .code;
  59. expect(output).toBe("42;");
  60. });
  61. it("handles the react preset", () => {
  62. const output = Babel.transform(
  63. "const someDiv = <div>{getMessage()}</div>",
  64. {
  65. presets: ["react"],
  66. },
  67. ).code;
  68. expect(output).toBe(
  69. 'const someDiv = React.createElement("div", null, getMessage());',
  70. );
  71. });
  72. it("handles presets with options", () => {
  73. const output = Babel.transform("export let x", {
  74. presets: [["es2015", { modules: false }]],
  75. }).code;
  76. expect(output).toBe("export var x;");
  77. });
  78. it("handles specifying a plugin by name", () => {
  79. const output = Babel.transform('const getMessage = () => "Hello World"', {
  80. plugins: ["transform-arrow-functions"],
  81. }).code;
  82. // Transforms arrow syntax but NOT "const".
  83. expect(output).toBe(
  84. "const getMessage = function () {\n" +
  85. ' return "Hello World";\n' +
  86. "};",
  87. );
  88. });
  89. it("handles plugins with options", () => {
  90. const output = Babel.transform("`${x}`", {
  91. plugins: [["transform-template-literals", { loose: true }]],
  92. }).code;
  93. expect(output).toBe('"" + x;');
  94. });
  95. it("throws on invalid preset name", () => {
  96. expect(() =>
  97. Babel.transform("var foo", { presets: ["lolfail"] }),
  98. ).toThrow(/Invalid preset specified in Babel options: "lolfail"/);
  99. });
  100. it("throws on invalid plugin name", () => {
  101. expect(() =>
  102. Babel.transform("var foo", { plugins: ["lolfail"] }),
  103. ).toThrow(/Invalid plugin specified in Babel options: "lolfail"/);
  104. });
  105. describe("custom plugins and presets", () => {
  106. const lolizer = () => ({
  107. visitor: {
  108. Identifier(path) {
  109. path.node.name = "LOL";
  110. },
  111. },
  112. });
  113. it("allows custom plugins to be registered", () => {
  114. Babel.registerPlugin("lolizer", lolizer);
  115. const output = Babel.transform(
  116. "function helloWorld() { alert(hello); }",
  117. { plugins: ["lolizer"] },
  118. );
  119. expect(output.code).toBe(`function LOL() {
  120. LOL(LOL);
  121. }`);
  122. });
  123. it("allows custom presets to be registered", () => {
  124. Babel.registerPreset("lulz", { plugins: [lolizer] });
  125. const output = Babel.transform(
  126. "function helloWorld() { alert(hello); }",
  127. { presets: ["lulz"] },
  128. );
  129. expect(output.code).toBe(`function LOL() {
  130. LOL(LOL);
  131. }`);
  132. });
  133. });
  134. },
  135. );