UNPKG

4.29 kBJavaScriptView Raw
1(function(window) {
2
3var formatError = function (error) {
4 var stack = error.stack
5 var message = error.message
6
7 if (stack) {
8 if (message && stack.indexOf(message) === -1) {
9 stack = message + '\n' + stack
10 }
11
12 // remove mocha stack entries
13 return stack.replace(/\n.+\/mocha\/mocha.js\?\w*\:.+(?=(\n|$))/g, '')
14 }
15
16 return message
17}
18
19// non-compliant version of Array::reduce.call (requires memo argument)
20var arrayReduce = function (array, reducer, memo) {
21 for (var i = 0, len = array.length; i < len; i++) {
22 memo = reducer(memo, array[i])
23 }
24 return memo
25}
26
27var createMochaReporterNode = function () {
28 var mochaRunnerNode = document.createElement('div')
29 mochaRunnerNode.setAttribute('id', 'mocha')
30 document.body.appendChild(mochaRunnerNode)
31}
32
33var haveMochaConfig = function (karma) {
34 return karma.config && karma.config.mocha
35}
36
37var createMochaReporterConstructor = function (tc, pathname) {
38 // Set custom reporter on debug page
39 if (/debug.html$/.test(pathname) && haveMochaConfig(tc) && tc.config.mocha.reporter) {
40 createMochaReporterNode()
41 return tc.config.mocha.reporter
42 }
43
44 // TODO(vojta): error formatting
45 return function (runner) {
46 // runner events
47 // - start
48 // - end
49 // - suite
50 // - suite end
51 // - test
52 // - test end
53 // - pass
54 // - fail
55
56 runner.on('start', function () {
57 tc.info({total: runner.total})
58 })
59
60 runner.on('end', function () {
61 tc.complete({
62 coverage: window.__coverage__
63 })
64 })
65
66 runner.on('test', function (test) {
67 test.$errors = []
68 })
69
70 runner.on('fail', function (test, error) {
71 if (test.type === 'hook') {
72 test.$errors = [formatError(error)]
73 runner.emit('test end', test)
74 } else {
75 test.$errors.push(formatError(error))
76 }
77 })
78
79 runner.on('test end', function (test) {
80 var skipped = test.pending === true
81
82 var result = {
83 id: '',
84 description: test.title,
85 suite: [],
86 success: test.state === 'passed',
87 skipped: skipped,
88 time: skipped ? 0 : test.duration,
89 log: test.$errors || []
90 }
91
92 var pointer = test.parent
93 while (!pointer.root) {
94 result.suite.unshift(pointer.title)
95 pointer = pointer.parent
96 }
97
98 tc.result(result)
99 })
100 }
101}
102/* eslint-disable no-unused-vars */
103var createMochaStartFn = function (mocha) {
104 /* eslint-enable no-unused-vars */
105 return function (config) {
106 var clientArguments
107 config = config || {}
108 clientArguments = config.args
109
110 if (clientArguments) {
111 if (Object.prototype.toString.call(clientArguments) === '[object Array]') {
112 arrayReduce(clientArguments, function (isGrepArg, arg) {
113 if (isGrepArg) {
114 mocha.grep(new RegExp(arg))
115 } else if (arg === '--grep') {
116 return true
117 } else {
118 var match = /--grep=(.*)/.exec(arg)
119
120 if (match) {
121 mocha.grep(new RegExp(match[1]))
122 }
123 }
124 return false
125 }, false)
126 }
127
128 /**
129 * TODO(maksimrv): remove when karma-grunt plugin will pass
130 * clientArguments how Array
131 */
132 if (clientArguments.grep) {
133 mocha.grep(clientArguments.grep)
134 }
135 }
136
137 mocha.run()
138 }
139}
140
141// Default configuration
142var mochaConfig = {
143 reporter: createMochaReporterConstructor(window.__karma__, window.location.pathname),
144 ui: 'bdd',
145 globals: ['__cov*']
146}
147
148// Pass options from client.mocha to mocha
149/* eslint-disable no-unused-vars */
150var createConfigObject = function (karma) {
151 /* eslint-enable no-unused-vars */
152
153 if (!karma.config || !karma.config.mocha) {
154 return mochaConfig
155 }
156
157 // Copy all properties to mochaConfig
158 for (var key in karma.config.mocha) {
159 // except for reporter
160 if (key === 'reporter') {
161 continue
162 }
163
164 // and merge the globals if they exist.
165 if (key === 'globals') {
166 mochaConfig.globals = mochaConfig.globals.concat(karma.config.mocha[key])
167 continue
168 }
169
170 mochaConfig[key] = karma.config.mocha[key]
171 }
172 return mochaConfig
173}
174
175
176window.__karma__.start = createMochaStartFn(window.mocha)
177window.mocha.setup(createConfigObject(window.__karma__))
178})(window);