// Note: this code tries to make sure that we don't fail to execute a tags we have have had at least // 1 assertion. This helps ensure that if a script tag fails to execute, // we'll report an error, even if no assertions failed. const scripts = document.getElementsByTagName('script'); for (script of scripts) { const id = script.id; if (!id) { continue; } if (!testing._executed_scripts[id]) { console.warn(`Failed to execute any expectations for `), testing._status = 'fail'; } } return testing._status; } // Set expectations to happen at some point in the future. Necessary for // testing callbacks which will only be executed after page.wait is called. function eventually(fn) { // capture the current state (script id, stack) so that, when we do run this // we can display more meaningful details on failure. testing._eventually.push([fn, { script_id: document.currentScript.id, stack: new Error().stack, }]); _registerErrorCallback(); } function _recordExecution() { if (testing._status === 'fail') { return; } testing._status = 'ok'; const script_id = testing._captured?.script_id || document.currentScript.id; testing._executed_scripts[script_id] = true; _registerErrorCallback(); } // We want to attach an onError callback to each .\n There should be a eval error printed above this.`, ); } } function _equal(a, b) { if (a === b) { return true; } if (a === null || b === null) { return false; } if (typeof a !== 'object' || typeof b !== 'object') { return false; } if (Object.keys(a).length != Object.keys(b).length) { return false; } for (property in a) { if (b.hasOwnProperty(property) === false) { return false; } if (_equal(a[property], b[property]) === false) { return false; } } return true; } window.testing = { _status: 'empty', _eventually: [], _executed_scripts: {}, _captured: null, skip: skip, getStatus: getStatus, eventually: eventually, expectEqual: expectEqual, expectError: expectError, withError: withError, }; // Helper, so you can do $(sel) in a test window.$ = function(sel) { return document.querySelector(sel); } // Helper, so you can do $$(sel) in a test window.$$ = function(sel) { return document.querySelectorAll(sel); } })();