-
Notifications
You must be signed in to change notification settings - Fork 1k
test(js-sdk): run the full unit test suite in a browser #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mishushakov
wants to merge
9
commits into
main
Choose a base branch
from
browser-test-suite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f57c67c
test(js-sdk): run the full unit test suite in a browser
mishushakov 0ab365c
test(js-sdk): disable browser failure screenshots, drop pagination gate
mishushakov ae300e7
test(js-sdk): unskip browser tests fixed by infra#3389
mishushakov a80a8ec
test(js-sdk): address browser-leg review feedback
mishushakov 6fd735e
test(js-sdk): serve CORS from in-sandbox test servers, drop the last …
mishushakov 785b938
test(js-sdk): exclude the msw suites main added since the browser leg…
mishushakov 33c28e8
fix(js-sdk): drain streams through the reader when buffering an upload
mishushakov d04fb04
fix(js-sdk): recognize the browser's mid-request connection drop
mishushakov 3288bcd
test(js-sdk): point the CORS note at belt#2068
mishushakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| 'e2b': patch | ||
| --- | ||
|
|
||
| Fix uploads of non-native `ReadableStream`s in the browser silently sending the text `[object ReadableStream]` instead of the data. Buffering a stream drained it with `new Response(stream)`, which accepts any async iterable on Node (an undici extension) but only its own stream class in a browser, stringifying anything else. Streams are now drained through the reader, which every implementation supports. |
This file contains hidden or 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,5 @@ | ||
| --- | ||
| 'e2b': patch | ||
| --- | ||
|
|
||
| Report a sandbox killed mid-request as an actionable `TimeoutError` in the browser. When the connection to a sandbox drops mid-request the SDK probes the sandbox's health to tell a killed sandbox apart from a transient network blip, but the probe only ran for connection-dropped wordings it recognized, and the browser's (`network error`) was missing — so killing a sandbox while a command was running surfaced a generic `SandboxError: [unknown] network error` instead of `TimeoutError: ... The sandbox was killed or reached its end of life while the request was in flight.` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| // Vite exposes the runner's `env` config on `import.meta.env`, which the | ||
| // browser suite reads instead of `process.env`. The canonical types live in | ||
| // `vite/client`, but vite is only a transitive dependency here (via vitest) and | ||
| // isn't resolvable from this package, so declare the shape the suite uses. | ||
| interface ImportMeta { | ||
| readonly env: Record<string, string | undefined> | ||
| } |
50 changes: 50 additions & 0 deletions
50
packages/js-sdk/tests/runtimes/browser/noProcessGlobal.test.ts
This file contains hidden or 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,50 @@ | ||
| import { assert, test } from 'vitest' | ||
|
|
||
| import { template } from '../../template' | ||
|
|
||
| // Bundlers don't give browser apps a `process` unless asked to, so the SDK has | ||
| // to be fully configurable through explicit options — `getEnvVar` | ||
| // (src/api/metadata.ts) returns '' when `process` is missing rather than | ||
| // throwing. The rest of this suite runs against the `process.env` shim from | ||
| // processEnv.setup.ts, which would mask a regression that reintroduces a bare | ||
| // `process` read on the create path, so this test drops the shim entirely and | ||
| // drives a real sandbox the way a browser app has to. | ||
| // | ||
| // The SDK is imported dynamically inside the test, after the shim is gone: a | ||
| // static import is evaluated at collection time, while the shim is still | ||
| // installed, which would let a top-level `process` read anywhere in the module | ||
| // graph pass here and still crash a browser app on import. | ||
| // | ||
| // Config comes from `import.meta.env` (where vitest puts the config's `env`), | ||
| // which is also how a Vite app would hand its own build-time values over. | ||
| const apiKey = import.meta.env.E2B_API_KEY | ||
| const domain = import.meta.env.E2B_DOMAIN || undefined | ||
| const isDebug = import.meta.env.E2B_DEBUG !== undefined | ||
|
|
||
| test.skipIf(isDebug)( | ||
| 'drives a sandbox with no process global', | ||
| async () => { | ||
| const shim = Reflect.getOwnPropertyDescriptor(globalThis, 'process') | ||
| Reflect.deleteProperty(globalThis, 'process') | ||
| assert.equal(typeof process, 'undefined') | ||
|
|
||
| try { | ||
| const { Sandbox } = await import('../../../src') | ||
| const sandbox = await Sandbox.create(template, { apiKey, domain }) | ||
|
|
||
| try { | ||
| await sandbox.files.write('hello.txt', 'Hello World') | ||
| assert.equal(await sandbox.files.read('hello.txt'), 'Hello World') | ||
|
|
||
| const result = await sandbox.commands.run('echo "from the browser"') | ||
| assert.equal(result.exitCode, 0) | ||
| assert.equal(result.stdout.trim(), 'from the browser') | ||
| } finally { | ||
| await sandbox.kill() | ||
| } | ||
| } finally { | ||
| if (shim) Reflect.defineProperty(globalThis, 'process', shim) | ||
| } | ||
| }, | ||
| 60_000 | ||
| ) |
15 changes: 15 additions & 0 deletions
15
packages/js-sdk/tests/runtimes/browser/processEnv.setup.ts
This file contains hidden or 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,15 @@ | ||
| // Browsers have no `process`: `getEnvVar` (src/api/metadata.ts) resolves to '' | ||
| // there, so E2B_* can only reach a browser app through whatever shim its | ||
| // bundler injects. Provide that shim — the same thing workerd's nodejs_compat | ||
| // gives the Cloudflare suite for free — so the shared suites can configure the | ||
| // SDK from the environment like they do on every other runtime. | ||
| // | ||
| // `process.env` aliases Vite's `import.meta.env`, which is where vitest puts | ||
| // the config's `env` and what `vi.stubEnv` writes to in browser mode, so env | ||
| // reads and stubbing both behave as they do on Node. | ||
| // | ||
| // Only `env` is defined: `process.release` must stay absent or the SDK would | ||
| // detect the runtime as 'node' instead of 'browser'. Tests that need a | ||
| // browser without any `process` at all delete it themselves — see | ||
| // noProcessGlobal.test.ts. | ||
| Object.assign(globalThis, { process: { env: import.meta.env } }) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.