/
babel-plugin-istanbul.js
214 lines (192 loc) · 6.7 KB
/
babel-plugin-istanbul.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
/* eslint-env mocha */
import * as babel from '@babel/core'
import makeVisitor from '../src'
import path from 'path'
require('chai').should()
describe('babel-plugin-istanbul', function () {
context('Babel plugin config', function () {
it('should instrument file if shouldSkip returns false', function () {
var result = babel.transformFileSync('./fixtures/plugin-should-cover.js', {
plugins: [
[makeVisitor, {
include: ['fixtures/plugin-should-cover.js']
}]
]
})
result.code.should.match(/statementMap/)
})
it('should not instrument file if shouldSkip returns true', function () {
var result = babel.transformFileSync('./fixtures/plugin-should-not-cover.js', {
plugins: [
[makeVisitor, {
include: ['fixtures/plugin-should-cover.js']
}]
]
})
result.code.should.not.match(/statementMap/)
})
context('local node_modules', function () {
it('should instrument file if shouldSkip returns false', function () {
var result = babel.transformFileSync('./fixtures/node_modules/should-cover.js', {
plugins: [
[makeVisitor({ types: babel.types }), {
excludeNodeModules: false,
exclude: ['node_modules/**'],
include: ['fixtures/node_modules/should-cover.js']
}]
]
})
result.code.should.match(/statementMap/)
})
it('should not instrument file if shouldSkip returns true', function () {
var result = babel.transformFileSync('./fixtures/node_modules/should-not-cover.js', {
plugins: [
[makeVisitor({ types: babel.types }), {
include: ['fixtures/node_modules/should-not-cover.js']
}]
]
})
result.code.should.not.match(/statementMap/)
})
})
it('should call onCover callback', function () {
var args
babel.transformFileSync('./fixtures/plugin-should-cover.js', {
plugins: [
[makeVisitor, {
onCover: function () {
args = [].slice.call(arguments)
},
include: ['fixtures/plugin-should-cover.js']
}]
]
})
args[0].should.equal(path.resolve('./fixtures/plugin-should-cover.js'))
args[1].statementMap.should.exist // eslint-disable-line
})
})
context('source maps', function () {
it('should use inline source map', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor, {
include: ['fixtures/has-inline-source-map.js']
}]
]
})
result.code.should.match(/inputSourceMap/)
})
it('should not use inline source map if inputSourceMap is set to false', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor, {
include: ['fixtures/has-inline-source-map.js'],
useInlineSourceMaps: false
}]
]
})
result.code.should.not.match(/inputSourceMap/)
})
it('should use provided source map', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor, {
include: ['fixtures/has-inline-source-map.js'],
inputSourceMap: { asdfQwer: 'foo' }
}]
]
})
result.code.should.match(/inputSourceMap:\s*{\s*asdfQwer: "foo"\s*}/)
})
})
context('package.json "nyc" config', function () {
context('process.env.NYC_CONFIG is set', function () {
it('should instrument file if shouldSkip returns false', function () {
var result = babel.transformFileSync('./fixtures/should-cover.js', {
plugins: [
makeVisitor
]
})
result.code.should.match(/statementMap/)
})
it('should not instrument file if shouldSkip returns true', function () {
var result = babel.transformFileSync('./fixtures/should-not-cover.js', {
plugins: [
makeVisitor
]
})
result.code.should.not.match(/statementMap/)
})
})
context('process.env.NYC_CONFIG is not set', function () {
const OLD_NYC_CONFIG = process.env.NYC_CONFIG
before(() => {
delete process.env.NYC_CONFIG
})
after(() => {
process.env.NYC_CONFIG = OLD_NYC_CONFIG
})
it('should instrument file if shouldSkip returns false', function () {
var result = babel.transformFileSync('./fixtures/should-cover.js', {
plugins: [
makeVisitor
]
})
result.code.should.match(/statementMap/)
})
it('should not instrument file if shouldSkip returns true', function () {
var result = babel.transformFileSync('./fixtures/should-not-cover.js', {
plugins: [
makeVisitor
]
})
result.code.should.not.match(/statementMap/)
})
})
})
// TODO: setup istanbul-lib-instrument, such that we can
// run various babel configurations.
context('regression tests', () => {
// regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/78
it('should instrument: export const foo = () => {}', function () {
var result = babel.transformFileSync('./fixtures/issue-78.js', {
plugins: [
[makeVisitor, {
include: ['fixtures/issue-78.js']
}]
]
})
result.code.match(/statementMap/)
})
// regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/201
it('should not conflict with transform-modules-commonjs', function () {
var result = babel.transformFileSync('./fixtures/issue-201.js', {
plugins: [
[makeVisitor, {
include: ['fixtures/issue-201.js']
}],
'@babel/plugin-transform-modules-commonjs'
]
})
result.code.should.match(/_path.*\.resolve\)\(_path\)/)
result.code.should.not.match(/_path\.resolve\)\(_path\)/)
})
it('should respect a changed cwd in options', function () {
const opts = {
cwd: path.resolve(__dirname, '..', 'lib')
}
const plugins = [
[makeVisitor, opts]
]
var resultBefore = babel.transformFileSync('./fixtures/should-respect-cwd.js', {
plugins
})
resultBefore.code.should.not.match(/statementMap/)
opts.cwd = path.resolve(__dirname, '..', 'fixtures')
var resultAfter = babel.transformFileSync('./fixtures/should-respect-cwd.js', {
plugins
})
resultAfter.code.should.match(/statementMap/)
})
})
})