Skip to content

Added TS-related tests #98

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

Closed
wants to merge 11 commits into from
5 changes: 5 additions & 0 deletions .changeset/tiny-adults-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/code-interpreter-template': patch
---

added typescript support
19 changes: 18 additions & 1 deletion js/tests/defaultKernels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { expect } from 'vitest'
import { sandboxTest } from './setup'

sandboxTest('test js kernel', async ({ sandbox }) => {
const output = await sandbox.runCode('console.log("Hello World!")', { language: 'js' })
const output = await sandbox.runCode('console.log("Hello World!")', {
language: 'js',
})
expect(output.logs.stdout).toEqual(['Hello World!\n'])
})

sandboxTest('test ts kernel', async ({ sandbox }) => {
const output = await sandbox.runCode(
'const message: string = "Hello World!"; console.log(message)',
{ language: 'ts' }
)
expect(output.logs.stdout).toEqual(['Hello World!\n'])
})

sandboxTest('test ts kernel errors', async ({ sandbox }) => {
const output = await sandbox.runCode('import x from "module";', {
language: 'typescript',
})
expect(output.error?.name).toEqual('TypeScriptCompilerError')
})
13 changes: 13 additions & 0 deletions python/tests/async/test_async_default_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ async def test_js_kernel(async_sandbox: AsyncSandbox):
"console.log('Hello, World!')", language="js"
)
assert execution.logs.stdout == ["Hello, World!\n"]

async def test_ts_kernel(async_sandbox: AsyncSandbox):
execution = await async_sandbox.run_code(
"const message: string = 'Hello, World!'; console.log(message);", language="ts"
)
assert execution.logs.stdout == ["Hello, World!\n"]

async def test_ts_kernel_errors(async_sandbox: AsyncSandbox):
execution = await async_sandbox.run_code(
"import x from 'module';", language="ts"
)
assert execution.error is not None
assert execution.error.name == "TypeScriptCompilerError"
12 changes: 12 additions & 0 deletions python/tests/sync/test_default_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ def test_r_kernel(sandbox: Sandbox):
def test_java_kernel(sandbox: Sandbox):
execution = sandbox.run_code('System.out.println("Hello, World!")', language="java")
assert execution.logs.stdout[0] == "Hello, World!"


@pytest.mark.skip_debug()
def test_ts_kernel(sandbox: Sandbox):
execution = sandbox.run_code("const message: string = 'Hello, World!'; console.log(message)", language="ts")
assert execution.logs.stdout == ["Hello, World!\n"]


def test_ts_kernel_errors(sandbox: Sandbox):
execution = sandbox.run_code("import x from 'module';", language="ts")
assert execution.error is not None
assert execution.error.name == "TypeScriptCompilerError"
2 changes: 1 addition & 1 deletion template/.ts.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"targets": "node 20"
},
"isModule": false
}
}
10 changes: 9 additions & 1 deletion template/server/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

logger = logging.Logger(__name__)

def get_kernel_for_language(language: str) -> str:
if language == "typescript":
return "javascript"

return language

def normalize_language(language: Optional[str]) -> str:
if not language:
Expand All @@ -21,13 +26,16 @@ def normalize_language(language: Optional[str]) -> str:
if language == "js":
return "javascript"

if language == "ts":
return "typescript"

return language


async def create_context(client, websockets: dict, language: str, cwd: str) -> Context:
data = {
"path": str(uuid.uuid4()),
"kernel": {"name": language},
"kernel": {"name": get_kernel_for_language(language)},
"type": "notebook",
"name": str(uuid.uuid4()),
}
Expand Down
28 changes: 27 additions & 1 deletion template/server/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import uuid
import asyncio
import subprocess

from asyncio import Queue
from envs import get_envs
Expand All @@ -27,7 +28,6 @@

logger = logging.getLogger(__name__)


class Execution:
def __init__(self, in_background: bool = False):
self.queue = Queue[
Expand Down Expand Up @@ -199,6 +199,32 @@ async def execute(
+ code
)

if self.language == "typescript":
logger.info("Compiling TypeScript: %s", code)

# call SWC to compile the typescript code
try:
compile_result = subprocess.run("swc --config-file .ts.swcrc --filename index.ts".split(), input=code.encode(), capture_output=True)

if compile_result.returncode != 0:
logger.error("Error during TypeScript compilation: %s", compile_result.stderr.decode())
yield Error(
name="TypeScriptCompilerError",
value=compile_result.stderr.decode(),
traceback="",
)
return

code = compile_result.stdout.decode()
except Exception as e:
logger.error("Error starting SWC process: %s", e)
yield Error(
name="TypeScriptCompilerError",
value=str(e),
traceback="",
)
return

logger.info(code)
request = self._get_execute_request(message_id, code, False)

Expand Down
Loading