UNPKG

6.86 kBJavaScriptView Raw
1/* globals jasmine, describe, beforeEach, it, expect */
2var jasmineReporters = require("../index");
3
4var env, suite, subSuite, subSubSuite,
5 reporter, suiteId=0, specId=0, noop=function(){};
6function fakeSpec(ste, name) {
7 var s = new jasmine.Spec({
8 env: env,
9 id: specId++,
10 description: name,
11 queueableFn: {fn: noop},
12 });
13 ste.addChild(s);
14 return s;
15}
16function fakeSuite(name, parentSuite) {
17 var s = new jasmine.Suite({
18 env: env,
19 id: suiteId++,
20 description: name,
21 parentSuite: parentSuite || jasmine.createSpy("pretend top suite") // I'm sure there's a good reason Jasmine does this...
22 });
23 if (parentSuite) {
24 parentSuite.addChild(s);
25 }
26 else {
27
28 env._suites = env._suites || [];
29 env._suites.push(s);
30 }
31 return s;
32}
33
34function setupReporterWithOptions(options) {
35 reporter = new jasmineReporters.TeamCityReporter(options);
36}
37
38// make sure reporter is set before calling this
39function triggerRunnerEvents() {
40 var logs = [];
41 var _stdoutWrite = process.stdout.write;
42 process.stdout.write = function() {
43 logs.push(arguments[0]);
44 };
45 reporter.jasmineStarted();
46 for (var i=0; i<env._suites.length; i++) {
47 var s = env._suites[i];
48 triggerSuiteEvents(s);
49 }
50 reporter.jasmineDone();
51 process.stdout.write = _stdoutWrite;
52 return logs;
53}
54function triggerSuiteEvents(ste) {
55 reporter.suiteStarted(ste.result);
56 var thing;
57 for (var i=0; i<ste.children.length; i++) {
58 thing = ste.children[i];
59 if (thing instanceof jasmine.Suite) {
60 triggerSuiteEvents(thing);
61 } else {
62 reporter.specStarted(thing.result);
63 reporter.specDone(thing.result);
64 }
65 }
66 reporter.suiteDone(ste.result);
67}
68
69describe("TeamCityReporter", function(){
70 beforeEach(function(){
71 env = new jasmine.Env();
72 suite = fakeSuite("ParentSuite");
73 subSuite = fakeSuite("SubSuite", suite);
74 subSubSuite = fakeSuite("SubSubSuite", subSuite);
75 fakeSuite("SiblingSuite");
76 var failedSpec = fakeSpec(subSubSuite, "should be failed");
77 failedSpec.result.status = "failed";
78 failedSpec.result.failedExpectations.push({
79 passed: false,
80 message: "Expected true to be false.",
81 expected: false,
82 actual: true,
83 matcherName: "toBe",
84 stack: 'Stack trace! Stack traces are cool & can have "special" characters <3\n\n Neat: yes.'
85 });
86 fakeSpec(subSuite, "should be one level down");
87 fakeSpec(subSubSuite, "(1) should be two levels down");
88 });
89
90 describe("General behavior", function() {
91 beforeEach(function() {
92 setupReporterWithOptions();
93 this.logs = triggerRunnerEvents();
94 });
95 it("should log testSuiteStarted and testSuiteFinished events", function() {
96 // we have 4 suites
97 var started = this.logs.filter(l=>l.indexOf("testSuiteStarted") > -1);
98 var finished = this.logs.filter(l=>l.indexOf("testSuiteStarted") > -1);
99 expect(started.length).toBe(4);
100 expect(finished.length).toBe(4);
101 expect(started[0].indexOf("name='ParentSuite'")).toBeGreaterThan(-1);
102 });
103 it("should log testStarted and testFinished events", function() {
104 // we have 3 specs
105 var started = this.logs.filter(l=>l.indexOf("testStarted") > -1);
106 var finished = this.logs.filter(l=>l.indexOf("testStarted") > -1);
107 expect(started.length).toBe(3);
108 expect(finished.length).toBe(3);
109 expect(started[0].indexOf("name='should be failed'")).toBeGreaterThan(-1);
110 });
111 it("should log testFailed event including reason and stack trace", function() {
112 // we have 1 failure
113 var failed = this.logs.filter(l=>l.indexOf("testFailed") > -1);
114 expect(failed.length).toBe(1);
115 expect(failed[0].indexOf("name='should be failed'")).toBeGreaterThan(-1);
116 expect(failed[0].indexOf("message='Expected true to be false.'")).toBeGreaterThan(-1);
117 expect(failed[0].indexOf("details='Stack trace! Stack traces are cool")).toBeGreaterThan(-1);
118 });
119 });
120
121 describe("modifySuiteName", function() {
122 var modification = "-modified";
123 beforeEach(function() {
124 setupReporterWithOptions({modifySuiteName: function(name) { return name + modification; }});
125 this.logs = triggerRunnerEvents();
126 });
127 it("should use the modification for suite names", function() {
128 // we have 4 suites
129 var started = this.logs.filter(l=>l.indexOf("testSuiteStarted") > -1);
130 started.forEach(e=>expect(e.indexOf(modification)).toBeGreaterThan(-1));
131 expect(started[0].indexOf("name='ParentSuite-modified'")).toBeGreaterThan(-1);
132 });
133 it("should *not* use the modification for spec names", function() {
134 // we have 3 specs
135 var started = this.logs.filter(l=>l.indexOf("testStarted") > -1);
136 started.forEach(e=>expect(e.indexOf(modification)).toBe(-1));
137 });
138 });
139});
140
141// [ { '0': '##teamcity[progressStart \'Running Jasmine Tests\']\n' },
142// { '0': '##teamcity[testSuiteStarted name=\'ParentSuite\' timestamp=\'2016-06-17T06:50:35.687\']\n' },
143// { '0': '##teamcity[testSuiteStarted name=\'SubSuite\' timestamp=\'2016-06-17T06:50:35.687\']\n' },
144// { '0': '##teamcity[testSuiteStarted name=\'SubSubSuite\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
145// { '0': '##teamcity[testStarted name=\'should be failed\' captureStandardOutput=\'true\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
146// { '0': '##teamcity[testFailed name=\'should be failed\' message=\'Expected true to be false.\' details=\'Stack trace! Stack trackes are cool & can have "special" characters <3|n|n Neat: yes.\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
147// { '0': '##teamcity[testFinished name=\'should be failed\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
148// { '0': '##teamcity[testSuiteFinished name=\'SubSubSuite\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
149// { '0': '##teamcity[testSuiteFinished name=\'SubSuite\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
150// { '0': '##teamcity[testSuiteFinished name=\'ParentSuite\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
151// { '0': '##teamcity[testSuiteStarted name=\'SiblingSuite With Invalid Chars & < > " |\' || : \\ /\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
152// { '0': '##teamcity[testSuiteFinished name=\'SiblingSuite With Invalid Chars & < > " |\' || : \\ /\' timestamp=\'2016-06-17T06:50:35.688\']\n' },
153// { '0': '##teamcity[progressFinish \'Running Jasmine Tests\']\n' } ]