LibWeb: Prevent calling test() twice
Calling test() multiple times in the same test file is not actually valid, and can cause the following test to hang forever. So let's stop doing that in the one test that did so, and also prevent the same mistake happening again. :^) Throwing an exception on subsequent test() calls means that we don't hang, the test will fail with missing output, and we get a log message explaining why.
This commit is contained in:
parent
99fc3c7551
commit
1e6cd19b28
Notes:
sideshowbarker
2024-07-17 18:46:30 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/1e6cd19b28 Pull-request: https://github.com/SerenityOS/serenity/pull/22391 Reviewed-by: https://github.com/kalenikaliaksandr Reviewed-by: https://github.com/trflynn89
2 changed files with 16 additions and 7 deletions
|
@ -13,12 +13,10 @@
|
|||
<script src="include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
const rect = document.getElementById("box").getBoundingClientRect();
|
||||
println(JSON.stringify(rect));
|
||||
});
|
||||
const box_rect = document.getElementById("box").getBoundingClientRect();
|
||||
println(JSON.stringify(box_rect));
|
||||
|
||||
test(() => {
|
||||
const rect = document.getElementById("inline").getBoundingClientRect();
|
||||
println(JSON.stringify(rect));
|
||||
const inline_rect = document.getElementById("inline").getBoundingClientRect();
|
||||
println(JSON.stringify(inline_rect));
|
||||
});
|
||||
</script>
|
||||
|
|
|
@ -1,4 +1,11 @@
|
|||
var __outputElement = null;
|
||||
let __alreadyCalledTest = false;
|
||||
function __preventMultipleTestFunctions() {
|
||||
if (__alreadyCalledTest) {
|
||||
throw new Error("You must only call test() or asyncTest() once per page");
|
||||
}
|
||||
__alreadyCalledTest = true;
|
||||
}
|
||||
|
||||
if (globalThis.internals === undefined) {
|
||||
internals = {
|
||||
|
@ -17,6 +24,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||
});
|
||||
|
||||
function test(f) {
|
||||
__preventMultipleTestFunctions();
|
||||
document.addEventListener("DOMContentLoaded", f);
|
||||
window.addEventListener("load", () => {
|
||||
internals.signalTextTestIsDone();
|
||||
|
@ -24,7 +32,10 @@ function test(f) {
|
|||
}
|
||||
|
||||
function asyncTest(f) {
|
||||
const done = () => internals.signalTextTestIsDone();
|
||||
const done = () => {
|
||||
__preventMultipleTestFunctions();
|
||||
internals.signalTextTestIsDone();
|
||||
};
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
f(done);
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue