forked from xjamundx/jshint-junit-reporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
163 lines (152 loc) · 5.35 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var assert = require('assert');
var path = require('path');
var fs = require('fs');
var outputFile = __dirname + '/test.xml';
var tests = [];
var mock1 = []; // 0 error
var mock2 = [{ // 1 error
file: './my/file.js',
error: {
id: '(error)',
raw: 'Missing semicolon.',
code: 'W033',
evidence: '}',
line: 36,
character: 2,
scope: '(main)',
a: undefined,
b: undefined,
c: undefined,
d: undefined,
reason: 'Missing semicolon.'
}
}];
var mock3 = [ { file: 'reporter.js',
error:
{ id: '(error)',
raw: 'Missing "use strict" statement.',
evidence: ' var pairs = {',
line: 9,
character: 3,
scope: '(main)',
a: undefined,
b: undefined,
c: undefined,
d: undefined,
reason: 'Missing "use strict" statement.' } },
{ file: 'reporter.js',
error:
{ id: '(error)',
raw: 'Missing "use strict" statement.',
evidence: ' var count = failures.length;',
line: 25,
character: 3,
scope: '(main)',
a: undefined,
b: undefined,
c: undefined,
d: undefined,
reason: 'Missing "use strict" statement.' } },
{ file: 'reporter.js',
error:
{ id: '(error)',
raw: 'Missing "use strict" statement.',
evidence: ' var msg = [];',
line: 34,
character: 3,
scope: '(main)',
a: undefined,
b: undefined,
c: undefined,
d: undefined,
reason: 'Missing "use strict" statement.' } },
{ file: 'reporter.js',
error:
{ id: '(error)',
raw: 'Missing "use strict" statement.',
evidence: ' console.log(results)',
line: 45,
character: 3,
scope: '(main)',
a: undefined,
b: undefined,
c: undefined,
d: undefined,
reason: 'Missing "use strict" statement.' } } ];
var expected1 = '';
var expected2 = '<?xml version="1.0" encoding="utf-8"?><testsuite name="jshint" tests="1" failures="1" errors="0"><testcase name="my/file.js"><failure message="1 JSHINT Failure">1. line 36, char 2: Missing semicolon.</failure></testcase></testsuite>';
var expected3 = '<?xml version="1.0" encoding="utf-8"?><testsuite name="jshint" tests="1" failures="4" errors="0"><testcase name="reporter.js"><failure message="4 JSHint Failures">1. line 9, char 3: Missing "use strict" statement. 2. line 25, char 3: Missing "use strict" statement. 3. line 34, char 3: Missing "use strict" statement. 4. line 45, char 3: Missing "use strict" statement.</failure></testcase></testsuite>';
var expected3 = '<?xml version="1.0" encoding="utf-8"?><testsuite name="jshint" tests="1" failures="4" errors="0"><testcase name="reporter.js"><failure message="4 JSHint Failures">1. line 9, char 3: Missing "use strict" statement.2. line 25, char 3: Missing "use strict" statement.3. line 34, char 3: Missing "use strict" statement.4. line 45, char 3: Missing "use strict" statement.</failure></testcase></testsuite>';
var strip = new RegExp('[\t\n]', 'g');
var oldWrite = process.stdout.write.bind(process.stdout);
// sanity check the path name provided
tests.push(function() {
var reporter = path.resolve('./reporter.js');
assert.equal(reporter, require('./index'));
assert.ok(require(reporter));
});
// make sure no errors get thrown, yep, ghetto test #2
tests.push(function() {
var formatter = require('./reporter.js');
});
// test output for 0 errors
tests.push(function() {
var formatter = require('./reporter.js');
process.stdout.write = function() {};
var results = formatter.reporter(mock1);
process.stdout.write = oldWrite;
var expected = ['<?xml', '<testcase', '<testsuite'];
if (!results) throw new Error('You should have some XML!');
expected.forEach(function(str) {
if (results.indexOf(str) === -1) {
throw new Error('Expected to see ' + str);
}
})
});
// test output for 1 error
tests.push(function() {
process.stdout.write = function() {};
var formatter = require('./reporter.js');
var results = formatter.reporter(mock2);
process.stdout.write = oldWrite;
if (results.replace(strip, '') !== expected2.replace(strip, '')) {
throw new Error('Unexpected results');
}
});
// test output for > 1 error
tests.push(function() {
process.stdout.write = function() {};
var formatter = require('./reporter.js');
var results = formatter.reporter(mock3);
process.stdout.write = oldWrite;
if (results.replace(strip, '') !== expected3.replace(strip, '')) {
throw new Error('Unexpected results');
}
});
// test for output file
tests.push(function() {
var formatter = require('./reporter.js');
var results = formatter.reporter(mock3, null, { outputFile: outputFile });
var success = false;
var content;
if (fs.existsSync(outputFile)) {
content = fs.readFileSync(outputFile, "utf8");
if (content.replace(strip, '') === expected3.replace(strip, ''))
success = true;
fs.unlinkSync(outputFile);
}
if (!success) {
throw new Error('Unexpected results');
}
});
// lint this file, but only if you have jshint
tests.push(function() {
console.log('Don\'t forget to lint this file also!');
console.log('jshint --reporter=reporter.js reporter.js\n\n');
});
// run all tests
tests.forEach(function(test) {
test();
});
// seems like we're ok
console.log('all of the tests passed!');