Debian Codegraph @

babel-plugin-istanbul/test/babel-plugin-istanbul.js

raw symbols SCIP
1 /* eslint-env mocha */
2
3 import * as babel from '@babel/core'
4 import makeVisitor from '../src'
5 import path from 'path'
6
7 require('chai').should()
8
9 describe('babel-plugin-istanbul', function () {
10 context('Babel plugin config', function () {
11 it('should instrument file if shouldSkip returns false', function () {
12 var result = babel.transformFileSync('./fixtures/plugin-should-cover.js', {
13 babelrc: false,
14 configFile: false,
15 plugins: [
16 [makeVisitor, {
17 include: ['fixtures/plugin-should-cover.js']
18 }]
19 ]
20 })
21 result.code.should.match(/statementMap/)
22 })
23
24 it('should not instrument file if shouldSkip returns true', function () {
25 var result = babel.transformFileSync('./fixtures/plugin-should-not-cover.js', {
26 babelrc: false,
27 configFile: false,
28 plugins: [
29 [makeVisitor, {
30 include: ['fixtures/plugin-should-cover.js']
31 }]
32 ]
33 })
34 result.code.should.not.match(/statementMap/)
35 })
36
37 context('local node_modules', function () {
38 it('should instrument file if shouldSkip returns false', function () {
39 var result = babel.transformFileSync('./fixtures/node_modules/should-cover.js', {
40 babelrc: false,
41 configFile: false,
42 plugins: [
43 [makeVisitor, {
44 excludeNodeModules: false,
45 exclude: ['node_modules/**'],
46 include: ['fixtures/node_modules/should-cover.js']
47 }]
48 ]
49 })
50 result.code.should.match(/statementMap/)
51 })
52
53 it('should not instrument file if shouldSkip returns true', function () {
54 var result = babel.transformFileSync('./fixtures/node_modules/should-not-cover.js', {
55 babelrc: false,
56 configFile: false,
57 plugins: [
58 [makeVisitor, {
59 include: ['fixtures/node_modules/should-not-cover.js']
60 }]
61 ]
62 })
63 result.code.should.not.match(/statementMap/)
64 })
65 })
66
67 it('should call onCover callback', function () {
68 var args
69 babel.transformFileSync('./fixtures/plugin-should-cover.js', {
70 babelrc: false,
71 configFile: false,
72 plugins: [
73 [makeVisitor, {
74 onCover: function () {
75 args = [].slice.call(arguments)
76 },
77 include: ['fixtures/plugin-should-cover.js']
78 }]
79 ]
80 })
81 args[0].should.equal(path.resolve('./fixtures/plugin-should-cover.js'))
82 args[1].statementMap.should.exist // eslint-disable-line
83 })
84 })
85
86 context('source maps', function () {
87 it('should use inline source map', function () {
88 var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
89 babelrc: false,
90 configFile: false,
91 plugins: [
92 [makeVisitor, {
93 include: ['fixtures/has-inline-source-map.js']
94 }]
95 ]
96 })
97 result.code.should.match(/inputSourceMap/)
98 })
99
100 it('should not use inline source map if inputSourceMap is set to false', function () {
101 var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
102 babelrc: false,
103 configFile: false,
104 plugins: [
105 [makeVisitor, {
106 include: ['fixtures/has-inline-source-map.js'],
107 useInlineSourceMaps: false
108 }]
109 ]
110 })
111 result.code.should.not.match(/inputSourceMap/)
112 })
113
114 it('should use provided source map', function () {
115 var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
116 babelrc: false,
117 configFile: false,
118 plugins: [
119 [makeVisitor, {
120 include: ['fixtures/has-inline-source-map.js'],
121 inputSourceMap: { asdfQwer: 'foo' }
122 }]
123 ]
124 })
125 result.code.should.match(/inputSourceMap:\s*{\s*asdfQwer: "foo"\s*}/)
126 })
127 })
128
129 context('instrument options', function () {
130 it('should honor coverageVariable option', function () {
131 const result = babel.transformFileSync('./fixtures/should-cover.js', {
132 babelrc: false,
133 configFile: false,
134 plugins: [
135 [makeVisitor, {
136 include: ['fixtures/should-cover.js'],
137 coverageVariable: '__TEST_VARIABLE__'
138 }]
139 ]
140 })
141 result.code.should.match(/__TEST_VARIABLE__/)
142 result.code.should.not.match(/__coverage__/)
143 })
144
145 it('should honor coverageGlobalScope option', function () {
146 const result = babel.transformFileSync('./fixtures/should-cover.js', {
147 babelrc: false,
148 configFile: false,
149 plugins: [
150 [makeVisitor, {
151 include: ['fixtures/should-cover.js'],
152 coverageGlobalScope: 'window'
153 }]
154 ]
155 })
156 result.code.should.match(/new Function\("return window"\)/)
157 result.code.should.not.match(/new Function\("return this"\)/)
158 })
159
160 it('should honor coverageGlobalScope option', function () {
161 const result = babel.transformFileSync('./fixtures/should-cover.js', {
162 babelrc: false,
163 configFile: false,
164 plugins: [
165 [makeVisitor, {
166 include: ['fixtures/should-cover.js'],
167 coverageGlobalScopeFunc: false
168 }]
169 ]
170 })
171 result.code.should.match(/global\s*=\s*this/)
172 result.code.should.not.match(/global\s*=\s*new Function\("return this"\)/)
173 })
174
175 it('should honor ignoreClassMethods option', function () {
176 const result = babel.transformFileSync('./fixtures/class-functions.js', {
177 babelrc: false,
178 configFile: false,
179 plugins: [
180 [makeVisitor, {
181 include: ['fixtures/class-functions.js'],
182 ignoreClassMethods: ['bar']
183 }]
184 ]
185 })
186
187 // bar() is ignored
188 result.code.should.match(/bar\(\)\s*{\s*}/)
189 result.code.should.not.match(/bar\(\)\s*{\s*cov_.*/)
190
191 // barz() does not get instrumented
192 result.code.should.match(/barz\(\)\s*{\s*cov_.*/)
193 result.code.should.not.match(/barz\(\)\s*{\s*}/)
194 })
195 })
196
197 context('package.json "nyc" config', function () {
198 context('process.env.NYC_CONFIG is set', function () {
199 it('should instrument file if shouldSkip returns false', function () {
200 var result = babel.transformFileSync('./fixtures/should-cover.js', {
201 babelrc: false,
202 configFile: false,
203 plugins: [
204 makeVisitor
205 ]
206 })
207 result.code.should.match(/statementMap/)
208 })
209
210 it('should not instrument file if shouldSkip returns true', function () {
211 var result = babel.transformFileSync('./fixtures/should-not-cover.js', {
212 babelrc: false,
213 configFile: false,
214 plugins: [
215 makeVisitor
216 ]
217 })
218 result.code.should.not.match(/statementMap/)
219 })
220 })
221
222 context('process.env.NYC_CONFIG is not set', function () {
223 this.timeout(10000)
224
225 const OLD_NYC_CONFIG = process.env.NYC_CONFIG
226 const OLD_NYC_CWD = process.env.NYC_CWD
227 before(() => {
228 delete process.env.NYC_CONFIG
229 delete process.env.NYC_CWD
230 })
231 after(() => {
232 process.env.NYC_CONFIG = OLD_NYC_CONFIG
233 process.env.NYC_CWD = OLD_NYC_CWD
234 })
235
236 it('should instrument file if shouldSkip returns false', function () {
237 var result = babel.transformFileSync('./fixtures/should-cover.js', {
238 babelrc: false,
239 configFile: false,
240 plugins: [
241 makeVisitor
242 ]
243 })
244 result.code.should.match(/statementMap/)
245 })
246
247 it('should not instrument file if shouldSkip returns true', function () {
248 var result = babel.transformFileSync('./fixtures/should-not-cover.js', {
249 babelrc: false,
250 configFile: false,
251 plugins: [
252 makeVisitor
253 ]
254 })
255 result.code.should.not.match(/statementMap/)
256 })
257
258 it('should load config using cwd', function () {
259 const cwd = path.resolve(__dirname, '..', 'fixtures', 'config')
260 function helper (file, match, opts) {
261 const result = babel.transformFileSync(
262 path.resolve(cwd, file),
263 {
264 babelrc: false,
265 configFile: false,
266 plugins: [
267 [makeVisitor, { cwd, ...opts }]
268 ]
269 }
270 )
271 if (match) {
272 result.code.should.match(/statementMap/)
273 } else {
274 result.code.should.not.match(/statementMap/)
275 }
276 }
277
278 helper('file1.js', true)
279 helper('file2.js', false)
280 helper('file1.js', false, { nycrcPath: 'nyc-alt.config.js' })
281 helper('file2.js', true, { nycrcPath: 'nyc-alt.config.js' })
282 ;(function () {
283 babel.transformFileSync(
284 path.resolve(cwd, 'file1.js'),
285 {
286 babelrc: false,
287 configFile: false,
288 plugins: [
289 [makeVisitor, { cwd, nycrcPath: 'missing-config.js' }]
290 ]
291 }
292 )
293 }).should.throw(/Requested configuration file missing-config.js not found/)
294 })
295 })
296 })
297
298 // TODO: setup istanbul-lib-instrument, such that we can
299 // run various babel configurations.
300 context('regression tests', () => {
301 // regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/78
302 it('should instrument: export const foo = () => {}', function () {
303 var result = babel.transformFileSync('./fixtures/issue-78.js', {
304 babelrc: false,
305 configFile: false,
306 plugins: [
307 [makeVisitor, {
308 include: ['fixtures/issue-78.js']
309 }]
310 ]
311 })
312 result.code.match(/statementMap/)
313 })
314
315 // regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/201
316 it('should not conflict with transform-modules-commonjs', function () {
317 var result = babel.transformFileSync('./fixtures/issue-201.js', {
318 babelrc: false,
319 configFile: false,
320 plugins: [
321 [makeVisitor, {
322 include: ['fixtures/issue-201.js']
323 }],
324 '@babel/plugin-transform-modules-commonjs'
325 ]
326 })
327 result.code.should.match(/_path.*\.resolve\)\(_path\)/)
328 result.code.should.not.match(/_path\.resolve\)\(_path\)/)
329 })
330 })
331 })