Skip to content

Commit a36790c

Browse files
Merge pull request #4 from getmimo/add-custom-test-runner
Add custom test runner
2 parents ead1a11 + 8fb53ed commit a36790c

9 files changed

Lines changed: 1123 additions & 178 deletions

File tree

dist/testrunner.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
const deepEqual = require('deep-equal');
3+
4+
// The stack of beforeEach callbacks
5+
const beforeEachStack = [[]];
6+
// Runs every beforeEach callback in the stack
7+
const runEveryBeforeEach = () => {
8+
beforeEachStack.forEach(level => level.forEach(cb => cb()));
9+
};
10+
const beforeEach = cb => {
11+
beforeEachStack[beforeEachStack.length - 1].push(cb);
12+
};
13+
14+
// Keeps some counters used to print the summary after the execution of a test suite is completed
15+
const summary = { success: true, testResults: [] };
16+
let tempResult = {};
17+
18+
// Declares a test unit
19+
const test = (input, title, cb) => {
20+
runEveryBeforeEach();
21+
tempResult.input = input;
22+
try {
23+
cb(input);
24+
summary.testResults.push({
25+
title,
26+
passed: true,
27+
result: tempResult,
28+
});
29+
tempResult = {};
30+
} catch (e) {
31+
summary.success = false;
32+
summary.testResults.push({
33+
title,
34+
passed: false,
35+
result: tempResult,
36+
});
37+
tempResult = {};
38+
}
39+
};
40+
41+
// Prints the test summary and finishes the process with the appropriate exit code
42+
const end = () => {
43+
console.log(`${JSON.stringify(summary)}`);
44+
if (summary.fail > 0) process.exit(1);
45+
process.exit(0);
46+
};
47+
48+
// ASSERTIONS
49+
50+
const isEqual = (actual, expected) => {
51+
tempResult.actual = actual;
52+
tempResult.expected = expected;
53+
if (!deepEqual(actual, expected)) {
54+
throw Error(actual);
55+
}
56+
return actual;
57+
};
58+
59+
module.exports = {
60+
test,
61+
end,
62+
beforeEach,
63+
summary,
64+
isEqual,
65+
};

0 commit comments

Comments
 (0)