|
| 1 | +import { expect } from 'vitest' |
| 2 | + |
| 3 | +import { sandboxTest } from '../setup' |
| 4 | + |
| 5 | +sandboxTest('js simple', async ({ sandbox }) => { |
| 6 | + const result = await sandbox.runCode('console.log("Hello, World!")', {language: "deno"}) |
| 7 | + |
| 8 | + expect(result.logs.stdout.join().trim()).toEqual('Hello, World!') |
| 9 | +}) |
| 10 | + |
| 11 | +sandboxTest('js import', async ({ sandbox }) => { |
| 12 | + const result = await sandbox.runCode('import isOdd from "npm:is-odd"\nisOdd(3)', {language: "deno"}) |
| 13 | + |
| 14 | + expect(result.results[0].text).toEqual('true') |
| 15 | +}) |
| 16 | + |
| 17 | +sandboxTest('js top level await', async ({ sandbox }) => { |
| 18 | + const result = await sandbox.runCode(` |
| 19 | + async function main() { |
| 20 | + return 'Hello, World!' |
| 21 | + } |
| 22 | +
|
| 23 | + await main() |
| 24 | + `, {language: "deno"}) |
| 25 | + expect(result.results[0].text).toEqual('Hello, World!') |
| 26 | +}) |
| 27 | + |
| 28 | +sandboxTest('js es6', async ({ sandbox }) => { |
| 29 | + const result = await sandbox.runCode(` |
| 30 | + const add = (x, y) => x + y; |
| 31 | + add(1, 2)`, {language: "deno"}) |
| 32 | + expect(result.results[0].text).toEqual('3') |
| 33 | +}) |
| 34 | + |
| 35 | + |
| 36 | +sandboxTest('js context', async ({ sandbox }) => { |
| 37 | + await sandbox.runCode('const z = 1', {language: "deno"}) |
| 38 | + const result = await sandbox.runCode('z', {language: "deno"}) |
| 39 | + expect(result.results[0].text).toEqual('1') |
| 40 | +}) |
| 41 | + |
| 42 | +sandboxTest('js cwd', async ({ sandbox }) => { |
| 43 | + const result = await sandbox.runCode('process.cwd()', {language: "deno"}) |
| 44 | + expect(result.results[0].text).toEqual('/home/user') |
| 45 | + |
| 46 | + const ctx = await sandbox.createCodeContext( {cwd: '/home', language: "deno"}) |
| 47 | + const result2 = await sandbox.runCode('process.cwd()', {context: ctx}) |
| 48 | + expect(result2.results[0].text).toEqual('/home') |
| 49 | +}) |
| 50 | + |
| 51 | +sandboxTest('ts simple', async ({ sandbox }) => { |
| 52 | + const result = await sandbox.runCode(` |
| 53 | +function subtract(x: number, y: number): number { |
| 54 | + return x - y; |
| 55 | +} |
| 56 | +
|
| 57 | +subtract(1, 2) |
| 58 | +`, {language: "deno"}) |
| 59 | + |
| 60 | + expect(result.results[0].text).toEqual('-1') |
| 61 | +}) |
| 62 | + |
| 63 | +sandboxTest('test display', async ({ sandbox }) => { |
| 64 | + const result = await sandbox.runCode(` |
| 65 | + { |
| 66 | + [Symbol.for("Jupyter.display")]() { |
| 67 | + return { |
| 68 | + // Plain text content |
| 69 | + "text/plain": "Hello world!", |
| 70 | +
|
| 71 | + // HTML output |
| 72 | + "text/html": "<h1>Hello world!</h1>", |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +`, {language: "deno"}) |
| 77 | + |
| 78 | + expect(result.results[0].html).toBe('<h1>Hello world!</h1>') |
| 79 | + expect(result.results[0].text).toBe('Hello world!') |
| 80 | +}) |
0 commit comments