UNPKG

3.7 kBJavaScriptView Raw
1(function(window) {
2
3function createQUnitConfig (karma, defaultConfig) { // eslint-disable-line no-unused-vars
4 var config = defaultConfig || {}
5
6 if (!karma.config || !karma.config.qunit) {
7 return config
8 }
9
10 for (var key in karma.config.qunit) {
11 config[key] = karma.config.qunit[key]
12 }
13
14 return config
15}
16
17function createQUnitStartFn (tc, runnerPassedIn) { // eslint-disable-line no-unused-vars
18 return function () {
19 var FIXTURE_ID = 'qunit-fixture'
20 var runner = runnerPassedIn || window.QUnit
21 var totalNumberOfTest = 0
22 var timer = null
23 var testResult = {}
24 var supportsTestTracking = false
25 var config = (tc.config && tc.config.qunit) || {}
26
27 if (config.showUI) {
28 var ui = document.createElement('div')
29 ui.id = 'qunit'
30 document.body.appendChild(ui)
31 }
32
33 if (runner.begin) {
34 runner.begin(function (args) {
35 if (args && typeof args.totalTests === 'number') {
36 tc.info({ total: args.totalTests })
37 supportsTestTracking = true
38 }
39 })
40 }
41
42 runner.done(function () {
43 if (!supportsTestTracking) {
44 tc.info({ total: totalNumberOfTest })
45 }
46
47 tc.complete({
48 coverage: window.__coverage__
49 })
50 })
51
52 runner.testStart(function (test) {
53 totalNumberOfTest += 1
54 timer = new Date().getTime()
55 testResult = { success: true, errors: [] }
56
57 if (typeof document !== 'undefined' && document.getElementById && document.createElement && document.body) {
58 // create a qunit-fixture element to match behaviour of regular qunit
59 // runner. The fixture is only removed at the start of a subsequent test
60 // so it can be inspected after a test run.
61 var fixture = document.getElementById(FIXTURE_ID)
62 if (fixture) {
63 fixture.parentNode.removeChild(fixture)
64 }
65 fixture = document.createElement('div')
66 fixture.id = FIXTURE_ID
67 // style to match qunit runner's CSS
68 fixture.style.position = 'absolute'
69 fixture.style.left = '-10000px'
70 fixture.style.top = '-10000px'
71 fixture.style.width = '1000px'
72 fixture.style.height = '1000px'
73 document.body.appendChild(fixture)
74 }
75 })
76
77 runner.log(function (details) {
78 if (!details.result) {
79 var msg = ''
80
81 if (details.message) {
82 msg += details.message + '\n'
83 }
84
85 if (typeof details.expected !== 'undefined') {
86 msg += 'Expected: ' + runner.dump.parse(details.expected) + '\n' + 'Actual: ' + runner.dump.parse(details.actual) + '\n'
87 }
88
89 if (details.source) {
90 msg += details.source + '\n'
91 }
92
93 testResult.success = false
94 testResult.errors.push(msg)
95 }
96 })
97
98 runner.testDone(function (test) {
99 var result = {
100 description: test.name,
101 suite: test.module && [test.module] || [],
102 success: testResult.success,
103 skipped: test.skipped,
104 log: testResult.errors || [],
105 time: new Date().getTime() - timer
106 }
107
108 if (result.description.indexOf('global failure') !== -1) {
109 return
110 }
111
112 tc.result(result)
113 })
114
115 runner.load()
116 runner.start()
117 }
118}
119
120var config = createQUnitConfig(window.__karma__, {
121 autostart: false
122});
123
124for (var key in config) {
125 window.QUnit.config[key] = config[key];
126}
127
128if (window.removeEventListener) {
129 window.removeEventListener('load', window.QUnit.load, false);
130} else if (window.detachEvent) {
131 window.detachEvent('onload', window.QUnit.load);
132}
133
134window.__karma__.start = createQUnitStartFn(window.__karma__);
135})(typeof window !== 'undefined' ? window : global);