source: trip-planner-front/node_modules/karma/lib/reporter.js@ 8d391a1

Last change on this file since 8d391a1 was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 5.4 KB
Line 
1'use strict'
2
3// eslint-disable-next-line node/no-deprecated-api
4const resolve = require('url').resolve
5const SourceMapConsumer = require('source-map').SourceMapConsumer
6const _ = require('lodash')
7
8const PathUtils = require('./utils/path-utils')
9const log = require('./logger').create('reporter')
10const MultiReporter = require('./reporters/multi')
11const baseReporterDecoratorFactory = require('./reporters/base').decoratorFactory
12
13function createErrorFormatter (config, emitter, SourceMapConsumer) {
14 const basePath = config.basePath
15 const urlRoot = config.urlRoot === '/' ? '' : (config.urlRoot || '')
16 let lastServedFiles = []
17
18 emitter.on('file_list_modified', (files) => {
19 lastServedFiles = files.served
20 })
21
22 const URL_REGEXP = new RegExp('(?:https?:\\/\\/' +
23 config.hostname + '(?:\\:' + config.port + ')?' + ')?\\/?' +
24 urlRoot + '\\/?' +
25 '(base/|absolute)' + // prefix, including slash for base/ to create relative paths.
26 '((?:[A-z]\\:)?[^\\?\\s\\:]*)' + // path
27 '(\\?\\w*)?' + // sha
28 '(\\:(\\d+))?' + // line
29 '(\\:(\\d+))?' + // column
30 '', 'g')
31
32 const cache = new WeakMap()
33
34 function getSourceMapConsumer (sourceMap) {
35 if (!cache.has(sourceMap)) {
36 cache.set(sourceMap, new SourceMapConsumer(sourceMap))
37 }
38 return cache.get(sourceMap)
39 }
40
41 return function (input, indentation) {
42 indentation = _.isString(indentation) ? indentation : ''
43 if (_.isError(input)) {
44 input = input.message
45 } else if (_.isEmpty(input)) {
46 input = ''
47 } else if (!_.isString(input)) {
48 input = JSON.stringify(input, null, indentation)
49 }
50
51 let msg = input.replace(URL_REGEXP, function (stackTracePath, prefix, path, __, ___, line, ____, column) {
52 const normalizedPath = prefix === 'base/' ? `${basePath}/${path}` : path
53 const file = lastServedFiles.find((file) => file.path === normalizedPath)
54
55 if (file && file.sourceMap && line) {
56 line = +line
57 column = +column
58
59 // When no column is given and we default to 0, it doesn't make sense to only search for smaller
60 // or equal columns in the sourcemap, let's search for equal or greater columns.
61 const bias = column ? SourceMapConsumer.GREATEST_LOWER_BOUND : SourceMapConsumer.LEAST_UPPER_BOUND
62
63 try {
64 const zeroBasedColumn = Math.max(0, (column || 1) - 1)
65 const original = getSourceMapConsumer(file.sourceMap).originalPositionFor({ line, column: zeroBasedColumn, bias })
66
67 // If there is no original position/source for the current stack trace path, then
68 // we return early with the formatted generated position. This handles the case of
69 // generated code which does not map to anything, see Case 1 of the source-map spec.
70 // https://sourcemaps.info/spec.html.
71 if (original.source === null) {
72 return PathUtils.formatPathMapping(path, line, column)
73 }
74
75 // Source maps often only have a local file name, resolve to turn into a full path if
76 // the path is not absolute yet.
77 const oneBasedOriginalColumn = original.column == null ? original.column : original.column + 1
78 return `${PathUtils.formatPathMapping(resolve(path, original.source), original.line, oneBasedOriginalColumn)} <- ${PathUtils.formatPathMapping(path, line, column)}`
79 } catch (e) {
80 log.warn(`An unexpected error occurred while resolving the original position for: ${stackTracePath}`)
81 log.warn(e)
82 }
83 }
84
85 return PathUtils.formatPathMapping(path, line, column) || prefix
86 })
87
88 if (indentation) {
89 msg = indentation + msg.replace(/\n/g, '\n' + indentation)
90 }
91
92 return config.formatError ? config.formatError(msg) : msg + '\n'
93 }
94}
95
96function createReporters (names, config, emitter, injector) {
97 const errorFormatter = createErrorFormatter(config, emitter, SourceMapConsumer)
98 const reporters = []
99
100 names.forEach((name) => {
101 if (['dots', 'progress'].includes(name)) {
102 [
103 require(`./reporters/${name}`),
104 require(`./reporters/${name}_color`)
105 ].forEach((Reporter) => {
106 reporters.push(new Reporter(errorFormatter, config.reportSlowerThan, config.colors, config.browserConsoleLogOptions))
107 })
108 return
109 }
110
111 const locals = {
112 baseReporterDecorator: ['factory', baseReporterDecoratorFactory],
113 formatError: ['value', errorFormatter]
114 }
115
116 try {
117 log.debug(`Trying to load reporter: ${name}`)
118 reporters.push(injector.createChild([locals], ['reporter:' + name]).get('reporter:' + name))
119 } catch (e) {
120 if (e.message.includes(`No provider for "reporter:${name}"`)) {
121 log.error(`Can not load reporter "${name}", it is not registered!\n Perhaps you are missing some plugin?`)
122 } else {
123 log.error(`Can not load "${name}"!\n ${e.stack}`)
124 }
125 emitter.emit('load_error', 'reporter', name)
126 return
127 }
128
129 const colorName = name + '_color'
130 if (!names.includes(colorName)) {
131 try {
132 log.debug(`Trying to load color-version of reporter: ${name} (${colorName})`)
133 reporters.push(injector.createChild([locals], ['reporter:' + colorName]).get('reporter:' + name))
134 } catch (e) {
135 log.debug('Couldn\'t load color-version.')
136 }
137 }
138 })
139
140 reporters.forEach((reporter) => emitter.bind(reporter))
141
142 return new MultiReporter(reporters)
143}
144
145createReporters.$inject = [
146 'config.reporters',
147 'config',
148 'emitter',
149 'injector'
150]
151
152exports.createReporters = createReporters
Note: See TracBrowser for help on using the repository browser.