This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.js
278 lines (243 loc) · 6.24 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const {loadOptions, transformSync} = require('@babel/core')
const fail = process.stdout.isTTY ? '\x1b[0;31mx\x1b[0m' : 'x'
const pass = process.stdout.isTTY ? '\x1b[0;32m✔\x1b[0m' : '✔'
let exit = 0
const test = (name, pre, expected, options = { presets: ["./"] }) => {
try {
const actual = transformSync(pre, options).code
if (expected instanceof RegExp) {
if (!expected.test(actual)) throw new Error(`No match ${expected}:\n ${actual}`)
} else if (typeof expected === 'string') {
if (actual !== expected) throw new Error(`Actual: \`\`\`\n${actual}\n\`\`\`\nExpected: \`\`\`\n${expected}\n\`\`\``)
} else {
throw new Error(`Actual: \`\`\`\n${actual}\n\`\`\``)
}
console.log(`${pass} ${name}`)
} catch (e) {
console.log(`${fail} ${name}`)
console.log(e.stack)
exit = 1
}
}
const testFail = (name, pre, ctor, options = { presets: ["./"] }) => {
try {
const actual = transformSync(pre, options).code
const err = new Error(`Actual: \`\`\`\n${actual}\n\`\`\`\n`)
console.log(`${fail} ${name}`)
console.log(`Expected transform to fail with ${ctor.name}`)
console.log(err.stack)
exit = 1
} catch (err) {
if (!(err instanceof ctor)) {
console.log(`${fail} ${name}`)
console.log(`Expected ${ctor.name} but got ${err.constructor.name}`)
exit = 1
} else {
console.log(`${pass} ${name}`)
}
}
}
test(
'json-strings works',
`"a
b"`,
`"a\\u2028b";`
)
test(
'json-strings transform is disabled with env=false',
`"a
b"`,
`"a
b";`,
{ presets: [["./", { env: false }]]}
)
test(
'optional-catch works',
`try { throw '' } catch {}`,
`try {
throw '';
} catch (_unused) {}`,
)
test(
'optional-catch transform is disabled with env=false',
`try { throw '' } catch {}`,
`try {
throw '';
} catch {}`,
{ presets: [["./", { env: false }]]}
)
test(
'destructuring works',
`const { f } = { f: 1 }`,
`const _f = {
f: 1
},
f = _f.f;`
)
test(
'destructuring transform is disabled with env=false',
`const { f } = { f: 1 }`,
`const {
f
} = {
f: 1
};`,
{ presets: [["./", { env: false }]]}
)
test(
'import.meta works',
`import.meta.foo`,
`import.meta.foo;`
)
test(
'object rest is transformed',
`const { ...f } = {a:1}`,
/f = _extends\({}, /
)
test(
'object rest is transformed with env=false',
`const { ...f } = {a:1}`,
/f = _extends\({}, /,
{ presets: [["./", { env: false }]]}
)
test(
'object rest is transformed with mobile browsers',
`const { ...f } = {a:1}`,
/f = _extends\({}, /,
{ presets: [["./", { targets: { browsers: 'mobile' } }]]}
)
test(
'object rest is transformed with mobile browsers even with env=false',
`const { ...f } = {a:1}`,
/f = _extends\({}, /,
{ presets: [["./", { env: false, targets: { browsers: 'mobile' } }]]}
)
test(
'object rest will use builtins if useBuiltIns=true',
`const { ...f } = {a:1}`,
/f = Object.assign/,
{ presets: [["./", { useBuiltIns: true }]]}
)
test(
'object rest will use builtins if useBuiltIns=true with env=false',
`const { ...f } = {a:1}`,
/f = Object.assign/,
{ presets: [["./", { env: false, useBuiltIns: true }]]}
)
test(
'object rest wont be transformed given modern browser set',
`const { ...f } = {a:1}`,
`const { ...f\n} = {\n a: 1\n};`,
{ presets: [["./", { targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]}
)
test(
'object rest wont be transformed given modern browser set even with env=false',
`const { ...f } = {a:1}`,
`const { ...f\n} = {\n a: 1\n};`,
{ presets: [["./", { env: false, targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]}
)
test(
'object spread is transformed',
`const f = { ...a }`,
/f = _objectSpread\({}, /
)
test(
'object spread still works with env=false',
`const f = { ...a }`,
/f = _objectSpread\({}, /,
{ presets: [["./", { env: false }]]}
)
test(
'object spread still works if useBuiltIns=true',
`const f = { ...a }`,
/f = _objectSpread\({}, /,
{ presets: [["./", { useBuiltIns: true }]]}
)
test(
'object spread still works if env=false and useBuiltIns=true',
`const f = { ...a }`,
/f = _objectSpread\({}, /,
{ presets: [["./", { env: false, useBuiltIns: true }]]}
)
test(
'object spread wont be transformed given modern browser set',
`const f = { ...a }`,
`const f = { ...a\n};`,
{ presets: [["./", { targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]}
)
test(
'object spread wont be transformed given modern browser set even with env=false',
`const f = { ...a }`,
`const f = { ...a\n};`,
{ presets: [["./", { env: false, targets: { browsers: ['Chrome > 60', 'Safari >= 11.1'] }}]]}
)
test(
'dynamic import works',
`import('foo')`,
`import('foo');`
)
test(
'flow gets stripped',
`var a: string = 'hello'`,
`var a = 'hello';`
)
test(
'babel-plugin-transform-invariant works',
`invariant(false)`,
`invariant(false, "null.js:1");`
)
test(
'Imports are not transformed',
`import foo from "bar"`,
`import foo from "bar";`
)
test(
'Imports can be transformed setting `modules`',
`import {foo} from "bar"`,
`"use strict";
var _bar = require("bar");`,
{ presets: [["./", { modules: 'commonjs' }]]}
)
test(
'classes dont get transformed down on desktop',
`class Foo {}`,
`class Foo {}`,
)
test(
'classes get transformed with custom profile',
`class Foo {}`,
/_classCallCheck/,
{ presets: [["./", { targets: { browsers: 'IE 11' } }]]}
)
test(
'classes dont get transformed on mobile',
`class Foo {}`,
`class Foo {}`,
{ presets: [["./", { targets: { browsers: 'mobile' } }]]}
)
test(
'async does not get transformed on desktop',
`async function foo() {}`,
`async function foo() {}`,
)
test(
'async gets transformed with custom profile',
`async function foo() {}`,
/asyncGeneratorStep/,
{ presets: [["./", { targets: { browsers: 'IE 11' } }]]}
)
test(
'async does not get transformed on mobile',
`async function foo() {}`,
`async function foo() {}`,
{ presets: [["./", { targets: { browsers: 'mobile' } }]]}
)
testFail(
'class properties are not supported',
`class Foo { x = 1; }`,
SyntaxError
)
testFail(
'class properties are not supported on mobile',
`class Foo { x = 1; }`,
SyntaxError,
{ presets: [["./", { targets: { browsers: 'mobile' } }]]}
)
process.exit(exit)