1- import { describe , it , beforeAll , expect } from 'vitest' ;
1+ import { describe , it , beforeAll , expect , vi } from 'vitest' ;
2+
23import jsdom from 'jsdom' ;
34import lodash from 'lodash' ;
45
5- import {
6- buildChallenge ,
7- runnerTypes
8- } from '../../../client/src/templates/Challenges/utils/build' ;
96import {
107 challengeTypes ,
118 hasNoSolution
@@ -28,6 +25,32 @@ import { sortChallenges } from './utils/sort-challenges.js';
2825
2926const { flatten, isEmpty, cloneDeep } = lodash ;
3027
28+ vi . mock (
29+ '../../../client/src/templates/Challenges/utils/typescript-worker-handler' ,
30+ async importOriginal => {
31+ const actual = await importOriginal ( ) ;
32+
33+ // ts and tsvfs must match the versions used in the typescript-worker.
34+ const tsvfs = await import ( '@typescript/vfs-1.6.1' ) ;
35+ const ts = await import ( 'typescript-5.9.2' ) ;
36+ // use the same TS compiler as the client
37+ const tsCompiler = await import (
38+ '../../../tools/client-plugins/browser-scripts/modules/typescript-compiler'
39+ ) ;
40+ const compiler = new tsCompiler . Compiler ( ts , tsvfs ) ;
41+ await compiler . setup ( { useNodeModules : true } ) ;
42+ return {
43+ ...actual ,
44+ checkTSServiceIsReady : ( ) => Promise . resolve ( true ) ,
45+ compileTypeScriptCode : code => {
46+ const { result, error } = compiler . compile ( code , 'index.tsx' ) ;
47+ if ( error ) throw error ;
48+ return result ;
49+ }
50+ } ;
51+ }
52+ ) ;
53+
3154const dom = new jsdom . JSDOM ( '' ) ;
3255global . document = dom . window . document ;
3356global . DOMParser = dom . window . DOMParser ;
@@ -86,13 +109,13 @@ export async function defineTestsForBlock(testFilter) {
86109
87110 const challengeData = { meta, challenges, lang } ;
88111
89- describe ( 'Check challenges' , ( ) => {
112+ describe ( 'Check challenges' , async ( ) => {
90113 beforeAll ( async ( ) => {
91114 page = await newPageContext ( ) ;
92115 global . Worker = createPseudoWorker ( page ) ;
93116 } ) ;
94117
95- populateTestsForLang ( challengeData , ( ) => page ) ;
118+ await populateTestsForLang ( challengeData , ( ) => page ) ;
96119 } ) ;
97120}
98121
@@ -123,7 +146,13 @@ export async function getChallenges(lang, filters) {
123146 return sortChallenges ( challenges ) ;
124147}
125148
126- function populateTestsForLang ( { lang, challenges, meta } ) {
149+ async function populateTestsForLang ( { lang, challenges, meta } ) {
150+ // We have to dynamically import this because otherwise it will not be mocked.
151+ // Presumably this is because we import from_this file in the generated block
152+ // test files and that happens before the mock is applied.
153+ const { buildChallenge } = await import (
154+ '../../../client/src/templates/Challenges/utils/build'
155+ ) ;
127156 const validateChallenge = challengeSchemaValidator ( ) ;
128157
129158 describe ( `Language: ${ lang } ` , function ( ) {
@@ -341,6 +370,10 @@ async function createTestRunner(
341370 buildChallenge ,
342371 solutionFromNext
343372) {
373+ const { runnerTypes } = await import (
374+ '../../../client/src/templates/Challenges/utils/build'
375+ ) ;
376+
344377 const challengeFiles = replaceChallengeFilesContentsWithSolutions (
345378 challenge . challengeFiles ,
346379 solutionFiles
0 commit comments