forked from istanbuljs/babel-plugin-istanbul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel-plugin-istanbul.js
176 lines (160 loc) · 5.66 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
/* 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({types: babel.types}), {
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({types: babel.types}), {
include: ['fixtures/plugin-should-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({types: babel.types}), {
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
})
it('should set a default coverageVariable', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor({types: babel.types}), {
include: ['fixtures/has-inline-source-map.js']
}]
]
})
result.code.should.match(/__coverage__/)
})
it('should set a custom coverageVariable', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor({types: babel.types}), {
include: ['fixtures/has-inline-source-map.js'],
coverageVariable: '__fooBar__'
}]
]
})
result.code.should.not.match(/__coverage__/)
result.code.should.match(/__fooBar__/)
})
})
context('source maps', function () {
it('should use inline source map', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor({types: babel.types}), {
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({types: babel.types}), {
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({types: babel.types}), {
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({types: babel.types})
]
})
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({types: babel.types})
]
})
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({types: babel.types})
]
})
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({types: babel.types})
]
})
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({types: babel.types}), {
include: ['fixtures/issue-78.js']
}]
]
})
result.code.match(/statementMap/)
})
})
})