This repository has been archived by the owner on Nov 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
76 lines (60 loc) · 1.62 KB
/
index.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
const ora = require('ora');
const isObj = require('is-obj');
const fastRet = require('fastret');
const jkValidate = require('json-key-validate');
const spinner = ora();
const REQUIRED_KEYS = ['name', 'url', 'method', 'response'];
function FastRet(data) {
if (!isObj(data)) {
throw new Error('Invalid JSON');
}
// Validates if data has required parameters
if (!this.isValid(data)) {
throw new Error('Required Parameters Missing');
}
this.data = data;
this.results = [];
}
// Execute the tests and store results
FastRet.prototype.execute = async function() {
for (let i=0; i<this.data.length; i++) {
const data = this.data[i];
// Start spinner
spinner.start([data.name]);
// Calls the api and validates the response with expected response
let isPassed = false;
let errorMessage = '';
try {
isPassed = await fastRet(data);
} catch (err) {
isPassed = false;
errorMessage = err.message;
}
// Storing test results
this.results.push({
name: data.name,
passed: isPassed,
errorMessage: errorMessage
});
// Stop Spinner
// Show tick or cross based on test result
if (isPassed) {
spinner.succeed([`Passed: ${data.name}`]);
} else {
spinner.fail([`Failed: ${data.name} => (${errorMessage})`]);
}
}
};
// Validates if input test api structure contains all required parameters
FastRet.prototype.isValid = function(data) {
if (!data) {
return false;
}
for (let i=0; i<data.length; i++) {
if (!jkValidate(data[i], REQUIRED_KEYS)) {
return false;
}
}
return true;
};
module.exports = FastRet;