Skip to content

fix: await Sandbox.connect in pause regression tests#1223

Open
passionworkeer wants to merge 6 commits intoe2b-dev:mainfrom
passionworkeer:fix/await-sandbox-connect
Open

fix: await Sandbox.connect in pause regression tests#1223
passionworkeer wants to merge 6 commits intoe2b-dev:mainfrom
passionworkeer:fix/await-sandbox-connect

Conversation

@passionworkeer
Copy link

Summary

This PR fixes the P1 bug identified in PR #1219 where Sandbox.connect(...) was called without �wait, causing connected.pause() to fail with a TypeError since connected was a Promise rather than a Sandbox instance.

Changes in packages/js-sdk/tests/sandbox/pause.test.ts:

  1. Await Sandbox.connect() - All calls now properly await the Promise
  2. Fix env restoration logic - Changed if (savedApiKey) to if (savedApiKey !== undefined) with else { delete process.env.E2B_API_KEY } to properly handle undefined cases
  3. Remove redundant apiKey variable - Simplified the apiKey retrieval pattern

Test cases covered:

  • pause() uses apiKey from connectionConfig when E2B_API_KEY is not set
  • pause() returns false when sandbox is already paused
  • pause() works on connected sandbox with apiKey in connectionConfig

Closes #1219

passionworkeer and others added 5 commits March 20, 2026 05:33
When using Sandbox.connect() with an apiKey, the pause() method was not
passing the apiKey to the API call, causing auth failures. This fix
ensures pause() (and deprecated betaPause()) use the same pattern as
kill() - merging connectionConfig with provided opts.

Fixes: e2b-dev#1215
…tion

- Update @returns JSDoc: pause() returns boolean (true=paused, false=already paused)
  matching SandboxApi.pause semantics, not a sandbox ID
- Add regression tests for apiKey propagation in Sandbox.connect():
  - pause() succeeds when E2B_API_KEY is unset but apiKey is passed to connect()
  - pause() returns false when sandbox is already paused
  - pause() works on connected sandbox with apiKey in connectionConfig

Refs: Copilot PR e2b-dev#1218 review comments
- Remove unused template import
- Change savedApiKey truthy check to explicit undefined check
- Add await before Sandbox.connect() calls (P1 - async Promise fix)
- Add explicit non-null assertion after toBeDefined() for TypeScript
… Copilot review)

- Remove unused 'expect' import from vitest
- Use explicit if-check + throw instead of expect(apiKey).toBeDefined()
- This properly narrows TypeScript type for apiKey without relying on assertion return type
- Remove redundant process.env.E2B_API_KEY read after delete (always undefined)
- Update JSDoc: 'Pause a sandbox by its ID' -> 'Pause this sandbox'
- PR e2b-dev#1221
Copilot AI review requested due to automatic review settings March 21, 2026 16:39
@changeset-bot
Copy link

changeset-bot bot commented Mar 21, 2026

🦋 Changeset detected

Latest commit: 034e2d9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
e2b Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression in the JS SDK where calling pause() (and betaPause()) on a connected Sandbox instance could fail to authenticate when E2B_API_KEY was unset, because instance-level connection configuration (e.g., apiKey) was not being forwarded into the underlying SandboxApi calls.

Changes:

  • Update Sandbox.pause() / Sandbox.betaPause() to merge this.connectionConfig into the options passed to SandboxApi.*Pause(...).
  • Add/adjust regression tests to ensure Sandbox.connect(..., { apiKey }) works even when process.env.E2B_API_KEY is cleared, and that repeated pause() returns false when already paused.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/js-sdk/tests/sandbox/pause.test.ts Adds regression coverage for pause behavior on connected sandboxes and env var handling.
packages/js-sdk/src/sandbox/index.ts Ensures instance pause() / betaPause() propagate connection config (e.g., apiKey) to API calls.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants