|
| 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 | +}) |
0 commit comments