Skip to content

Commit 83bafc3

Browse files
authoredMar 23, 2020
refactor: replace migrate coffee unit tests to modern JS (#407)
1 parent 49f174d commit 83bafc3

18 files changed

+906
-790
lines changed
 

‎.eslintrc

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
{
2-
"extends": "standard"
2+
"extends": "standard",
3+
"env": {
4+
"node": true,
5+
"mocha": true
6+
},
7+
"globals": {
8+
"expect": true,
9+
"sinon": true
10+
}
311
}

‎gruntfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module.exports = function (grunt) {
88
},
99
unit: {
1010
src: [
11-
'test/mocha-globals.coffee',
12-
'test/*.spec.coffee'
11+
'test/mocha-globals.js',
12+
'test/*.spec.js'
1313
]
1414
}
1515
},

‎test/coverage-map.spec.coffee

-28
This file was deleted.

‎test/coverage-map.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const coverageMap = require('../lib/coverage-map')
2+
const coverageObj = { path: './path.js', otherThings: 'that are in instrumented code' }
3+
4+
describe('coverageMap', () => {
5+
it('should add coverageMap and get them', () => {
6+
coverageMap.add(coverageObj)
7+
expect(coverageMap.get()['./path.js']).to.equal(coverageObj)
8+
})
9+
10+
it('should be able to be reset', () => {
11+
coverageMap.reset()
12+
13+
expect(coverageMap.get()['./path.js']).to.not.exist
14+
15+
coverageMap.add(coverageObj)
16+
17+
expect(coverageMap.get()['./path.js']).to.equal(coverageObj)
18+
19+
coverageMap.reset()
20+
21+
expect(coverageMap.get()['./path.js']).to.not.exist
22+
})
23+
24+
it('should be able to have multiple coverageMap', () => {
25+
coverageMap.reset()
26+
coverageMap.add(coverageObj)
27+
coverageMap.add({ path: './anotherFile.js', moarKeys: [1, 2, 3] })
28+
29+
expect(Object.keys(coverageMap.get()).length).to.equal(2)
30+
})
31+
})

‎test/in-memory-report.spec.coffee

-28
This file was deleted.

‎test/in-memory-report.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const InMemoryReport = require('../lib/in-memory-report')
2+
3+
describe('InMemoryReport', () => {
4+
const emitter = {emit: sinon.stub()}
5+
const browser = { name: 'firefox' }
6+
const result = { test: { data: 'result' } }
7+
const fc = {
8+
path: 'test',
9+
toJSON: sinon.stub().returns({ data: 'result' })
10+
}
11+
const node = {getFileCoverage: sinon.stub().returns(fc)}
12+
13+
it('should raise an "coverage_complete" event.', () => {
14+
const sut = new InMemoryReport({browser, emitter})
15+
sut.onStart()
16+
sut.onDetail(node)
17+
sut.onEnd()
18+
expect(node.getFileCoverage).to.have.been.called
19+
expect(fc.toJSON).to.have.been.called
20+
expect(emitter.emit).to.have.been.calledWith('coverage_complete', browser, result)
21+
})
22+
23+
it('should be of type "in-memory"', () =>
24+
expect(InMemoryReport.TYPE).to.be.equal('in-memory')
25+
)
26+
27+
it('should not fail when created without arguments', () =>
28+
expect(new InMemoryReport()).to.be.ok
29+
)
30+
})

‎test/index.spec.coffee

-7
This file was deleted.

‎test/index.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require('../lib/index')
2+
const InMemoryReport = require('../lib/in-memory-report')
3+
const reportCreator = require('../lib/report-creator')
4+
5+
describe('Index', () => {
6+
it('should register "InMemoryReport" to Report Creator', () =>
7+
expect(reportCreator.create('in-memory', {})).to.be.an.instanceof(InMemoryReport)
8+
)
9+
})

‎test/mocha-globals.coffee

-13
This file was deleted.

‎test/mocha-globals.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const sinon = require('sinon')
2+
const chai = require('chai')
3+
4+
// publish globals that all specs can use
5+
global.expect = chai.expect
6+
global.should = chai.should()
7+
global.sinon = sinon
8+
9+
// chai plugins
10+
chai.use(require('sinon-chai'))
11+
12+
afterEach(() => global.sinon.restore())

‎test/preprocessor.spec.coffee

-190
This file was deleted.

