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
"use strict";
var _ = require("lodash");
var Assertion = require("chai").Assertion;
var U = require("glace-utils");
Assertion.prototype.correspond = function (cond, msg) {
var matchers, expVal;
if (msg) this.__flags.message = msg;
if (typeof(cond) === "object") {
if (Object.keys(cond).length !== 1) {
throw new Error("Condition should contain only one key-value pair");
}
matchers = Object.keys(cond)[0];
expVal = Object.values(cond)[0];
} else if (typeof(cond) === "string") {
matchers = cond;
} else {
throw new Error("Condition should be string or object only");
}
matchers = _.filter(_.split(matchers, " "));
var predicate = this;
for (var matcher of matchers) {
predicate = predicate[matcher];
if (!predicate) throw new TypeError(`Undefined matcher '${matcher}'`);
}
if (expVal) predicate.call(this, expVal);
return this;
};
Assertion.prototype.waitFor = async function (cond, timeout, msg) {
var err = null;
var predicate = async () => {
try {
return await new Assertion(
await this.__flags.object(),
this.__flags.message).correspond(cond, msg);
} catch (e) {
err = e;
return false;
}
};
var result = await U.waitFor(predicate, { timeout: timeout });
if (result) return this;
if (err) {
throw err;
} else {
throw new Error("Unexpected matcher error");
}
};