-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.test.js
142 lines (118 loc) · 5.01 KB
/
lib.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
'use strict';
const babelCore = require('@babel/core');
const serializerPath = require('jest-serializer-path');
const preset = require('./lib');
const env = process.env.NODE_ENV;
expect.addSnapshotSerializer(serializerPath);
afterEach(() => {
process.env.NODE_ENV = env;
});
it('should have the right config with the default options', () => {
expect(preset()).toMatchSnapshot();
});
describe('modules', () => {
it('should set modules to false if options.modules is disabled', () => {
expect(preset(null, { modules: false })).toMatchSnapshot();
});
it('should set modules to false by default if BABEL_ENV is es', () => {
process.env.BABEL_ENV = 'es';
try {
expect(preset(null)).toMatchSnapshot();
} finally {
delete process.env.BABEL_ENV;
}
});
});
describe('react', () => {
it('should enable react plugins if options.react is enabled and enable development goodies', () => {
expect(preset(null, { react: true })).toMatchSnapshot();
});
it('should enable react optimizations if options.env is production', () => {
expect(preset(null, { env: 'production', react: true })).toMatchSnapshot();
});
});
describe('lodash', () => {
it('should disable the lodash plugin if options.lodash is disabled', () => {
expect(preset(null, { lodash: false })).toMatchSnapshot();
});
it('should pass any options to the lodash plugin', () => {
expect(preset(null, { lodash: { id: 'recompose' } })).toMatchSnapshot();
});
});
describe('namedDefaultExport & babel-plugin-add-module-exports', () => {
it('should enable add-module-exports by default', () => {
expect(preset(null)).toMatchSnapshot();
});
it('should enable add-module-exports if options.modules is commonjs', () => {
expect(preset(null)).toMatchSnapshot();
expect(preset(null, { modules: 'commonjs' })).toMatchSnapshot();
});
it('should not enable add-module-exports if options.modules is not commonjs', () => {
expect(preset(null, { modules: false })).toMatchSnapshot();
});
it('should respect options.namedDefaultExport', () => {
expect(preset(null, { namedDefaultExport: true })).toMatchSnapshot();
expect(preset(null, { modules: 'commonjs', namedDefaultExport: true })).toMatchSnapshot();
expect(preset(null, { modules: 'cjs', namedDefaultExport: true })).toMatchSnapshot();
expect(preset(null, { namedDefaultExport: false })).toMatchSnapshot();
expect(preset(null, { modules: 'commonjs', namedDefaultExport: false })).toMatchSnapshot();
expect(preset(null, { modules: 'cjs', namedDefaultExport: false })).toMatchSnapshot();
});
it('should throw when enabling options.namedDefaultExport and options.modules is not commonjs', () => {
expect(() => preset(null, { modules: false, namedDefaultExport: true }))
.toThrow('The `namedDefaultExport` option can only be enabled when `modules` is commonjs');
});
});
describe('dynamicImport', () => {
it('should respect options.modules and options.dynamicImport', () => {
expect(preset(null, { modules: 'commonjs', dynamicImport: true })).toMatchSnapshot();
expect(preset(null, { modules: 'cjs', dynamicImport: true })).toMatchSnapshot();
expect(preset(null, { modules: false, dynamicImport: true })).toMatchSnapshot();
expect(preset(null, { modules: 'foo', dynamicImport: true })).toMatchSnapshot();
expect(preset(null, { modules: 'commonjs', dynamicImport: false })).toMatchSnapshot();
expect(preset(null, { modules: 'cjs', dynamicImport: false })).toMatchSnapshot();
expect(preset(null, { modules: false, dynamicImport: false })).toMatchSnapshot();
expect(preset(null, { modules: 'foo', dynamicImport: true })).toMatchSnapshot();
});
});
describe('functional', () => {
describe('class-properties', () => {
it('should handle correctly', () => {
expect(
babelCore.transform(`
class Bork {
static a = 'foo';
static b;
x = 'bar';
y;
}
`, preset()).code
).toMatchSnapshot();
});
});
describe('optional-chaining', () => {
it('should handle correctly', () => {
expect(
babelCore.transform(`
const obj = {
foo: {
bar: {
baz: 42,
},
},
};
const baz = obj?.foo?.bar?.baz;
`, preset()).code
).toMatchSnapshot();
});
});
describe('nullish-coalescing-operator', () => {
it('should handle correctly', () => {
expect(
babelCore.transform(`
const foo = object.foo ?? 'bar';
`, preset()).code
).toMatchSnapshot();
});
});
});