Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

created boilerplate for integration tests within js-sdk

0divf4c4bbde6a84
diff --git a/packages/js-sdk/package.json b/packages/js-sdk/package.json
index d57b08e15..ad2fdc50e 100644
--- a/packages/js-sdk/package.json
+++ b/packages/js-sdk/package.json
@@ -35,7 +35,8 @@
"update-deps": "ncu -u && pnpm i",
"postPublish": "./scripts/post-publish.sh || true",
"test:bun": "bun test tests/runtimes/bun --env-file=.env",
- "test:deno": "deno test tests/runtimes/deno/ --allow-net --allow-read --allow-env --unstable-sloppy-imports --trace-leaks"
+ "test:deno": "deno test tests/runtimes/deno/ --allow-net --allow-read --allow-env --unstable-sloppy-imports --trace-leaks",
+ "test:integration": "E2B_INTEGRATION_TEST=1 vitest run tests/integration/**"
},
"devDependencies": {
"@testing-library/react": "^16.0.1",
diff --git a/packages/js-sdk/tests/integration/stress.test.ts b/packages/js-sdk/tests/integration/stress.test.ts
new file mode 100644
index 000000000..b82830372
--- /dev/null
+++ b/packages/js-sdk/tests/integration/stress.test.ts
@@ -0,0 +1,73 @@
+import { test } from 'vitest'
+
+import Sandbox from '../../src/index.js'
+import { wait, isIntegrationTest } from '../setup.js'
+
+const heavyArray = new ArrayBuffer(256 * 1024 * 1024) // 256 MiB = 256 * 1024 * 1024 bytes
+const view = new Uint8Array(heavyArray)
+for (let i = 0; i < view.length; i++) {
+ view[i] = Math.floor(Math.random() * 256)
+}
+
+const integrationTestTemplate = 'jonas_base'
+
+test.skipIf(!isIntegrationTest)(
+ 'stress test heavy file writes and reads',
+ async () => {
+ const promises: Array<Promise<string | void>> = []
+ for (let i = 0; i < 500; i++) {
+ promises.push(
+ Sandbox.create(integrationTestTemplate, { timeoutMs: 60 })
+ .then((sbx) => {
+ console.log(sbx.sandboxId)
+ return sbx.files
+ .write('heavy-file', heavyArray)
+ .then(() => sbx.files.read('heavy-file'))
+ })
+ .catch(console.error)
+ )
+ }
+ await wait(10_000)
+ await Promise.all(promises)
+ }
+)
+
+test.skipIf(!isIntegrationTest)('stress network ingress', async ({}) => {
+ const promises: Array<Promise<string | void>> = []
+
+ for (let i = 0; i < 10; i++) {
+ promises.push(
+ Sandbox.create(integrationTestTemplate, { timeoutMs: 60 }).then((sbx) => {
+ console.log('created sandbox', sbx.sandboxId)
+ sbx.files
+ .write('heavy-file', heavyArray)
+ .then(() => {
+ sbx.commands.run('python -m http.server 8000', { background: true })
+ })
+ .then(() => {
+ new Promise((resolve, reject) => {
+ try {
+ resolve(sbx.getHost(8000))
+ } catch (e) {
+ console.error('error getting sbx host', e)
+ reject(e)
+ }
+ }).then((host) => {
+ const url = `https://${host}`
+ console.log('fetching url', url)
+ fetch(url)
+ })
+
+ try {
+ sbx.kill()
+ } catch (e) {
+ console.error('error killing sbx', e)
+ }
+ })
+ })
+ )
+ }
+
+ await wait(10_000)
+ await Promise.all(promises)
+})
diff --git a/packages/js-sdk/tests/integration/template/README.md b/packages/js-sdk/tests/integration/template/README.md
new file mode 100644
index 000000000..2d4155dc3
--- /dev/null
+++ b/packages/js-sdk/tests/integration/template/README.md
@@ -0,0 +1,5 @@
+# Integration test template
+
+# Build the template
+
+`$ e2b template build"`
diff --git a/packages/js-sdk/tests/integration/template/e2b.Dockerfile b/packages/js-sdk/tests/integration/template/e2b.Dockerfile
new file mode 100644
index 000000000..65ceb7d65
--- /dev/null
+++ b/packages/js-sdk/tests/integration/template/e2b.Dockerfile
@@ -0,0 +1,8 @@
+FROM e2bdev/code-interpreter:latest
+
+# Clone the Next.js app repository
+RUN git clone https://github.com/ezesundayeze/basic-nextjs-app
+
+# Install dependencies
+RUN cd basic-nextjs-app && npm install
+
diff --git a/packages/js-sdk/tests/integration/template/e2b.toml b/packages/js-sdk/tests/integration/template/e2b.toml
new file mode 100644
index 000000000..4f6dff736
--- /dev/null
+++ b/packages/js-sdk/tests/integration/template/e2b.toml
@@ -0,0 +1,18 @@
+# This is a config for E2B sandbox template.
+# You can use template ID (2e2z80zhv34yumbrybvn) or template name (integration-test-v1) to create a sandbox:
+
+# Python SDK
+# from e2b import Sandbox, AsyncSandbox
+# sandbox = Sandbox("integration-test-v1") # Sync sandbox
+# sandbox = await AsyncSandbox.create("integration-test-v1") # Async sandbox
+
+# JS SDK
+# import { Sandbox } from 'e2b'
+# const sandbox = await Sandbox.create('integration-test-v1')
+
+team_id = "b9c07023-d095-4bdc-9634-e25d5530ba47"
+memory_mb = 1_024
+start_cmd = "npm run dev"
+dockerfile = "e2b.Dockerfile"
+template_name = "integration-test-v1"
+template_id = "2e2z80zhv34yumbrybvn"
diff --git a/packages/js-sdk/tests/setup.ts b/packages/js-sdk/tests/setup.ts
index 7cdbd5e0c..30889c60f 100644
--- a/packages/js-sdk/tests/setup.ts
+++ b/packages/js-sdk/tests/setup.ts
@@ -1,7 +1,7 @@
import { Sandbox } from '../src'
import { test as base } from 'vitest'
-export const template = 'base'
+export const template = 'jonas_base'
interface SandboxFixture {
sandbox: Sandbox
@@ -30,6 +30,7 @@ export const sandboxTest = base.extend<SandboxFixture>({
})
export const isDebug = process.env.E2B_DEBUG !== undefined
+export const isIntegrationTest = process.env.E2B_INTEGRATION_TEST !== undefined
export async function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
diff --git a/packages/js-sdk/vitest.workspace.mts b/packages/js-sdk/vitest.workspace.mts
index 8ce2118f6..38006cd44 100644
--- a/packages/js-sdk/vitest.workspace.mts
+++ b/packages/js-sdk/vitest.workspace.mts
@@ -5,20 +5,11 @@ const env = config()
export default defineWorkspace([
{
test: {
- include: [
- 'tests/**/*.test.ts',
- ],
- exclude: [
- 'tests/runtimes/**',
- ],
- poolOptions: {
- threads: {
- minThreads: 1,
- maxThreads: 4,
- },
- },
+ include: ['tests/**/*.test.ts'],
+ exclude: ['tests/runtimes/**', 'tests/integration/**'],
+ isolate: false, // for projects that don't rely on side effects, disabling isolation will improve the speed of the tests
globals: false,
- testTimeout: 30000,
+ testTimeout: 30_000,
environment: 'node',
bail: 1,
server: {},
@@ -26,7 +17,7 @@ export default defineWorkspace([
interopDefault: true,
},
env: {
- ...process.env as Record<string, string>,
+ ...(process.env as Record<string, string>),
...env.parsed,
},
},
@@ -43,8 +34,8 @@ export default defineWorkspace([
providerOptions: {},
},
env: {
- ...process.env as Record<string, string>,
- ...env.parsed,
+ ...(process.env as Record<string, string>),
+ ...env.parsed,
},
},
},
@@ -55,4 +46,16 @@ export default defineWorkspace([
environment: 'edge-runtime',
},
},
+ {
+ test: {
+ include: ['tests/integration/**/*.test.ts'],
+ globals: false,
+ testTimeout: 60_000,
+ environment: 'node',
+ env: {
+ ...(process.env as Record<string, string>),
+ ...env.parsed,
+ },
+ },
+ },
])