1 | process.env.NODE_ENV = 'production';
|
2 |
|
3 | const { transformAsync } = require('@babel/core');
|
4 |
|
5 | const fixture = `
|
6 | const myVar = new Map([]);
|
7 | class TestClass {
|
8 | static staticProp = 'static prop';
|
9 | prop = 'prop';
|
10 |
|
11 | get getterProp() {
|
12 | return 'getter prop';
|
13 | }
|
14 | }
|
15 |
|
16 | const objectA = {a: 'a', b: 'b'};
|
17 |
|
18 | const d = 'd';
|
19 | const objectB = {...objectA, c: 'c', [d]: 'd'};
|
20 |
|
21 | function test(firstArg, ...args) {
|
22 | const [,test2] = args;
|
23 | console.log(firstArg, args[0], test2);
|
24 | }
|
25 |
|
26 | const testAsync = async () => {
|
27 | await 'test';
|
28 | }
|
29 | `;
|
30 |
|
31 | describe('web preset', () => {
|
32 | it('should match the snapshot', async () => {
|
33 | const { code } = await transformAsync(fixture, {
|
34 | babelrc: false,
|
35 | presets: [[require('.'), { debug: false }]],
|
36 | });
|
37 |
|
38 | return expect(code).toMatchSnapshot();
|
39 | });
|
40 |
|
41 | it('should allow for corejs override', async () => {
|
42 | const { code } = await transformAsync(fixture, {
|
43 | babelrc: false,
|
44 | presets: [[require('.'), { debug: false, corejs: 2 }]],
|
45 | });
|
46 |
|
47 | return expect(code).toMatchSnapshot();
|
48 | });
|
49 |
|
50 | it('should allow for cjs modules', async () => {
|
51 | const { code } = await transformAsync(fixture, {
|
52 | babelrc: false,
|
53 | presets: [[require('.'), { debug: false, modules: 'commonjs' }]],
|
54 | });
|
55 |
|
56 | return expect(code).toMatchSnapshot();
|
57 | });
|
58 |
|
59 | it('should do the __DEV__ dev expression', async () => {
|
60 | const { code } = await transformAsync(
|
61 | `
|
62 | if (__DEV__) {
|
63 | console.log('test');
|
64 | }
|
65 | `,
|
66 | {
|
67 | babelrc: false,
|
68 | presets: [[require('.'), { debug: false }]],
|
69 | },
|
70 | );
|
71 |
|
72 | return expect(code).toMatchSnapshot();
|
73 | });
|
74 |
|
75 | it('should do the invariant dev expression', async () => {
|
76 | const { code } = await transformAsync(
|
77 | `
|
78 | invariant(test === true, "some error");
|
79 | `,
|
80 | {
|
81 | babelrc: false,
|
82 | presets: [[require('.'), { debug: false }]],
|
83 | },
|
84 | );
|
85 |
|
86 | return expect(code).toMatchSnapshot();
|
87 | });
|
88 |
|
89 | it('should do the warning dev expression', async () => {
|
90 | const { code } = await transformAsync(
|
91 | `
|
92 | warning(test === true, "some error");
|
93 | `,
|
94 | {
|
95 | babelrc: false,
|
96 | presets: [[require('.'), { debug: false }]],
|
97 | },
|
98 | );
|
99 |
|
100 | return expect(code).toMatchSnapshot();
|
101 | });
|
102 |
|
103 | it('should optimize clsx', async () => {
|
104 | const { code } = await transformAsync(
|
105 | `
|
106 | import clsx from 'clsx';
|
107 |
|
108 | const validCondition = true;
|
109 | const invalidCondition = false;
|
110 | const a = 'a-class';
|
111 | const b = 'b-class';
|
112 | const d = 'd-class';
|
113 |
|
114 | clsx('a');
|
115 | clsx(a);
|
116 | clsx(['a', b]);
|
117 | clsx(a, 'b', ['c', d]);
|
118 | clsx(a, 'b', ['c', d], 'e');
|
119 | clsx(a, {
|
120 | ['b']: true,
|
121 | ['c']: false,
|
122 | ['e']: someCondition,
|
123 | f: true,
|
124 | g: false,
|
125 | h: someCondition,
|
126 | i: validCondition,
|
127 | j: invalidCondition
|
128 | });
|
129 | clsx(validCondition && 'a');
|
130 | clsx({
|
131 | btn: true,
|
132 | 'btn-foo': isDisabled,
|
133 | 'btn-bar': !isDisabled,
|
134 | });
|
135 | `,
|
136 | {
|
137 | babelrc: false,
|
138 | presets: [[require('.'), { debug: false }]],
|
139 | },
|
140 | );
|
141 |
|
142 | return expect(code).toMatchSnapshot();
|
143 | });
|
144 | });
|