-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
test-runner.js
79 lines (69 loc) · 2.23 KB
/
test-runner.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
const analyser = require('./assertion-analyser');
const EventEmitter = require('events').EventEmitter;
const Mocha = require('mocha'),
fs = require('fs'),
path = require('path');
const mocha = new Mocha();
const testDir = './tests'
// Add each .js file to the mocha instance
fs.readdirSync(testDir).filter(function (file) {
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function (file) {
mocha.addFile(
path.join(testDir, file)
);
});
const emitter = new EventEmitter();
emitter.run = function () {
const tests = [];
let context = "";
const separator = ' -> ';
// Run the tests.
try {
const runner = mocha.ui('tdd').run()
.on('test end', function (test) {
// remove comments
let body = test.body.replace(/\/\/.*\n|\/\*.*\*\//g, '');
// collapse spaces
body = body.replace(/\s+/g, ' ');
const obj = {
title: test.title,
context: context.slice(0, -separator.length),
state: test.state,
// body: body,
assertions: analyser(body)
};
tests.push(obj);
})
.on('end', function () {
emitter.report = tests;
emitter.emit('done', tests)
})
.on('suite', function (s) {
context += (s.title + separator);
})
.on('suite end', function (s) {
context = context.slice(0, -(s.title.length + separator.length))
})
} catch (e) {
throw (e);
}
};
module.exports = emitter;
/*
* Mocha.runner Events:
* can be used to build a better custom report
*
* - `start` execution started
* - `end` execution complete
* - `suite` (suite) test suite execution started
* - `suite end` (suite) all tests (and sub-suites) have finished
* - `test` (test) test execution started
* - `test end` (test) test completed
* - `hook` (hook) hook execution started
* - `hook end` (hook) hook complete
* - `pass` (test) test passed
* - `fail` (test, err) test failed
* - `pending` (test) test pending
*/