Debian Codegraph @

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

raw symbols SCIP
1 import * as babel from "@babel/core";
2 import definePolyfillProvider from "../lib";
3
4 function transform(code, opts, provider) {
5 return babel.transformSync(code, {
6 configFile: false,
7 plugins: [[definePolyfillProvider(provider), opts]],
8 });
9 }
10
11 describe("options", () => {
12 it("must be non-empty", () => {
13 expect(() => transform("code", {}, () => {})).toThrow(/requires/);
14 });
15 });
16
17 function withMethod(method) {
18 return transform("code", { method }, () => {});
19 }
20
21 describe("method", () => {
22 it("must be a string", () => {
23 expect(() => withMethod({})).toThrow(/string/);
24 });
25
26 it("throws for unexpected values", () => {
27 expect(() => withMethod("foo")).toThrow(
28 /entry-global.*usage-global.*usage-pure/,
29 );
30 });
31 });
32
33 function extractTargets(targets) {
34 let resolved;
35 transform("code", { method: "usage-global", targets }, ({ targets }) => {
36 resolved = targets;
37 return {
38 usageGlobal() {},
39 };
40 });
41 return resolved;
42 }
43
44 describe("targets", () => {
45 it("accepts a string", () => {
46 const targets = extractTargets("firefox 67, ie 11");
47
48 expect(targets).toEqual({ firefox: "67.0.0", ie: "11.0.0" });
49 });
50
51 it("accepts an array", () => {
52 const targets = extractTargets(["firefox 67", "ie 11"]);
53
54 expect(targets).toEqual({ firefox: "67.0.0", ie: "11.0.0" });
55 });
56
57 it("accepts an object", () => {
58 const targets = extractTargets({ firefox: "67", ie: "11" });
59
60 expect(targets).toEqual({ firefox: "67.0.0", ie: "11.0.0" });
61 });
62 });
63
64 describe("shouldInjectPolyfill", () => {
65 it("is not compatible with .include", () => {
66 expect(() =>
67 transform(
68 "code",
69 {
70 method: "usage-global",
71 include: ["foo"],
72 shouldInjectPolyfill() {},
73 },
74 () => ({}),
75 ),
76 ).toThrow(/include.*shouldInjectPolyfill/);
77 });
78
79 it("is not compatible with .exclude", () => {
80 expect(() =>
81 transform(
82 "code",
83 {
84 method: "usage-global",
85 exclude: ["foo"],
86 shouldInjectPolyfill() {},
87 },
88 () => ({}),
89 ),
90 ).toThrow(/exclude.*shouldInjectPolyfill/);
91 });
92
93 it("should receive the correct default shouldInject (true)", () => {
94 let args;
95
96 transform(
97 "code",
98 {
99 method: "usage-global",
100 targets: { chrome: 50 },
101 shouldInjectPolyfill(...xs) {
102 args = xs;
103 return true;
104 },
105 },
106 ({ shouldInjectPolyfill }) => ({
107 polyfills: {
108 foo: { chrome: 60 },
109 },
110 usageGlobal() {
111 shouldInjectPolyfill("foo");
112 },
113 }),
114 );
115
116 expect(args).toEqual(["foo", true]);
117 });
118
119 it("should receive the correct default shouldInject (false)", () => {
120 let args;
121
122 transform(
123 "code",
124 {
125 method: "usage-global",
126 targets: { chrome: 50 },
127 shouldInjectPolyfill(...xs) {
128 args = xs;
129 return true;
130 },
131 },
132 ({ shouldInjectPolyfill }) => ({
133 polyfills: {
134 foo: { chrome: 40 },
135 },
136 usageGlobal() {
137 shouldInjectPolyfill("foo");
138 },
139 }),
140 );
141
142 expect(args).toEqual(["foo", false]);
143 });
144
145 it("can override the default shouldInject (true -> false)", () => {
146 let result;
147
148 transform(
149 "code",
150 {
151 method: "usage-global",
152 targets: { chrome: 50 },
153 shouldInjectPolyfill() {
154 return false;
155 },
156 },
157 ({ shouldInjectPolyfill }) => ({
158 polyfills: {
159 foo: { chrome: 60 },
160 },
161 usageGlobal() {
162 result = shouldInjectPolyfill("foo");
163 },
164 }),
165 );
166
167 expect(result).toBe(false);
168 });
169
170 it("can override the default shouldInject (false -> true)", () => {
171 let result;
172
173 transform(
174 "code",
175 {
176 method: "usage-global",
177 targets: { chrome: 50 },
178 shouldInjectPolyfill() {
179 return true;
180 },
181 },
182 ({ shouldInjectPolyfill }) => ({
183 polyfills: {
184 foo: { chrome: 40 },
185 },
186 usageGlobal() {
187 result = shouldInjectPolyfill("foo");
188 },
189 }),
190 );
191
192 expect(result).toBe(true);
193 });
194 });
195
196 function withAbsoluteImports(absoluteImports) {
197 let resolved;
198 transform("code", { method: "usage-global", absoluteImports }, () => ({
199 usageGlobal() {},
200 }));
201 return resolved;
202 }
203
204 describe("absoluteImports", () => {
205 it("can be a boolean or a string", () => {
206 expect(() => withAbsoluteImports(false)).not.toThrow();
207 expect(() => withAbsoluteImports(true)).not.toThrow();
208 expect(() => withAbsoluteImports("./foo/bar")).not.toThrow();
209 });
210 });