‎test/preprocessor.spec.js

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
const vm = require('vm')
2+
const util = require('util')
3+
const path = require('path')
4+
5+
const coverageMap = require('../lib/coverage-map')
6+
7+
describe('preprocessor', () => {
8+
const createPreprocessor = require('../lib/preprocessor')
9+
10+
const ORIGINAL_CODE = `
11+
if (a) {
12+
something();
13+
} else {
14+
other();
15+
}
16+
`
17+
18+
const ORIGINAL_COFFEE_CODE = `
19+
if a
20+
something()
21+
else
22+
other()
23+
`
24+
25+
const mockLogger = {create: () => {
26+
return {
27+
error: (...arg) => { throw new Error(util.format.apply(util, arg)) },
28+
warn: () => {},
29+
info: () => {},
30+
debug: () => {}
31+
}
32+
}}
33+
34+
// TODO(vojta): refactor this somehow ;-) it's copy pasted from lib/file-list.js
35+
function File (path, mtime) {
36+
this.path = path
37+
this.originalPath = path
38+
this.contentPath = path
39+
this.mtime = mtime
40+
this.isUrl = false
41+
}
42+
43+
it('should not do anything if coverage reporter is not used', (done) => {
44+
const process = createPreprocessor(mockLogger, null, ['dots', 'progress'], {})
45+
const file = new File('/base/path/file.js')
46+
47+
process(ORIGINAL_CODE, file, (preprocessedCode) => {
48+
expect(preprocessedCode).to.equal(ORIGINAL_CODE)
49+
expect(file.path).to.equal('/base/path/file.js')
50+
done()
51+
})
52+
})
53+
54+
it('should preprocess the code', (done) => {
55+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage', 'progress'], {})
56+
const file = new File('/base/path/file.js')
57+
58+
process(ORIGINAL_CODE, file, (preprocessedCode) => {
59+
const sandbox = {
60+
a: true,
61+
something: () => {}
62+
}
63+
64+
vm.runInNewContext(preprocessedCode, sandbox)
65+
expect(sandbox.__coverage__).to.have.ownProperty(path.resolve('/base/path/file.js'))
66+
done()
67+
})
68+
})
69+
70+
it('should preprocess the fake code', (done) => {
71+
class fakeInstanbulLikeInstrumenter {
72+
instrument (_a, _b, callback) {
73+
callback()
74+
}
75+
lastSourceMap () {}
76+
}
77+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage', 'progress'], {
78+
instrumenters: {
79+
fakeInstanbulLike: {
80+
Instrumenter: fakeInstanbulLikeInstrumenter
81+
}
82+
},
83+
instrumenter: {
84+
'**/*.fake': 'fakeInstanbulLike'
85+
}
86+
})
87+
const file = new File('/base/path/file.fake')
88+
89+
process(ORIGINAL_COFFEE_CODE, file, (preprocessedCode) => {
90+
const sandbox = {
91+
a: true,
92+
something: () => {}
93+
}
94+
95+
vm.runInNewContext(preprocessedCode, sandbox)
96+
expect(file.path).to.equal('/base/path/file.fake')
97+
done()
98+
})
99+
})
100+
101+
it('should preprocess the fake code with the config options', (done) => {
102+
class fakeInstanbulLikeInstrumenter {
103+
constructor (options) {
104+
expect(options.experimental).to.be.ok
105+
}
106+
instrument (_a, _b, callback) {
107+
callback()
108+
}
109+
lastSourceMap () {}
110+
}
111+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage', 'progress'], {
112+
instrumenters: {
113+
fakeInstanbulLike: {
114+
Instrumenter: fakeInstanbulLikeInstrumenter
115+
}
116+
},
117+
instrumenterOptions: {
118+
fakeInstanbulLike: {
119+
experimental: 'yes'
120+
}
121+
},
122+
instrumenter: {
123+
'**/*.fake': 'fakeInstanbulLike'
124+
}
125+
})
126+
127+
const file = new File('/base/path/file.fake')
128+
process(ORIGINAL_COFFEE_CODE, file, done)
129+
})
130+
131+
it('should not preprocess the coffee code', (done) => {
132+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage', 'progress'], {instrumenter: {'**/*.coffee': 'istanbul'}})
133+
const file = new File('/base/path/file.coffee')
134+
135+
process(ORIGINAL_CODE, file, (preprocessedCode) => {
136+
const sandbox = {
137+
a: true,
138+
something: () => {}
139+
}
140+
141+
vm.runInNewContext(preprocessedCode, sandbox)
142+
expect(file.path).to.equal('/base/path/file.coffee')
143+
expect(sandbox.__coverage__).to.have.ownProperty(path.resolve('/base/path/file.coffee'))
144+
done()
145+
})
146+
})
147+
148+
it('should fail if invalid instrumenter provided', () => {
149+
const work = () => {
150+
createPreprocessor(mockLogger, '/base/path', ['coverage', 'progress'], {instrumenter: {'**/*.coffee': 'madeup'}})
151+
}
152+
expect(work).to.throw()
153+
})
154+
155+
it('should add coverageMap when including all sources', (done) => {
156+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage'], { includeAllSources: true })
157+
const file = new File('/base/path/file.js')
158+
159+
coverageMap.reset()
160+
161+
process(ORIGINAL_CODE, file, (preprocessedCode) => {
162+
expect(coverageMap.get()[path.resolve('/base/path/file.js')]).to.exist
163+
done()
164+
})
165+
})
166+
167+
it('should not add coverageMap when not including all sources', (done) => {
168+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage'], { includeAllSources: false })
169+
const file = new File('/base/path/file.js')
170+
171+
coverageMap.reset()
172+
173+
process(ORIGINAL_CODE, file, (preprocessedCode) => {
174+
expect(coverageMap.get()['./file.js']).to.not.exist
175+
done()
176+
})
177+
})
178+
179+
it('should not add coverageMap in the default state', (done) => {
180+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage'], {})
181+
const file = new File('/base/path/file.js')
182+
183+
coverageMap.reset()
184+
185+
process(ORIGINAL_CODE, file, (preprocessedCode) => {
186+
expect(coverageMap.get()['./file.js']).to.not.exist
187+
done()
188+
})
189+
})
190+
191+
it('should change extension of CoffeeScript files when given `useJSExtensionForCoffeeScript`', (done) => {
192+
class ibrikInstrumenter {
193+
instrument (_a, _b, callback) {
194+
callback()
195+
}
196+
lastSourceMap () {}
197+
}
198+
199+
const process = createPreprocessor(mockLogger, '/base/path', ['coverage', 'progress'], {
200+
instrumenters: {
201+
ibrik: {
202+
Instrumenter: ibrikInstrumenter
203+
}
204+
},
205+
instrumenter: {
206+
'**/*.coffee': 'ibrik'
207+
},
208+
useJSExtensionForCoffeeScript: true
209+
})
210+
211+
const file = new File('/base/path/file.coffee')
212+
213+
process(ORIGINAL_COFFEE_CODE, file, (preprocessedCode) => {
214+
const sandbox = {
215+
a: true,
216+
something: () => {}
217+
}
218+
219+
vm.runInNewContext(preprocessedCode, sandbox)
220+
expect(file.path).to.equal('/base/path/file.js')
221+
done()
222+
})
223+
})
224+
})

