-
Notifications
You must be signed in to change notification settings - Fork 248
/
preprocessor.spec.coffee
160 lines (126 loc) · 4.78 KB
/
preprocessor.spec.coffee
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
vm = require 'vm'
util = require 'util'
helper = {_: require 'lodash'}
coverageMap = require '../lib/coverage-map'
describe 'preprocessor', ->
createPreprocessor = require '../lib/preprocessor'
ORIGINAL_CODE = '''
if (a) {
something();
} else {
other();
}
'''
ORIGINAL_COFFEE_CODE = '''
if a
something()
else
other()
'''
mockLogger = create: ->
error: -> throw new Error(util.format.apply util, arguments)
warn: -> null
info: -> null
debug: -> null
# TODO(vojta): refactor this somehow ;-) it's copy pasted from lib/file-list.js
File = (path, mtime) ->
@path = path
@originalPath = path
@contentPath = path
@mtime = mtime
@isUrl = false
it 'should not do anything if coverage reporter is not used', (done) ->
process = createPreprocessor mockLogger, helper, null, ['dots', 'progress'], {}
file = new File '/base/path/file.js'
process ORIGINAL_CODE, file, (preprocessedCode) ->
expect(preprocessedCode).to.equal ORIGINAL_CODE
expect(file.path).to.equal '/base/path/file.js'
done()
it 'should preprocess the code', (done) ->
process = createPreprocessor mockLogger, helper, '/base/path', ['coverage', 'progress'], {}
file = new File '/base/path/file.js'
process ORIGINAL_CODE, file, (preprocessedCode) ->
sandbox =
a: true
something: ->
vm.runInNewContext preprocessedCode, sandbox
expect(sandbox.__coverage__).to.have.ownProperty '/base/path/file.js'
done()
it 'should preprocess the fake code', (done) ->
fakeInstanbulLikeInstrumenter = ->
fakeInstanbulLikeInstrumenter::instrument = (_a, _b, callback) ->
callback()
return
process = createPreprocessor mockLogger, helper, '/base/path', ['coverage', 'progress'],
instrumenters:
fakeInstanbulLike :
Instrumenter : fakeInstanbulLikeInstrumenter
instrumenter:
'**/*.fake': 'fakeInstanbulLike'
file = new File '/base/path/file.fake'
process ORIGINAL_COFFEE_CODE, file, (preprocessedCode) ->
sandbox =
a: true
something: ->
vm.runInNewContext preprocessedCode, sandbox
expect(file.path).to.equal '/base/path/file.fake'
done()
it 'should preprocess the fake code with the config options', (done) ->
fakeInstanbulLikeInstrumenter = (options) ->
expect(options.experimental).to.be.ok
return
fakeInstanbulLikeInstrumenter::instrument = (_a, _b, callback) ->
callback()
return
process = createPreprocessor mockLogger, helper, '/base/path', ['coverage', 'progress'],
instrumenters:
fakeInstanbulLike:
Instrumenter: fakeInstanbulLikeInstrumenter
instrumenterOptions:
fakeInstanbulLike:
experimental: yes
instrumenter:
'**/*.fake': 'fakeInstanbulLike'
file = new File '/base/path/file.fake'
process ORIGINAL_COFFEE_CODE, file, done
it 'should not preprocess the coffee code', (done) ->
process = createPreprocessor mockLogger, helper, '/base/path', ['coverage', 'progress'],
instrumenter:
'**/*.coffee': 'istanbul'
file = new File '/base/path/file.coffee'
process ORIGINAL_CODE, file, (preprocessedCode) ->
sandbox =
a: true
something: ->
vm.runInNewContext preprocessedCode, sandbox
expect(file.path).to.equal '/base/path/file.coffee'
expect(sandbox.__coverage__).to.have.ownProperty '/base/path/file.coffee'
done()
it 'should fail if invalid instrumenter provided', (done) ->
work = ->
createPreprocessor mockLogger, helper, '/base/path', ['coverage', 'progress'],
instrumenter:
'**/*.coffee': 'madeup'
expect(work).to.throw()
done()
it 'should add coverageMap when including all sources', (done) ->
process = createPreprocessor mockLogger, helper, '/base/path', ['coverage'], { includeAllSources: true }
file = new File '/base/path/file.js'
coverageMap.reset()
process ORIGINAL_CODE, file, (preprocessedCode) ->
expect(coverageMap.get()['/base/path/file.js']).to.exist
done()
it 'should not add coverageMap when not including all sources', (done) ->
process = createPreprocessor mockLogger, helper, '/base/path', ['coverage'], { includeAllSources: false }
file = new File '/base/path/file.js'
coverageMap.reset()
process ORIGINAL_CODE, file, (preprocessedCode) ->
expect(coverageMap.get()['./file.js']).to.not.exist
done()
it 'should not add coverageMap in the default state', (done) ->
process = createPreprocessor mockLogger, helper, '/base/path', ['coverage'], {}
file = new File '/base/path/file.js'
coverageMap.reset()
process ORIGINAL_CODE, file, (preprocessedCode) ->
expect(coverageMap.get()['./file.js']).to.not.exist
done()