-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
varia.test.js
273 lines (239 loc) · 8.41 KB
/
varia.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
/* globals describe, it */
import { assert } from 'chai';
import Instrumenter from '../src/instrumenter';
import * as verifier from './util/verifier';
describe('varia', () => {
it('debug/ walkDebug should not cause errors', () => {
const v = verifier.create('output = args[0];', {}, { debug: true });
assert.ok(!v.err);
v.verify(['X'], 'X', {
lines: { 1: 1 },
statements: { 0: 1 }
});
});
it('auto-generates filename', () => {
const v = verifier.create('output = args[0];', { file: null });
assert.ok(!v.err);
v.verify(['X'], 'X', {
lines: { 1: 1 },
statements: { 0: 1 }
});
});
it('handles windows-style paths in file names', () => {
const v = verifier.create('output = args[0];', { file: 'c:\\x\\y.js' });
assert.ok(!v.err);
v.verify(['X'], 'X', {
lines: { 1: 1 },
statements: { 0: 1 }
});
const cov = v.getCoverage();
assert.equal(Object.keys(cov)[0], 'c:\\x\\y.js');
});
it('preserves comments when requested', () => {
const v = verifier.create(
'/* hello */\noutput = args[0];',
{},
{ preserveComments: true }
);
assert.ok(!v.err);
v.verify(['X'], 'X', {
lines: { 2: 1 },
statements: { 0: 1 }
});
const code = v.getGeneratedCode();
assert.ok(code.match(/\/* hello */));
});
it('preserves function names for named export arrow functions', () => {
/* https://github.com/istanbuljs/babel-plugin-istanbul/issues/125 */
const v = verifier.create(
'export const func = () => true;',
{ generateOnly: true },
{ esModules: true }
);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(
code.match(/cov_(.+)\.s\[\d+\]\+\+;export const func=\(\)=>/)
);
});
it('honors ignore next for exported functions', () => {
/* https://github.com/istanbuljs/istanbuljs/issues/297 */
const v = verifier.create(
'/* istanbul ignore next*/ export function fn1() {}' +
'/* istanbul ignore next*/ export default function() {}',
{ generateOnly: true },
{ esModules: true, preserveComments: false }
);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(
code.match(
/return actualCoverage;}cov_[^(]+\(\);export function fn1\(\){}export default function\(\){}/
)
);
});
it('instruments exported functions', () => {
/* https://github.com/istanbuljs/istanbuljs/issues/297 */
const v = verifier.create(
'export function fn1() {}' + 'export default function() {}',
{ generateOnly: true },
{ esModules: true }
);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(
code.match(
/return actualCoverage;}cov_([^(]+)\(\);export function fn1\(\){cov_(.+)\.f\[\d+\]\+\+;}export default function\(\){cov_(.+)\.f\[\d+\]\+\+;}/
)
);
});
it('returns last coverage object', cb => {
const instrumenter = new Instrumenter({
coverageVariable: '__testing_coverage__'
});
let err;
let cov;
instrumenter.instrument('output = args[0]', __filename, e => {
err = e;
cov = instrumenter.lastFileCoverage();
assert.ok(!err);
assert.ok(cov);
cb();
});
});
it('creates a source-map when requested', () => {
const opts = {
produceSourceMap: true,
coverageVariable: '__testing_coverage__'
};
const instrumenter = new Instrumenter(opts);
const generated = instrumenter.instrumentSync(
'output = args[0]',
__filename
);
assert.ok(generated);
assert.ok(typeof generated === 'string');
assert.ok(instrumenter.lastSourceMap());
});
it('registers source map URLs seen in the original source', () => {
let f = '';
let u = '';
const fn = function(file, sourceMapUrl) {
f = file;
u = sourceMapUrl;
};
const opts = {
sourceMapUrlCallback: fn,
coverageVariable: '__testing_coverage__'
};
const instrumenter = new Instrumenter(opts);
const generated = instrumenter.instrumentSync(
'/* foobar */ output = args[0]\n// @sourceMappingURL=foo.map',
__filename
);
assert.ok(generated);
assert.equal(f, __filename);
assert.equal(u, 'foo.map');
});
describe('callback style instrumentation', () => {
it('allows filename to be optional', cb => {
const instrumenter = new Instrumenter({
coverageVariable: '__testing_coverage__'
});
let generated;
let err;
instrumenter.instrument('output = args[0]', (e, c) => {
err = e;
generated = c;
assert.ok(!err);
assert.ok(generated);
cb();
});
});
it('returns instead of throwing errors', () => {
const instrumenter = new Instrumenter({
coverageVariable: '__testing_coverage__'
});
let generated = null;
let err = null;
instrumenter.instrument('output = args[0] : 1: 2', (e, c) => {
err = e;
generated = c;
});
assert.ok(err);
assert.ok(!generated);
});
});
// see: https://github.com/istanbuljs/istanbuljs/issues/110
// TODO: it feels like we should be inserting line counters
// for class exports and class declarations.
it('properly exports named classes', () => {
const v = verifier.create(
'export class App extends Component {};',
{ generateOnly: true },
{ esModules: true }
);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(
code.match(
/return actualCoverage;}cov_[^(]+\(\);export class App extends/
)
);
});
it('declares Function when needed', () => {
const v = verifier.create(
'function Function() {}',
{ generateOnly: true },
{ esModules: true }
);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(code.match(/var Function\s*=/));
});
it('does not declare Function when not needed', () => {
const v = verifier.create(
'function differentFunction() {}',
{ generateOnly: true },
{ esModules: true }
);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(!code.match(/var Function\s*=/));
});
it('does not add extra parenthesis when superclass is an identifier', () => {
const v = verifier.create('class App extends Component {};', {
generateOnly: true
});
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(
code.match(
/return actualCoverage;}cov_[^(]+\(\);class App extends Component/
)
);
});
it('can store coverage object in alternative scope', () => {
const opts = { generateOnly: true };
const instrumentOpts = { coverageGlobalScope: 'window.top' };
const v = verifier.create('console.log("test");', opts, instrumentOpts);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(
code.match(
/global\s*=\s*\(*new\s*Function\(['"]return\s*window.top['"]\)\)*\(\)/
)
);
});
it('can store coverage object in alternative scope without function', () => {
const opts = { generateOnly: true };
const instrumentOpts = {
coverageGlobalScope: 'window.top',
coverageGlobalScopeFunc: false
};
const v = verifier.create('console.log("test");', opts, instrumentOpts);
assert.ok(!v.err);
const code = v.getGeneratedCode();
assert.ok(code.match(/global\s*=\s*window.top;/));
});
});