11"use client" ;
22import { Heading } from "@/[docs_id]/markdown" ;
3- import "mocha/mocha.js" ;
43import "mocha/mocha.css" ;
54import { Fragment , useEffect , useRef , useState } from "react" ;
65import { useWandbox } from "./wandbox/runtime" ;
@@ -13,6 +12,7 @@ import { useJSEval } from "./worker/jsEval";
1312import { ReplTerminal } from "./repl" ;
1413import { EditorComponent , getAceLang } from "./editor" ;
1514import { ExecFile } from "./exec" ;
15+ import { useTypeScript } from "./typescript/runtime" ;
1616
1717export default function RuntimeTestPage ( ) {
1818 return (
@@ -69,8 +69,17 @@ const sampleConfig: Record<RuntimeLang, SampleConfig> = {
6969 javascript : {
7070 repl : true ,
7171 replInitContent : '> console.log("Hello, World!");\nHello, World!' ,
72- editor : false ,
73- exec : false ,
72+ editor : {
73+ "main.js" : 'console.log("Hello, World!");' ,
74+ } ,
75+ exec : [ "main.js" ] ,
76+ } ,
77+ typescript : {
78+ repl : false ,
79+ editor : {
80+ "main.ts" : 'function greet(name: string): void {\n console.log("Hello, " + name + "!");\n}\n\ngreet("World");' ,
81+ } ,
82+ exec : [ "main.ts" ] ,
7483 } ,
7584 cpp : {
7685 repl : false ,
@@ -122,13 +131,15 @@ function RuntimeSample({
122131function MochaTest ( ) {
123132 const pyodide = usePyodide ( ) ;
124133 const ruby = useRuby ( ) ;
125- const javascript = useJSEval ( ) ;
134+ const jsEval = useJSEval ( ) ;
135+ const typescript = useTypeScript ( jsEval ) ;
126136 const wandboxCpp = useWandbox ( "cpp" ) ;
127137 const runtimeRef = useRef < Record < RuntimeLang , RuntimeContext > > ( null ! ) ;
128138 runtimeRef . current = {
129139 python : pyodide ,
130140 ruby : ruby ,
131- javascript : javascript ,
141+ javascript : jsEval ,
142+ typescript : typescript ,
132143 cpp : wandboxCpp ,
133144 } ;
134145
@@ -139,21 +150,27 @@ function MochaTest() {
139150 const [ mochaState , setMochaState ] = useState < "idle" | "running" | "finished" > (
140151 "idle"
141152 ) ;
142- const { writeFile } = useEmbedContext ( ) ;
153+ const { files } = useEmbedContext ( ) ;
154+ const filesRef = useRef ( files ) ;
155+ filesRef . current = files ;
143156
144- const runTest = ( ) => {
145- setMochaState ( "running" ) ;
157+ const runTest = async ( ) => {
158+ if ( typeof window !== "undefined" ) {
159+ setMochaState ( "running" ) ;
160+
161+ await import ( "mocha/mocha.js" ) ;
146162
147- mocha . setup ( "bdd" ) ;
163+ mocha . setup ( "bdd" ) ;
148164
149- for ( const lang of Object . keys ( runtimeRef . current ) as RuntimeLang [ ] ) {
150- defineTests ( lang , runtimeRef , writeFile ) ;
151- }
165+ for ( const lang of Object . keys ( runtimeRef . current ) as RuntimeLang [ ] ) {
166+ defineTests ( lang , runtimeRef , filesRef ) ;
167+ }
152168
153- const runner = mocha . run ( ) ;
154- runner . on ( "end" , ( ) => {
155- setMochaState ( "finished" ) ;
156- } ) ;
169+ const runner = mocha . run ( ) ;
170+ runner . on ( "end" , ( ) => {
171+ setMochaState ( "finished" ) ;
172+ } ) ;
173+ }
157174 } ;
158175
159176 return (
0 commit comments