|
| 1 | +define([ |
| 2 | + 'intern!object', |
| 3 | + 'intern/chai!assert', |
| 4 | + '../../../lib/Suite', |
| 5 | + '../../../lib/Test', |
| 6 | + '../../../lib/reporters/teamcity' |
| 7 | +], function (registerSuite, assert, Suite, Test, reporter) { |
| 8 | + if (typeof console !== 'object') { |
| 9 | + // IE<10 does not provide a global console object when Developer Tools is turned off |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + function mockConsole(method, callback) { |
| 14 | + var oldMethod = console[method]; |
| 15 | + console[method] = callback; |
| 16 | + return { |
| 17 | + remove: function () { |
| 18 | + console[method] = oldMethod; |
| 19 | + } |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + var messagePatterns = { |
| 24 | + '/suite/start': '^##teamcity\\[testSuiteStarted name=\'{id}\'', |
| 25 | + '/suite/end': '^##teamcity\\[testSuiteFinished name=\'{id}\' duration=\'\\d+\'', |
| 26 | + '/test/start': '^##teamcity\\[testStarted name=\'{id}\'', |
| 27 | + '/test/end': '^##teamcity\\[testFinished name=\'{id}\' duration=\'\\d+\'', |
| 28 | + '/test/fail': '^##teamcity\\[testFailed name=\'{id}\' message=\'{message}\'' |
| 29 | + }; |
| 30 | + |
| 31 | + function testSuite(suite, topic, type) { |
| 32 | + var actualMessage, |
| 33 | + handle = mockConsole('log', function (message) { |
| 34 | + actualMessage = message; |
| 35 | + }), |
| 36 | + expected = messagePatterns[topic].replace('{id}', suite.id); |
| 37 | + |
| 38 | + try { |
| 39 | + reporter[topic](suite); |
| 40 | + assert.ok(actualMessage, 'console.log should be called when the reporter ' + topic + ' method is called'); |
| 41 | + assert.match( |
| 42 | + actualMessage, |
| 43 | + new RegExp(expected), |
| 44 | + 'console.log should be called with a ' + type + ' message'); |
| 45 | + } |
| 46 | + finally { |
| 47 | + handle.remove(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function testTest(test, topic, type) { |
| 52 | + var actualMessage, |
| 53 | + handle = mockConsole('log', function (message) { |
| 54 | + actualMessage = message; |
| 55 | + }), |
| 56 | + expected = messagePatterns[topic].replace('{id}', test.id); |
| 57 | + |
| 58 | + if (test.error) { |
| 59 | + expected = expected.replace('{message}', test.error.message); |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + reporter[topic](test); |
| 64 | + assert.ok(actualMessage, 'console.log should be called when the reporter ' + topic + ' method is called'); |
| 65 | + assert.match( |
| 66 | + actualMessage, |
| 67 | + new RegExp(expected), |
| 68 | + 'console.log should be called with a ' + type + ' message'); |
| 69 | + } |
| 70 | + finally { |
| 71 | + handle.remove(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + registerSuite({ |
| 76 | + name: 'intern/lib/reporters/teamcity', |
| 77 | + |
| 78 | + '/suite/start': function () { |
| 79 | + var suite = new Suite({ name: 'suite' }); |
| 80 | + testSuite(suite, '/suite/start', 'testSuiteStarted'); |
| 81 | + }, |
| 82 | + |
| 83 | + '/suite/end': (function () { |
| 84 | + var suite = { |
| 85 | + 'successful suite': function () { |
| 86 | + var suite = new Suite({ name: 'suite', tests: [ new Test({ hasPassed: true }) ] }); |
| 87 | + reporter._suiteStarts[suite.id] = 0; |
| 88 | + testSuite(suite, '/suite/end', 'testSuiteFinished'); |
| 89 | + }, |
| 90 | + |
| 91 | + 'failed suite': function () { |
| 92 | + var suite = new Suite({ name: 'suite', tests: [ new Test({ hasPassed: false }) ] }); |
| 93 | + reporter._suiteStarts[suite.id] = 0; |
| 94 | + testSuite(suite, '/suite/end', 'testSuiteFinished'); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + return suite; |
| 99 | + })(), |
| 100 | + |
| 101 | + '/test/start': function () { |
| 102 | + var test = new Test({ |
| 103 | + name: 'test', |
| 104 | + timeElapsed: 123, |
| 105 | + parent: { name: 'parent', id: 'parent' }, |
| 106 | + error: new Error('Oops') |
| 107 | + }); |
| 108 | + testTest(test, '/test/start', 'testStarted'); |
| 109 | + }, |
| 110 | + |
| 111 | + '/test/end': function () { |
| 112 | + var test = new Test({ |
| 113 | + name: 'test', |
| 114 | + timeElapsed: 123, |
| 115 | + parent: { name: 'parent', id: 'parent' }, |
| 116 | + error: new Error('Oops') |
| 117 | + }); |
| 118 | + testTest(test, '/test/end', 'testFinished'); |
| 119 | + }, |
| 120 | + |
| 121 | + '/test/fail': function () { |
| 122 | + var test = new Test({ |
| 123 | + name: 'test', |
| 124 | + timeElapsed: 123, |
| 125 | + parent: { name: 'parent', id: 'parent' }, |
| 126 | + error: new Error('Oops') |
| 127 | + }); |
| 128 | + testTest(test, '/test/fail', 'testFailed'); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments