This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite.js
144 lines (116 loc) · 2.46 KB
/
suite.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
const path = require('path');
const stackTrace = require('./stackTrace.js');
const Status = {
PASS: Symbol.for('pass'),
SKIP: Symbol.for('skip'),
FAIL: Symbol.for('fail')
};
class TestCase {
constructor(text, handler) {
this.tags = [];
this.description = text;
this.state = '';
this.filename = '';
this.lineNumber = 0;
this.only = false;
this.todo = false;
this.error = null;
this.environmentError = null;
this._handler = handler;
this.status = Status.PASS;
}
async run(...args) {
try {
await this._handler(...args, this);
} catch (e) {
this.fail(e);
}
}
pass() {
}
fail(e) {
this.status = Status.FAIL;
this.error = e;
}
}
class Suite {
constructor(name = null) {
this.tests = [];
this.name = name;
let stack = stackTrace.parse(new Error());
this.filename = stack[1].fileName;
this.hasOnly = false;
this.onlyTest = null;
this.beforeEachFunction = async () => { };
}
getName() {
if (this.name)
return this.name;
let str = path.basename(this.filename);
return str.charAt(0).toUpperCase() + str.substr(1);
}
_test(text, fn) {
let stack = stackTrace.parse(new Error());
let caller = stack[2];
let test = new TestCase(text, fn);
test.filename = caller.fileName;
test.lineNumber = caller.lineNumber;
return test;
}
/**
* @param {String} text
* @param {Function} fn
*/
test(text, fn) {
let test = this._test(text, fn);
this.tests.push(test);
}
/**
* @param {String} text
* @param {Function} fn
*/
add(text, fn) {
let test = this._test(text, fn);
this.tests.push(test);
}
/**
* @param {String} text
* @param {Function} fn
*/
todo(text, fn) {
let test = this._test(text, fn);
test.todo = true;
this.tests.push(test);
}
/**
* @param {String} text
* @param {Function} fn
*/
skip(text, fn) {
let test = this._test(text, fn);
test.status = Status.SKIP;
this.tests.push(test);
}
/**
* @param {String} text
* @param {Function} fn
*/
only(text, fn) {
let test = this._test(text, fn);
if (this.hasOnly) {
throw new Error('There are more than one only test');
}
this.hasOnly = true;
this.onlyTest = test;
test.only = true;
this.tests.push(test);
}
/**
* @param {Function} fn
*/
beforeEach(fn) {
this.beforeEachFunction = fn;
}
}
module.exports = Suite;
module.exports.Status = Status;