Debian Codegraph @

packages/babel-helper-define-polyfill-provider/test/methods.js

raw symbols SCIP
1 import * as babel from "@babel/core";
2 import definePolyfillProvider from "../lib";
3
4 function transform(code, method, obj) {
5 return babel.transformSync(code, {
6 configFile: false,
7 plugins: [[definePolyfillProvider(() => obj), { method }]],
8 });
9 }
10
11 describe("entry-global", () => {
12 it("is a valid method", () => {
13 expect(() =>
14 transform("", "entry-global", { entryGlobal() {} }),
15 ).not.toThrow();
16 });
17
18 it("calls the entryGlobal function", () => {
19 const entryGlobal = jest.fn();
20 const usageGlobal = jest.fn();
21 const usagePure = jest.fn();
22
23 transform(`import "polyfill";`, "entry-global", {
24 entryGlobal,
25 usageGlobal,
26 usagePure,
27 });
28
29 expect(entryGlobal).toHaveBeenCalled();
30 expect(usageGlobal).not.toHaveBeenCalled();
31 expect(usagePure).not.toHaveBeenCalled();
32 });
33
34 it("not supported", () => {
35 expect(() => {
36 transform("", "entry-global", { usageGlobal() {}, usagePure() {} });
37 }).toThrow(/support/);
38 });
39
40 it("import declaration", () => {
41 const entryGlobal = jest.fn();
42
43 transform(`import "my-polyfill";`, "entry-global", { entryGlobal });
44
45 expect(entryGlobal).toHaveBeenCalledWith(
46 expect.objectContaining({ kind: "import" }),
47 expect.anything(), // utils
48 expect.anything(), // path
49 );
50 });
51
52 it("require call", () => {
53 const entryGlobal = jest.fn();
54
55 transform(`require("my-polyfill");`, "entry-global", { entryGlobal });
56
57 expect(entryGlobal).toHaveBeenCalledWith(
58 expect.objectContaining({ kind: "import" }),
59 expect.anything(), // utils
60 expect.anything(), // path
61 );
62 });
63
64 it("ignores global", () => {
65 const entryGlobal = jest.fn();
66
67 transform(`Promise;`, "entry-global", { entryGlobal });
68
69 expect(entryGlobal).not.toHaveBeenCalled();
70 });
71
72 it("ignores property", () => {
73 const entryGlobal = jest.fn();
74
75 transform(`arr.includes();`, "entry-global", { entryGlobal });
76
77 expect(entryGlobal).not.toHaveBeenCalled();
78 });
79 });
80
81 describe("usage-global", () => {
82 it("is a valid method", () => {
83 expect(() =>
84 transform("", "usage-global", { usageGlobal() {} }),
85 ).not.toThrow();
86 });
87
88 it("calls the usageGlobal function", () => {
89 const entryGlobal = jest.fn();
90 const usageGlobal = jest.fn();
91 const usagePure = jest.fn();
92
93 transform(`foo;`, "usage-global", { entryGlobal, usageGlobal, usagePure });
94
95 expect(entryGlobal).not.toHaveBeenCalled();
96 expect(usageGlobal).toHaveBeenCalled();
97 expect(usagePure).not.toHaveBeenCalled();
98 });
99
100 it("not supported", () => {
101 expect(() => {
102 transform("", "usage-global", { entryGlobal() {}, usagePure() {} });
103 }).toThrow(/support/);
104 });
105
106 it("global", () => {
107 const usageGlobal = jest.fn();
108
109 transform(`Promise;`, "usage-global", { usageGlobal });
110
111 expect(usageGlobal).toHaveBeenCalledWith(
112 expect.objectContaining({ kind: "global" }),
113 expect.anything(), // utils
114 expect.anything(), // path
115 );
116 });
117
118 it("member expression", () => {
119 const usageGlobal = jest.fn();
120
121 transform(`arr.includes();`, "usage-global", { usageGlobal });
122
123 expect(usageGlobal).toHaveBeenCalledWith(
124 expect.objectContaining({ kind: "property" }),
125 expect.anything(), // utils
126 expect.anything(), // path
127 );
128 });
129
130 it("ignores namespaces", () => {
131 const usageGlobal = jest.fn();
132
133 const source = `
134 import * as bar from "bar";
135 bar.map();`;
136
137 transform(source, "usage-global", { usageGlobal });
138
139 expect(usageGlobal).not.toHaveBeenCalled();
140 });
141
142 it("destructuring", () => {
143 const usageGlobal = jest.fn();
144
145 transform(`var { includes } = arr;`, "usage-global", { usageGlobal });
146
147 expect(usageGlobal).toHaveBeenCalledWith(
148 expect.objectContaining({ kind: "property" }),
149 expect.anything(), // utils
150 expect.anything(), // path
151 );
152 });
153
154 it("ignores import declaration", () => {
155 const usageGlobal = jest.fn();
156
157 transform(`import "my-polyfill";`, "usage-global", { usageGlobal });
158
159 expect(usageGlobal).not.toHaveBeenCalled();
160 });
161
162 it("called on the require id for require calls", () => {
163 const usageGlobal = jest.fn();
164
165 transform(`require("my-polyfill");`, "usage-global", { usageGlobal });
166
167 expect(usageGlobal).toHaveBeenCalledWith(
168 expect.objectContaining({ kind: "global" }),
169 expect.anything(), // utils
170 expect.objectContaining({ type: "Identifier" }), // path
171 );
172 });
173 });
174
175 describe("usage-pure", () => {
176 it("is a valid method", () => {
177 expect(() => transform("", "usage-pure", { usagePure() {} })).not.toThrow();
178 });
179
180 it("calls the usagePure function", () => {
181 const entryGlobal = jest.fn();
182 const usageGlobal = jest.fn();
183 const usagePure = jest.fn();
184
185 transform(`foo;`, "usage-pure", { entryGlobal, usageGlobal, usagePure });
186
187 expect(entryGlobal).not.toHaveBeenCalled();
188 expect(usageGlobal).not.toHaveBeenCalled();
189 expect(usagePure).toHaveBeenCalled();
190 });
191
192 it("not supported", () => {
193 expect(() => {
194 transform("", "usage-pure", { entryGlobal() {}, usageGlobal() {} });
195 }).toThrow(/support/);
196 });
197
198 it("global", () => {
199 const usagePure = jest.fn();
200
201 transform(`Promise;`, "usage-pure", { usagePure });
202
203 expect(usagePure).toHaveBeenCalledWith(
204 expect.objectContaining({ kind: "global" }),
205 expect.anything(), // utils
206 expect.anything(), // path
207 );
208 });
209
210 it("member expression", () => {
211 const usagePure = jest.fn();
212
213 transform(`arr.includes();`, "usage-pure", { usagePure });
214
215 expect(usagePure).toHaveBeenCalledWith(
216 expect.objectContaining({ kind: "property" }),
217 expect.anything(), // utils
218 expect.anything(), // path
219 );
220 });
221
222 it("ignores namespaces", () => {
223 const usagePure = jest.fn();
224
225 const source = `
226 import * as bar from "bar";
227 bar.map();`;
228
229 transform(source, "usage-pure", { usagePure });
230
231 expect(usagePure).not.toHaveBeenCalled();
232 });
233
234 it("destructuring", () => {
235 const usagePure = jest.fn();
236
237 transform(`var { includes } = arr;`, "usage-pure", { usagePure });
238
239 expect(usagePure).toHaveBeenCalledWith(
240 expect.objectContaining({ kind: "property" }),
241 expect.anything(), // utils
242 expect.anything(), // path
243 );
244 });
245
246 it("ignores import declaration", () => {
247 const usagePure = jest.fn();
248
249 transform(`import "my-polyfill";`, "usage-pure", { usagePure });
250
251 expect(usagePure).not.toHaveBeenCalled();
252 });
253
254 it("called on the require id for require calls", () => {
255 const usagePure = jest.fn();
256
257 transform(`require("my-polyfill");`, "usage-pure", { usagePure });
258
259 expect(usagePure).toHaveBeenCalledWith(
260 expect.objectContaining({ kind: "global" }),
261 expect.anything(), // utils
262 expect.objectContaining({ type: "Identifier" }), // path
263 );
264 });
265 });