‎test/report-creator.spec.coffee

-45
This file was deleted.

‎test/report-creator.spec.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const istanbulReports = require('istanbul-reports')
2+
const reportCreator = require('../lib/report-creator')
3+
4+
describe('Report Creator', () => {
5+
afterEach(() => reportCreator.reset())
6+
7+
describe('register', () => {
8+
it('should throw when reporter does not include type', () => {
9+
const reporter = {}
10+
expect(() => reportCreator.register(reporter)).to.throw
11+
})
12+
13+
it('should complete when report includes a type', () => {
14+
const reporter = { TYPE: 'test' }
15+
expect(reportCreator.register(reporter)).to.be.equal('test')
16+
})
17+
})
18+
19+
describe('create', () => {
20+
it('should return custom reporter if registered', () => {
21+
const Reporter = sinon.stub()
22+
Reporter.TYPE = 'test'
23+
reportCreator.register(Reporter)
24+
const fallbackCreateStub = sinon.stub(istanbulReports, 'create')
25+
const reporterOpts = { test: 'options' }
26+
27+
reportCreator.create('test', reporterOpts)
28+
29+
expect(fallbackCreateStub).not.to.be.called
30+
expect(Reporter.calledWithNew()).to.be.true
31+
expect(Reporter).to.be.calledWith(reporterOpts)
32+
})
33+
34+
it('should proxy call to istanbul if custom reporter is not registered', () => {
35+
const fallbackCreateStub = sinon.stub(istanbulReports, 'create')
36+
fallbackCreateStub.returnsThis()
37+
const reporterOpts = { test: 'options' }
38+
39+
reportCreator.create('test', reporterOpts)
40+
expect(fallbackCreateStub).to.be.calledWith('test', reporterOpts)
41+
})
42+
})
43+
})

‎test/reporter.spec.coffee

-454
This file was deleted.

‎test/reporter.spec.js

+523
Large diffs are not rendered by default.

‎test/source-map-store.spec.coffee

-22
This file was deleted.

‎test/source-map-store.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const istanbulLibSourceMaps = require('istanbul-lib-source-maps')
2+
const globalSourceMapStore = require('../lib/source-map-store')
3+
4+
describe('Source Map Store', () => {
5+
it('should create a source map store for path if it did not exist previously', () => {
6+
const createSourceMapStoreStub = sinon.stub(istanbulLibSourceMaps, 'createSourceMapStore')
7+
createSourceMapStoreStub.returns({})
8+
9+
globalSourceMapStore.get('__test', { opts: 'test' })
10+
expect(createSourceMapStoreStub).to.be.calledWith({ opts: 'test' })
11+
})
12+
13+
it('should not create a source map store for path if it previously was called', () => {
14+
const createSourceMapStoreStub = sinon.stub(istanbulLibSourceMaps, 'createSourceMapStore')
15+
createSourceMapStoreStub.returns({})
16+
17+
globalSourceMapStore.get('__test2', { opts: 'test2' })
18+
globalSourceMapStore.get('__test2', { opts: 'test3' })
19+
20+
expect(createSourceMapStoreStub.callCount).to.be.equal(1)
21+
expect(createSourceMapStoreStub).to.be.calledWith({ opts: 'test2' })
22+
})
23+
})

0 commit comments

Comments
 (0)
Please sign in to comment.