-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCaseParser.js
More file actions
81 lines (73 loc) · 2.99 KB
/
TestCaseParser.js
File metadata and controls
81 lines (73 loc) · 2.99 KB
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
var path = require('path');
var colors = require('colors');
var Table = require('cli-table2');
var urldecode = require('urldecode');
const wrapAnsi = require('wrap-ansi');
var Call = require('./Call.js');
var Logger = require('./Logger.js');
var Assertions = require('./Assertions.js');
var Case = function(testCase) {
// console.log(testCase); // for debug
var logger = new Logger();
this.failed = false;
var call = new Call(path.resolve(__dirname + path.sep + 'collections' + path.sep + testCase.collection + path.sep + testCase.call + '.json'), testCase.endpoint);
call.setInputTestData(testCase.input);
call.send();
this.response = call.response;
this.requestUrl = call.url;
// test case should compare response with expected results ???
var assertions = new Assertions(call.response, testCase.expected);
if (assertions.failReasons.length > 0) {
// FAIL
this.failed = true;
console.log((" \u2715 " + testCase.description).red);
var tableSize = [16, 124];
var width = process.stdout.columns;
if (width !== undefined) {
tableSize = [16, width - 21];
}
var table = new Table({
colWidths: tableSize,
wordWrap: true
});
var reasons = '';
assertions.failReasons.forEach(function (reason) {
reasons = reasons + reason + "\n";
});
table.push(
{ 'Fail reasons': reasons.trim() },
// { 'URL': call.url }
{ 'URL': wrapAnsi(urldecode(call.url), width - 25, {hard: true}) }
);
if (call.method != 'GET' && call.method !== null) {
table.push(
{ 'Request body': JSON.stringify(call.body, null, 4) }
);
}
var tableResponse = '';
if(JSON.stringify(call.response.json, null, 4).length > 2000) {
tableResponse = JSON.stringify(call.response.json, null, 4).substring(0, 2000) + '\n...';
} else {
tableResponse = JSON.stringify(call.response.json, null, 4);
}
table.push(
{ 'Response': tableResponse }
);
console.log(table.toString());
if(process.listeners('exit').length < 1) {
process.on('exit', function() {throw new Error("Test failed!");} ); // Make build failed
}
} else {
// PASS
console.log((" \u2713 " + testCase.description).green);
}
if (testCase.log !== undefined) {
logger.addToLog(testCase.log, 'Description: ' + testCase.description + '\n');
logger.addToLog(testCase.log, 'URL: ' + call.url + '\n');
if (call.method != 'GET' && call.method !== null) {
logger.addToLog(testCase.log, 'Request body: ' + JSON.stringify(call.body, null, 4) + '\n');
}
logger.addToLog(testCase.log, 'Response: ' + JSON.stringify(call.response.json, null, 4) + '\n');
}
};
module.exports = Case;