fix: sandbox without loosing document.location context #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What's changing, and why
A bug was found if tested code contains ServiceWorker
Steps to reproduce
Expected
ServiceWorker to register.
Actual
"InvalidStateError: Failed to get ServiceWorkerRegistration objects: The document is in an invalid state"
in ChromeScript origin does not match the registering client's origin
in SafariTechnical details
This problem happens, because
TestSandbox
abstraction executes each individual test in a new iframe to isolate those. This iframe is created without thesrc
property, so all the tests are executed in the empty page context: ifdocument.location
is debugged, it appears asabout:blank
without any hostname.This is a problem to register a ServiceWorker, and also it may bring incompatibility issues to other browser APIs.
A proposed way to fix it:
iframe-entrypoint.html
fileThis way the test is executed inside a secure HTTPs context. ServiceWorkers are tested to behave properly, MSW mocks fine.
What else might be impacted?
This is a relatively small change in terms of production code, but it also brings a change to the testing infrastructure of the project.
Previously only unit tests has been present in the project.
This PR adds a new
integration
tests folder, just because I believe the best way to implement such a test is by integration test. Such a test may bring additional confidence this functionality is not broken in the future releases especially if internal implementation (e.g. changing iframe isolation to any other) is changed.This integration test uses
esm-unit
to runesm-unit
tests. And nevertheless I personally love the romance of such a quine, I also admit this idea may be different from the project's ideology and roadmap. And also I confirm that the tests run-time has been dramatically increased.So please provide me with an input about this change, I see 3 different possibilities:
npm test
can be rewritten to runbin/esm-unit
to better match real-world usage and the tool itself may be used to collect coverage itself. I am enthusiastic to do such a migration.Checklist
[x] Tested with
--debug
[x] Tested with headless command line
[x] Documentation (function/class docs, comments, etc.)