Debian Codegraph @

babel-preset-modules/test/browser/index.js

raw symbols SCIP
1 import asyncArrows from "fixture!./fixtures/async-arrows";
2 import awaitNegation from "fixture!./fixtures/await-negation";
3 import functionName from "fixture!./fixtures/function-name";
4 import defaultParams from "fixture!./fixtures/default-params";
5 import defaultParamsArrow from "fixture!./fixtures/default-params-arrow";
6
7 /* eslint-disable no-unused-vars */
8
9 const cjs = code =>
10 `var module={},exports=module.exports={},require=function(){};\n${code}\nmodule.exports`;
11
12 function run(code, exportName = "default") {
13 let exports;
14 expect(() => {
15 exports = eval(cjs(code));
16 }).not.toThrow();
17 expect(Object.keys(exports)).toContain(exportName);
18 return exports[exportName];
19 }
20
21 describe("Tagged Templates", () => {
22 it("should cache template strings", () => {
23 const myTag = s => s;
24
25 function a() {
26 return myTag`a`;
27 }
28
29 function b() {
30 return myTag`a`;
31 }
32
33 // Also make sure the transform got applied.
34 expect(Function.prototype.toString.call(a)).not.toMatch(/myTag`/);
35
36 expect(a()).toBe(a());
37 expect(a()).toBe(new a());
38 expect(b()).toBe(new b());
39 expect(a()).not.toBe(b());
40 });
41 });
42
43 describe("class arrows", () => {
44 it("should allow async arrows in classes", async () => {
45 const X = run(asyncArrows);
46 expect(typeof X).toBe("function");
47 let x, fn, a;
48 expect(() => {
49 x = new X();
50 fn = x.a();
51 a = fn();
52 }).not.toThrow();
53 expect(await a).toBe(x.a);
54 });
55 });
56
57 describe("await negation", () => {
58 it("should be wrapped in parens", async () => {
59 expect(awaitNegation).toMatch(/!\(await/);
60 });
61
62 it("should execute successfully", async () => {
63 const x = run(awaitNegation);
64 expect(typeof x).toBe("function");
65
66 let p;
67 expect(() => {
68 p = x();
69 }).not.toThrow();
70
71 expect(await p).toBe(false);
72 });
73 });
74
75 describe("default-params", () => {
76 it("should support default destructured parameters in functions", async () => {
77 const foo = run(defaultParams, "foo");
78 expect(typeof foo).toBe("function");
79
80 let p;
81 expect(() => {
82 p = foo(undefined, {}, {});
83 }).not.toThrow();
84 expect(p).toEqual([1, 2, 3]);
85
86 expect(() => {
87 p = foo(4, { b: 5 }, { c: 6 });
88 }).not.toThrow();
89 expect(p).toEqual([4, 5, 6]);
90 });
91
92 it("should support default destructured parameters in arrow functions", async () => {
93 const foo = run(defaultParamsArrow, "foo");
94 expect(typeof foo).toBe("function");
95
96 let p;
97 expect(() => {
98 p = foo(undefined, {}, {});
99 }).not.toThrow();
100 expect(p).toEqual([1, 2, 3]);
101
102 expect(() => {
103 p = foo(4, { b: 5 }, { c: 6 });
104 }).not.toThrow();
105 expect(p).toEqual([4, 5, 6]);
106 });
107 });
108
109 describe("function-name", () => {
110 let test;
111 it("should execute", async () => {
112 test = run(functionName);
113 });
114 it("should infer .name for function expressions in variable declarations", async () => {
115 const [expression] = test;
116 expect(typeof expression).toBe("function");
117 expect(expression.name).toBe("foo");
118 });
119 it("should infer .name for arrow functions in variable declarations", async () => {
120 const [, arrow] = test;
121 expect(typeof arrow).toBe("function");
122 expect(arrow.name).toBe("bar");
123 });
124 });