-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.ts
52 lines (43 loc) · 2.1 KB
/
test.ts
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
import * as fs from 'fs'
import {test, expect} from '@jest/globals'
import {transformSync} from '@babel/core'
import preset from './index'
// the 'test' environment is excluded because it targets the currently running version of Node
// since our CI runs multiple Node versions, a snapshot for 'test' might not match one of them
const NON_TEST_ENVIRONMENTS = ['development', 'production', 'esm', 'cjs']
function macro(title: string, fixture: string, presetOptions = {}, topLevelOptions = {}): void {
const presetOptionsString = JSON.stringify(presetOptions)
const topLevelOptionsString = JSON.stringify(topLevelOptions)
test(`${title} given ${presetOptionsString}, ${topLevelOptionsString}`, () => {
const file = `${__dirname}/fixtures/${fixture}`
const input = fs.readFileSync(file, 'utf8')
expect(input).toMatchSnapshot('input')
NON_TEST_ENVIRONMENTS.forEach((envName) => {
const result = transformSync(input, {
envName,
presets: [[preset, presetOptions]],
filename: `/${fixture}`,
babelrc: false,
...topLevelOptions,
})
expect(result?.code).toMatchSnapshot(`output (${envName})`)
})
})
}
macro('transpiles ES2020+ syntax', 'syntax.js')
macro('transpiles ES2020+ syntax', 'syntax.js', undefined, {targets: 'Chrome 106'})
macro('transpiles ES2020+ syntax', 'syntax.js', {runtime: false})
macro('transpiles ES2020+ syntax', 'syntax.js', {modules: 'commonjs'})
macro('transpiles ES2020+ syntax', 'syntax.js', undefined, {
assumptions: {enumerableModuleMeta: true},
})
macro('transpiles React', 'react.js')
macro('transpiles TSX', 'typescript.tsx')
macro('transpiles Emotion', 'emotion.js', {emotion: true})
macro('replaces generic polyfill with env-targeted polyfills', 'polyfills.js')
macro('replaces generic polyfill with env-targeted polyfills', 'polyfills.js', undefined, {
targets: 'Chrome 106',
})
macro('transpiles ESM in node_modules', 'vendor/node_modules/my-pkg/index.js')
macro('transpiles CJS in node_modules', 'vendor/node_modules/my-pkg/cjs.js')
macro('avoids transpiling known precompiled packages', 'vendor/node_modules/react/index.js')