1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
6 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
7 | };
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.Test = void 0;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const is_ci_1 = __importDefault(require("is-ci"));
|
19 | const retry_1 = __importDefault(require("retry"));
|
20 | const time_span_1 = __importDefault(require("time-span"));
|
21 | const Emitter_1 = require("../Emitter");
|
22 | const Callable_1 = require("../Callable");
|
23 | const utils_1 = require("../utils");
|
24 | const Exceptions_1 = require("../Exceptions");
|
25 | const Contracts_1 = require("../Contracts");
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | class Test {
|
35 | constructor(title, _resolveFn, _callback, options) {
|
36 | this.title = title;
|
37 | this._resolveFn = _resolveFn;
|
38 | this._callback = _callback;
|
39 | |
40 |
|
41 |
|
42 |
|
43 | this._regressionMessage = '';
|
44 | |
45 |
|
46 |
|
47 |
|
48 | this._retries = 0;
|
49 | |
50 |
|
51 |
|
52 |
|
53 | this._duration = 0;
|
54 | |
55 |
|
56 |
|
57 | this._error = null;
|
58 | |
59 |
|
60 |
|
61 | this._completed = false;
|
62 | this._todo = typeof (this._callback) !== 'function';
|
63 | this._timeout = options.timeout;
|
64 | this._regression = options.regression;
|
65 | if (options.skip) {
|
66 | this._skip = true;
|
67 | }
|
68 | else if (options.skipInCI && is_ci_1.default) {
|
69 | this._skip = true;
|
70 | }
|
71 | else if (options.runInCI && !is_ci_1.default) {
|
72 | this._skip = true;
|
73 | }
|
74 | }
|
75 | |
76 |
|
77 |
|
78 |
|
79 | get _isHardException() {
|
80 | return (0, utils_1.isCoreException)(this._error);
|
81 | }
|
82 | |
83 |
|
84 |
|
85 | _runTest() {
|
86 | return new Promise((resolve, reject) => {
|
87 | const op = retry_1.default.operation({ retries: this._retries, factor: 1 });
|
88 | op.attempt(async () => {
|
89 | (0, Callable_1.Callable)(this._resolveFn, this._callback, this._timeout)
|
90 | .then(resolve)
|
91 | .catch((error) => {
|
92 | if (op.retry(error)) {
|
93 | return;
|
94 | }
|
95 | reject(op.mainError());
|
96 | });
|
97 | });
|
98 | });
|
99 | }
|
100 | |
101 |
|
102 |
|
103 |
|
104 | toJSON() {
|
105 | let status = Contracts_1.ITestStatus.PENDING;
|
106 | if (this._todo) {
|
107 | status = Contracts_1.ITestStatus.TODO;
|
108 | }
|
109 | else if (this._skip) {
|
110 | status = Contracts_1.ITestStatus.SKIPPED;
|
111 | }
|
112 | else if (this._completed && this._error) {
|
113 | status = (this._regression && !this._isHardException) ? Contracts_1.ITestStatus.PASSED : Contracts_1.ITestStatus.FAILED;
|
114 | }
|
115 | else if (this._completed && !this._error) {
|
116 | status = Contracts_1.ITestStatus.PASSED;
|
117 | }
|
118 | return {
|
119 | title: this.title,
|
120 | status: status,
|
121 | regression: this._regression,
|
122 | regressionMessage: this._regressionMessage,
|
123 | duration: this._duration,
|
124 | error: this._error,
|
125 | };
|
126 | }
|
127 | |
128 |
|
129 |
|
130 |
|
131 | retry(counts) {
|
132 | if (typeof (counts) !== 'number') {
|
133 | throw new Error('"test.retry" expects a number value');
|
134 | }
|
135 | this._retries = counts;
|
136 | return this;
|
137 | }
|
138 | |
139 |
|
140 |
|
141 | timeout(duration) {
|
142 | if (typeof (duration) !== 'number') {
|
143 | throw new Error('"test.timeout" expects a number value');
|
144 | }
|
145 | this._timeout = duration;
|
146 | return this;
|
147 | }
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | async run() {
|
167 | Emitter_1.emitter.emit(Contracts_1.IEvents.TESTSTARTED, this.toJSON());
|
168 | const start = (0, time_span_1.default)();
|
169 |
|
170 | if (!this._todo && !this._skip) {
|
171 | |
172 |
|
173 |
|
174 | try {
|
175 | await this._runTest();
|
176 | |
177 |
|
178 |
|
179 | if (this._regression) {
|
180 | throw new Exceptions_1.RegressionException('Expected regression test to fail');
|
181 | }
|
182 | }
|
183 | catch (error) {
|
184 | this._error = error;
|
185 | if (!this._isHardException && this._regression) {
|
186 | this._regressionMessage = error.message;
|
187 | }
|
188 | }
|
189 | }
|
190 | this._duration = start.rounded();
|
191 | this._completed = true;
|
192 | Emitter_1.emitter.emit(Contracts_1.IEvents.TESTCOMPLETED, this.toJSON());
|
193 | }
|
194 | }
|
195 | exports.Test = Test;
|