|
| 1 | +import 'jasmine'; |
| 2 | + |
| 3 | +import * as glob from 'glob'; |
| 4 | +import * as Istanbul from 'istanbul'; |
| 5 | +import {SpecReporter as JasmineSpecReporter} from 'jasmine-spec-reporter'; |
| 6 | +import {join, relative} from 'path'; |
| 7 | +import {Position, SourceMapConsumer, SourceMapGenerator} from 'source-map'; |
| 8 | + |
| 9 | + |
| 10 | +const Jasmine = require('jasmine'); |
| 11 | + |
| 12 | +const projectBaseDir = join(__dirname, '../packages'); |
| 13 | +require('source-map-support').install({ |
| 14 | + hookRequire: true |
| 15 | +}); |
| 16 | + |
| 17 | + |
| 18 | +declare let global: any & { |
| 19 | + __coverage__: any; |
| 20 | +}; |
| 21 | + |
| 22 | +const inlineSourceMapRe = /\/\/# sourceMappingURL=data:application\/json;base64,(\S+)$/; |
| 23 | + |
| 24 | + |
| 25 | +// Use the internal SDK Hook of the require extension installed by our bootstrapping code to add |
| 26 | +// Istanbul collection to the code. |
| 27 | +const codeMap = new Map<string, { code: string, map: SourceMapConsumer }>(); |
| 28 | + |
| 29 | +(global as any)._SdkRequireHook = function(code: string, filename: string) { |
| 30 | + // Skip spec files. |
| 31 | + if (filename.match(/_spec\.ts$/)) { |
| 32 | + return code; |
| 33 | + } |
| 34 | + if (codeMap.get(filename)) { |
| 35 | + return codeMap.get(filename)!.code; |
| 36 | + } |
| 37 | + |
| 38 | + const instrumenter = new Istanbul.Instrumenter({ |
| 39 | + esModules: true, |
| 40 | + codeGenerationOptions: { |
| 41 | + sourceMap: filename, |
| 42 | + sourceMapWithCode: true |
| 43 | + } |
| 44 | + }); |
| 45 | + let instrumentedCode = instrumenter.instrumentSync(code, filename); |
| 46 | + const sourceMapGenerator: SourceMapGenerator = (instrumenter as any).sourceMap; |
| 47 | + const match = code.match(inlineSourceMapRe); |
| 48 | + |
| 49 | + if (match) { |
| 50 | + // Fix source maps for exception reporting (since the exceptions happen in the instrumented |
| 51 | + // code. |
| 52 | + const sourceMapJson = JSON.parse(Buffer.from(match[1], 'base64').toString()); |
| 53 | + const consumer = new SourceMapConsumer(sourceMapJson); |
| 54 | + sourceMapGenerator.applySourceMap(consumer, filename); |
| 55 | + |
| 56 | + instrumentedCode = instrumentedCode.replace(inlineSourceMapRe, '') |
| 57 | + + '//# sourceMappingURL=data:application/json;base64,' |
| 58 | + + new Buffer(sourceMapGenerator.toString()).toString('base64'); |
| 59 | + |
| 60 | + // Keep the consumer from the original source map, because the reports from Istanbul are |
| 61 | + // already mapped against the code. |
| 62 | + codeMap.set(filename, { code: instrumentedCode, map: consumer }); |
| 63 | + } |
| 64 | + |
| 65 | + return instrumentedCode; |
| 66 | +}; |
| 67 | + |
| 68 | + |
| 69 | +// Add the Istanbul reporter. |
| 70 | +const istanbulCollector = new Istanbul.Collector({}); |
| 71 | +const istanbulReporter = new Istanbul.Reporter(undefined, 'coverage/'); |
| 72 | +istanbulReporter.addAll(['json', 'lcov']); |
| 73 | + |
| 74 | + |
| 75 | +interface CoverageLocation { |
| 76 | + start: Position; |
| 77 | + end: Position; |
| 78 | +} |
| 79 | + |
| 80 | +class IstanbulReporter implements jasmine.CustomReporter { |
| 81 | + // Update a location object from a SourceMap. Will ignore the location if the sourcemap does |
| 82 | + // not have a valid mapping. |
| 83 | + private _updateLocation(consumer: SourceMapConsumer, location: CoverageLocation) { |
| 84 | + const start = consumer.originalPositionFor(location.start); |
| 85 | + const end = consumer.originalPositionFor(location.end); |
| 86 | + |
| 87 | + // Filter invalid original positions. |
| 88 | + if (start.line !== null && start.column !== null) { |
| 89 | + // Filter unwanted properties. |
| 90 | + location.start = { line: start.line, column: start.column }; |
| 91 | + } |
| 92 | + if (end.line !== null && end.column !== null) { |
| 93 | + location.end = { line: end.line, column: end.column }; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private _updateCoverageJsonSourceMap(coverageJson: any) { |
| 98 | + // Update the coverageJson with the SourceMap. |
| 99 | + for (const path of Object.keys(coverageJson)) { |
| 100 | + const entry = codeMap.get(path); |
| 101 | + if (!entry) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + const consumer = entry.map; |
| 106 | + const coverage = coverageJson[path]; |
| 107 | + |
| 108 | + // Update statement maps. |
| 109 | + for (const branchId of Object.keys(coverage.branchMap)) { |
| 110 | + debugger; |
| 111 | + const branch = coverage.branchMap[branchId]; |
| 112 | + let line: number | null = null; |
| 113 | + let column = 0; |
| 114 | + do { |
| 115 | + line = consumer.originalPositionFor({ line: branch.line, column: column++ }).line; |
| 116 | + } while (line === null && column < 100); |
| 117 | + |
| 118 | + branch.line = line; |
| 119 | + |
| 120 | + for (const location of branch.locations) { |
| 121 | + this._updateLocation(consumer, location); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + for (const id of Object.keys(coverage.statementMap)) { |
| 126 | + const location = coverage.statementMap[id]; |
| 127 | + this._updateLocation(consumer, location); |
| 128 | + } |
| 129 | + |
| 130 | + for (const id of Object.keys(coverage.fnMap)) { |
| 131 | + const fn = coverage.fnMap[id]; |
| 132 | + fn.line = consumer.originalPositionFor({ line: fn.line, column: 0 }).line; |
| 133 | + this._updateLocation(consumer, fn.loc); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + jasmineDone(_runDetails: jasmine.RunDetails): void { |
| 139 | + if (global.__coverage__) { |
| 140 | + this._updateCoverageJsonSourceMap(global.__coverage__); |
| 141 | + istanbulCollector.add(global.__coverage__); |
| 142 | + } |
| 143 | + |
| 144 | + istanbulReporter.write(istanbulCollector, true, () => {}); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +// Create a Jasmine runner and configure it. |
| 150 | +const runner = new Jasmine({ projectBaseDir: projectBaseDir }); |
| 151 | + |
| 152 | +if (process.argv.indexOf('--spec-reporter') != -1) { |
| 153 | + runner.env.clearReporters(); |
| 154 | + runner.env.addReporter(new JasmineSpecReporter({ |
| 155 | + stacktrace: { |
| 156 | + // Filter all JavaScript files that appear after a TypeScript file (callers) from the stack |
| 157 | + // trace. |
| 158 | + filter: (x: string) => { |
| 159 | + return x.substr(0, x.indexOf('\n', x.indexOf('\n', x.lastIndexOf('.ts:')) + 1)); |
| 160 | + } |
| 161 | + }, |
| 162 | + suite: { |
| 163 | + displayNumber: true |
| 164 | + }, |
| 165 | + summary: { |
| 166 | + displayStacktrace: true, |
| 167 | + displayErrorMessages: true |
| 168 | + } |
| 169 | + })); |
| 170 | +} |
| 171 | +runner.env.addReporter(new IstanbulReporter()); |
| 172 | + |
| 173 | + |
| 174 | +// Manually set exit code (needed with custom reporters) |
| 175 | +runner.onComplete((success: boolean) => { |
| 176 | + process.exitCode = success ? 0 : 1; |
| 177 | +}); |
| 178 | + |
| 179 | + |
| 180 | +glob.sync('packages/**/*.spec.ts') |
| 181 | + .filter(p => !/schematics_cli\/schematics\//.test(p)) |
| 182 | + .forEach(path => { |
| 183 | + console.error(`Invalid spec file name: ${path}. You're using the old convention.`); |
| 184 | + }); |
| 185 | + |
| 186 | +// Run the tests. |
| 187 | +const allTests = |
| 188 | + glob.sync('packages/**/*_spec.ts') |
| 189 | + .map(p => relative(projectBaseDir, p)) |
| 190 | + .filter(p => !/schematics_cli\/schematics\//.test(p)); |
| 191 | + |
| 192 | +export default function(_args: any) { |
| 193 | + runner.execute(allTests); |
| 194 | +} |
0 commit comments