-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
239 lines (194 loc) · 4.1 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
const babel = require('babel-core');
const plugin = require('./index');
console.profile = jest.fn();
console.profileEnd = jest.fn();
it('wraps a function in profile block', () => {
const example = `
const b = () => {
// profile
return 42;
}
const a = () => {
// profile
return b();
}
`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('does not wrap a function if comment does not exactly match', () => {
const example = `
const b = () => {
// wraps a profile
return 42;
}
const a = () => {
// bug in something else called profile
return b();
}
`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('caters to the case of multiple returns', () => {
const example = `
const a = () => {
// profile
if (true) {
return Math.random(12);
}
return Math.sqrt(10000000000000000000);
}`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('does not add profile statement if one is already present', () => {
const example = `
const b = () => {
// profile
console.profile('some name');
return 42;
console.profileEnd();
}`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('has multiple returns, one of them invalid', () => {
const example = `
const b = () => {
// profile
return 42;
return 42;
return 42;
}`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('has multiple comments', () => {
let example = `
const b = () => {
// profile
// profile
// profile
// profile
// profile
return 42;
}`;
let code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
example = `
const b = () => {
// profile
//
// profile
//
return 42;
// profile
//
// profile
}`;
code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('maintains indentation with nested functions', () => {
let example = `
const b = () => {
const a = () => {
return Math.sqrt(1000000000000);
}
// profile
return 42;
}`;
let code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
example = `
const b = () => {
const a = () => {
// profile
return Math.sqrt(1000000000000);
}
// profile
return a();
}`;
code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('maintains indentation when used with iterators', () => {
const example = `
const b = () => {
[1, 3, 5].map(item => {
return item;
})
// profile
return 42;
}`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
it('maintains indentation when for if blocks', () => {
const example = `
const b = () => {
// profile
if (true) {
return 12;
}
switch (true) {
case 'case1':
return 'what';
case 'case 2': {
return 'where';
}
case 'case 3':
return 'who';
default:
return 'default'
}
return 42;
};
`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
fit('usage inside if conditions', () => {
const example = `
const b = () => {
if (true) {
// profile
return 12;
}
switch (true) {
case 'case1':
return 'what';
case 'case 2': {
return 'where';
}
case 'case 3':
return 'who';
default:
return 'default'
}
return 42;
};
`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
describe('usage inside classes', () => {
it('works inside function properties', () => {
const example = `
class A {
changeVisualState (id) {
// profile
return item => {
this.setState({
newState: {}
});
callSomeMethod();
};
}
}
`;
const code = babel.transform(example, { plugins: [ plugin ] }).code;
expect(code).toMatchSnapshot();
});
});