forked from mswjs/msw
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(browser integration): patch upstream PreviewServer to fix IPv6 i…
…ssue Patch `page-with` IPv6 bug until upstream is merged Tracking: kettanaito/page-with#9
- Loading branch information
1 parent
de6039e
commit 4c0e774
Showing
2 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Patch the `server` variable from `page-with/server`, which is a mutable | ||
* variable that is updated during execution of `createBrowser` to contain | ||
* the connection info of the preview server which was created for the page. | ||
* | ||
* This function needs to be called from a scope where that mutable variable | ||
* is available. That's why we call it in `jest.browser.setup.ts`. | ||
* | ||
* When the upstream bug is fixed, remove this file and remove the code that | ||
* calls it in `jest.browser.setup.ts` | ||
* | ||
*/ | ||
export const patchServerConnectionInfo = (server: PreviewServer) => { | ||
// bug: `PreviewServer.listen()` serializes IPv6 hosts to invalid URL (missing square brackets) | ||
// see: node_modules/page-with/lib/server/PreviewServer.js / PreviewServer.listen | ||
const conn = server.connectionInfo | ||
|
||
// fix: re-serialize any URL containing an IPv6 host without surrounding brackets | ||
if (conn.host.includes(':') && !conn.url.includes(`[${conn.host}]`)) { | ||
// note: scheme is hardcoded to `http` to match `PreviewServer.listen()` | ||
server.connectionInfo.url = `http://[${conn.host}]:${conn.port}` | ||
} | ||
} | ||
|
||
/** | ||
* This is the same as the upstream type (but non-nullable), but we don't import | ||
* from `page-with` because we we want to access its mutable `server` export, | ||
* which gets messy if we import from thw file twice (could access wrong closure) | ||
*/ | ||
type PreviewServer = { | ||
connectionInfo: { | ||
port: number | ||
host: string | ||
url: string | ||
} | ||
} |