Skip to content

Commit 131aae5

Browse files
committed
Fix waitForText() timeout and reject messages
1 parent 871e48f commit 131aae5

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

test/helpers/wait-for.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ export function waitForFunction(fn, arg, options = {}) {
3737
timeElapsed += settings.delay;
3838

3939
if (timeElapsed >= settings.timeout) {
40-
reject(
41-
`waitForFunction() did not return a truthy value\n\n${fn.toString()}\n\n`
42-
);
40+
const msg = `waitForFunction did not return a truthy value (${
41+
settings.timeout
42+
} ms): ${fn.toString()}\n`;
43+
44+
reject(msg);
4345
}
4446
}, settings.delay);
4547
});
@@ -71,9 +73,9 @@ export function waitForSelector(cssSelector, options = {}) {
7173
timeElapsed += settings.delay;
7274

7375
if (timeElapsed >= settings.timeout) {
74-
reject(
75-
`waitForSelector() was unable to find CSS selector ${cssSelector}`
76-
);
76+
const msg = `waitForSelector did not match CSS selector '${cssSelector}' (${settings.timeout} ms)`;
77+
78+
reject(msg);
7779
}
7880
}, settings.delay);
7981
});
@@ -94,20 +96,31 @@ export function waitForText(cssSelector, text, options = {}) {
9496
};
9597

9698
return new Promise((resolve, reject) => {
99+
let timeElapsed = 0;
100+
97101
waitForSelector(cssSelector, settings)
98102
.then(elm => {
99-
const isMatch = elm.textContent.includes(text);
100-
101-
if (isMatch) {
102-
resolve(elm);
103-
} else {
104-
reject(
105-
`waitForText() did not find "${text}" in CSS selector ${cssSelector}`
106-
);
107-
}
103+
const int = setInterval(() => {
104+
const isMatch = elm.textContent.includes(text);
105+
106+
if (isMatch) {
107+
clearInterval(int);
108+
resolve(true);
109+
}
110+
111+
timeElapsed += settings.delay;
112+
113+
if (timeElapsed >= settings.timeout) {
114+
const msg = `waitForText did not find '${text}' in CSS selector '${cssSelector}' (${settings.timeout} ms): '${elm.textContent}'`;
115+
116+
reject(msg);
117+
}
118+
}, settings.delay);
108119
})
109120
.catch(() => {
110-
reject(`waitForText() was unable to find CSS selector ${cssSelector}`);
121+
const msg = `waitForText did not match CSS selector '${cssSelector}' (${settings.timeout} ms)`;
122+
123+
reject(msg);
111124
});
112125
});
113126
}

0 commit comments

Comments
 (0)