-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
varia.test.js
145 lines (129 loc) · 4.92 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
/* 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('returns last coverage object', function (cb) {
var instrumenter = new Instrumenter({coverageVariable: '__testing_coverage__'}),
generated,
err,
cov;
instrumenter.instrument('output = args[0]',__filename, function (e, c) {
err = e;
generated = c;
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/));
});
});