Skip to content

Commit 88266d6

Browse files
authored
Unify passing of template used in tests and log background commands output (#704)
- Show errors from background commands - Unify passing of template used in tests
1 parent 0572175 commit 88266d6

20 files changed

Lines changed: 122 additions & 34 deletions

File tree

packages/js-sdk/tests/api/list.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assert } from 'vitest'
22

33
import { Sandbox } from '../../src'
4-
import { isDebug, sandboxTest } from '../setup.js'
4+
import { isDebug, sandboxTest, template } from '../setup.js'
55

66
sandboxTest.skipIf(isDebug)('list sandboxes', async ({ sandbox }) => {
77
const sandboxes = await Sandbox.list()
@@ -15,11 +15,11 @@ sandboxTest.skipIf(isDebug)('list sandboxes', async ({ sandbox }) => {
1515
sandboxTest.skipIf(isDebug)('list sandboxes with metadata filter', async () => {
1616
const uniqueId = Date.now().toString()
1717
// Create an extra sandbox with a uniqueId
18-
const extraSbx = await Sandbox.create({ })
18+
const extraSbx = await Sandbox.create(template)
1919
try {
20-
const sbx = await Sandbox.create({metadata: {uniqueId: uniqueId}})
20+
const sbx = await Sandbox.create(template, { metadata: { uniqueId: uniqueId } })
2121
try {
22-
const sandboxes = await Sandbox.list({query:{metadata: {uniqueId}}})
22+
const sandboxes = await Sandbox.list({ query: { metadata: { uniqueId } } })
2323
assert.equal(sandboxes.length, 1)
2424
assert.equal(sandboxes[0].sandboxId, sbx.sandboxId)
2525
} finally {
@@ -34,7 +34,7 @@ sandboxTest.skipIf(isDebug)('list sandboxes empty filter', async ({ sandbox }) =
3434
const sandboxes = await Sandbox.list()
3535
assert.isAtLeast(sandboxes.length, 1)
3636
assert.include(
37-
sandboxes.map((s) => s.sandboxId),
38-
sandbox.sandboxId
37+
sandboxes.map((s) => s.sandboxId),
38+
sandbox.sandboxId
3939
)
4040
})

packages/js-sdk/tests/cmdHelper.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { CommandHandle, CommandExitError } from '../src/index.js'
2+
import { assert } from 'vitest'
3+
4+
export function catchCmdExitErrorInBackground(cmd: CommandHandle) {
5+
let disabled = false
6+
7+
cmd.wait().catch((res: CommandExitError) => {
8+
if (!disabled) {
9+
assert.equal(res.exitCode, 0, `command failed with exit code ${res.exitCode}: ${res.stderr}`)
10+
}
11+
})
12+
13+
return () => {
14+
disabled = true
15+
}
16+
}

packages/js-sdk/tests/runtimes/browser/run.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { expect, inject, test } from 'vitest'
22
import { render } from 'vitest-browser-react'
3+
import { waitFor } from '@testing-library/react'
34
import React from 'react'
45
import { useEffect, useState } from 'react'
6+
57
import { Sandbox } from '../../../src'
6-
import { waitFor } from '@testing-library/react'
8+
import { template } from '../../template'
79

810
function E2BTest() {
911
const [text, setText] = useState<string>()
1012

1113
useEffect(() => {
1214
const getText = async () => {
13-
const sandbox = await Sandbox.create({apiKey: inject('E2B_API_KEY')})
15+
const sandbox = await Sandbox.create(template, { apiKey: inject('E2B_API_KEY') })
1416

1517
try {
1618
await sandbox.commands.run('echo "Hello World" > hello.txt')

packages/js-sdk/tests/runtimes/bun/run.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { expect, test } from 'bun:test'
22

33
import { Sandbox } from '../../../src'
4+
import { template } from '../../template'
45

56
test(
67
'Bun test',
78
async () => {
8-
const sbx = await Sandbox.create('base', { timeoutMs: 5_000 })
9+
const sbx = await Sandbox.create(template, { timeoutMs: 5_000 })
910
try {
1011
const isRunning = await sbx.isRunning()
1112
expect(isRunning).toBeTruthy()

packages/js-sdk/tests/runtimes/deno/run.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { load } from 'https://deno.land/std@0.224.0/dotenv/mod.ts'
44
await load({ envPath: '.env', export: true })
55

66
import { Sandbox } from '../../../dist/index.mjs'
7+
import { template } from '../../template'
78

89

910
Deno.test('Deno test', async () => {
10-
const sbx = await Sandbox.create('base', { timeoutMs: 5_000 })
11+
const sbx = await Sandbox.create(template, { timeoutMs: 5_000 })
1112
try {
1213
const isRunning = await sbx.isRunning()
1314
assert(isRunning)

packages/js-sdk/tests/sandbox/closed_port.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { assert, test } from 'vitest'
22

33
import { Sandbox } from '../../src/index.js'
44
import { isDebug, template, wait } from '../setup.js'
5+
import { catchCmdExitErrorInBackground } from '../cmdHelper.js'
56

67
test.skipIf(isDebug)('closed port in SDK', async () => {
78
const sbx = await Sandbox.create(template, { timeoutMs: 60_000 })
89
const goodPort = 8000
910

10-
await sbx.commands.run(`python -m http.server ${goodPort}`, {
11+
const cmd = await sbx.commands.run(`python -m http.server ${goodPort}`, {
1112
background: true,
1213
})
1314

14-
await wait(1000)
15+
const disable = catchCmdExitErrorInBackground(cmd)
1516

1617
const goodHost = sbx.getHost(goodPort)
1718
// leave this here as a helper to visit host in browser
@@ -44,16 +45,19 @@ test.skipIf(isDebug)('closed port in SDK', async () => {
4445
assert.equal(resp.message, 'The sandbox is running but port is not open')
4546
assert.equal(cleanedSbxId, resp.sandboxId)
4647
assert.equal(resp.port, badPort)
48+
disable()
4749
})
4850

4951
test.skipIf(isDebug)('closed port in browser ', async () => {
5052
const sbx = await Sandbox.create(template, { timeoutMs: 60_000 })
5153
const goodPort = 8000
5254

53-
await sbx.commands.run(`python -m http.server ${goodPort}`, {
55+
const cmd = await sbx.commands.run(`python -m http.server ${goodPort}`, {
5456
background: true,
5557
})
5658

59+
const disable = catchCmdExitErrorInBackground(cmd)
60+
5761
await wait(1000)
5862

5963
const goodHost = sbx.getHost(goodPort)
@@ -87,4 +91,5 @@ test.skipIf(isDebug)('closed port in browser ', async () => {
8791
assert.equal(res.status, 502)
8892
const resp_text = await res.text()
8993
assert(resp_text.includes('<title>Closed Port Error</title>'))
94+
disable()
9095
})

packages/js-sdk/tests/sandbox/commands/envVars.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert } from 'vitest'
22

3-
import { sandboxTest, isDebug } from '../../setup.js'
3+
import { sandboxTest, isDebug, template } from '../../setup.js'
44
import { Sandbox } from '../../../src'
55

66
sandboxTest.skipIf(isDebug)('env vars', async ({ sandbox }) => {
@@ -11,7 +11,7 @@ sandboxTest.skipIf(isDebug)('env vars', async ({ sandbox }) => {
1111
})
1212

1313
sandboxTest.skipIf(isDebug)('env vars on sandbox', async () => {
14-
const sandbox = await Sandbox.create({ envs: { FOO: 'bar' } })
14+
const sandbox = await Sandbox.create(template, { envs: { FOO: 'bar' } })
1515

1616
try {
1717
const cmd = await sandbox.commands.run('echo "$FOO"')

packages/js-sdk/tests/sandbox/commands/run.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ sandboxTest('run with multiline string', async ({ sandbox }) => {
3030
})
3131

3232
sandboxTest('run with timeout', async ({ sandbox }) => {
33-
const cmd = await sandbox.commands.run('echo "Hello, World!"', { timeoutMs: 1000 })
33+
const cmd = await sandbox.commands.run('echo "Hello, World!"', { timeoutMs: 4000 })
3434

3535
assert.equal(cmd.exitCode, 0)
3636
})

packages/js-sdk/tests/sandbox/host.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { assert } from 'vitest'
22

33
import { isDebug, sandboxTest, wait } from '../setup.js'
4-
4+
import { catchCmdExitErrorInBackground } from '../cmdHelper.js'
55
sandboxTest(
66
'ping server in running sandbox',
77
async ({ sandbox }) => {
88
const cmd = await sandbox.commands.run('python -m http.server 8000', {
99
background: true,
1010
})
1111

12+
const disable = catchCmdExitErrorInBackground(cmd)
13+
1214
try {
1315
await wait(1000)
1416

@@ -25,6 +27,7 @@ sandboxTest(
2527
await wait(500)
2628
}
2729
assert.equal(res.status, 200)
30+
disable()
2831
} finally {
2932
try {
3033
await cmd.kill()

packages/js-sdk/tests/setup.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { Sandbox } from '../src'
22
import { test as base } from 'vitest'
3-
4-
export const template = 'base'
3+
import { template } from './template'
54

65
interface SandboxFixture {
76
sandbox: Sandbox
87
}
98

109
export const sandboxTest = base.extend<SandboxFixture>({
1110
sandbox: [
12-
async ({}, use) => {
11+
async ({ }, use) => {
1312
const sandbox = await Sandbox.create(template)
1413
try {
1514
await use(sandbox)
@@ -25,7 +24,7 @@ export const sandboxTest = base.extend<SandboxFixture>({
2524
}
2625
}
2726
},
28-
{ auto: true },
27+
{ auto: false },
2928
],
3029
})
3130

@@ -35,3 +34,5 @@ export const isIntegrationTest = process.env.E2B_INTEGRATION_TEST !== undefined
3534
export async function wait(ms: number) {
3635
return new Promise((resolve) => setTimeout(resolve, ms))
3736
}
37+
38+
export { template }

0 commit comments

Comments
 (0)