-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
varia.test.js
241 lines (220 loc) · 7.83 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
/* globals describe, it */
import * as verifier from './util/verifier';
import Instrumenter from '../src/instrumenter';
import { assert } from 'chai';
describe('varia', function() {
it('debug/ walkDebug should not cause errors', function() {
var 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', function() {
var 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', function() {
var v = verifier.create('output = args[0];', { file: 'c:\\x\\y.js' }),
cov;
assert.ok(!v.err);
v.verify(['X'], 'X', {
lines: { 1: 1 },
statements: { 0: 1 }
});
cov = v.getCoverage();
assert.equal(Object.keys(cov)[0], 'c:\\x\\y.js');
});
it('preserves comments when requested', function() {
var v = verifier.create(
'/* hello */\noutput = args[0];',
{},
{ preserveComments: true }
),
code;
assert.ok(!v.err);
v.verify(['X'], 'X', {
lines: { 2: 1 },
statements: { 0: 1 }
});
code = v.getGeneratedCode();
assert.ok(code.match(/\/* hello */));
});
it('preserves function names for named export arrow functions', function() {
/* https://github.com/istanbuljs/babel-plugin-istanbul/issues/125 */
var v = verifier.create(
'export const func = () => true;',
{ generateOnly: true },
{ esModules: true }
),
code;
assert.ok(!v.err);
code = v.getGeneratedCode();
assert.ok(
code.match(/cov_(.+)\.s\[\d+\]\+\+;export const func=\(\)=>/)
);
});
it('honors ignore next for exported functions', function() {
/* https://github.com/istanbuljs/istanbuljs/issues/297 */
var v = verifier.create(
'/* istanbul ignore next*/ export function fn1() {}' +
'/* istanbul ignore next*/ export default function() {}',
{ generateOnly: true },
{ esModules: true }
),
code;
assert.ok(!v.err);
code = v.getGeneratedCode();
assert.ok(
code.match(
/}\(\);export function fn1\(\){}export default function\(\){}/
)
);
});
it('instruments exported functions', function() {
/* https://github.com/istanbuljs/istanbuljs/issues/297 */
var v = verifier.create(
'export function fn1() {}' + 'export default function() {}',
{ generateOnly: true },
{ esModules: true }
),
code;
assert.ok(!v.err);
code = v.getGeneratedCode();
assert.ok(
code.match(
/}\(\);export function fn1\(\){cov_(.+)\.f\[\d+\]\+\+;}export default function\(\){cov_(.+)\.f\[\d+\]\+\+;}/
)
);
});
it('returns last coverage object', function(cb) {
var instrumenter = new Instrumenter({
coverageVariable: '__testing_coverage__'
}),
err,
cov;
instrumenter.instrument('output = args[0]', __filename, function(e) {
err = e;
cov = instrumenter.lastFileCoverage();
assert.ok(!err);
assert.ok(cov);
cb();
});
});
it('creates a source-map when requested', function() {
var opts = {
produceSourceMap: true,
coverageVariable: '__testing_coverage__'
},
instrumenter = new Instrumenter(opts),
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', function() {
var f = '',
u = '',
fn = function(file, sourceMapUrl) {
f = file;
u = sourceMapUrl;
},
opts = {
sourceMapUrlCallback: fn,
coverageVariable: '__testing_coverage__'
},
instrumenter = new Instrumenter(opts),
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', function() {
it('allows filename to be optional', function(cb) {
var instrumenter = new Instrumenter({
coverageVariable: '__testing_coverage__'
}),
generated,
err;
instrumenter.instrument('output = args[0]', function(e, c) {
err = e;
generated = c;
assert.ok(!err);
assert.ok(generated);
cb();
});
});
it('returns instead of throwing errors', function() {
var instrumenter = new Instrumenter({
coverageVariable: '__testing_coverage__'
}),
generated = null,
err = null;
instrumenter.instrument('output = args[0] : 1: 2', function(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', function() {
var v = verifier.create(
'export class App extends Component {};',
{ generateOnly: true },
{ esModules: true }
),
code;
assert.ok(!v.err);
code = v.getGeneratedCode();
assert.ok(code.match(/cov_(.+);export class App extends/));
});
it('does not add extra parenthesis when superclass is an identifier', function() {
var v = verifier.create('class App extends Component {};', {
generateOnly: true
}),
code;
assert.ok(!v.err);
code = v.getGeneratedCode();
assert.ok(code.match(/cov_(.+);class App extends Component/));
});
it('can store coverage object in alternative scope', function() {
const opts = { generateOnly: true };
const instrumentOpts = { coverageGlobalScope: 'window.top' };
const v = verifier.create('console.log("test");', opts, instrumentOpts);
var code;
assert.ok(!v.err);
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', function() {
const opts = { generateOnly: true };
const instrumentOpts = {
coverageGlobalScope: 'window.top',
coverageGlobalScopeFunc: false
};
const v = verifier.create('console.log("test");', opts, instrumentOpts);
var code;
assert.ok(!v.err);
code = v.getGeneratedCode();
assert.ok(code.match(/global\s*=\s*window.top;/));
});
});