@@ -34,6 +34,19 @@ import { serveEngine } from '../../src/worker-rpc/serve-engine';
3434
3535const MAIN = 'apps/client/src/main.ts' ;
3636
37+ /**
38+ * Waits for a check's result. A checker runs TypeScript over the project: with coverage on a slow
39+ * runner it takes several times longer than locally, so the wait is long and the tests' timeouts
40+ * leave room for it.
41+ */
42+ const settled = ( done : ( ) => unknown , timeout = 25_000 ) =>
43+ vi . waitFor (
44+ ( ) => {
45+ if ( ! done ( ) ) throw new Error ( 'not yet' ) ;
46+ } ,
47+ { timeout, interval : 25 } ,
48+ ) ;
49+
3750/** Worker plugin: renames the first `const` declared in a file. */
3851const renamePlugin : WorkerPluginModule = {
3952 activate : ( sdk ) => {
@@ -229,22 +242,18 @@ describe('CodeService', () => {
229242 await code . engine ( ) ;
230243 await ( await code . engine ( ) ) . setFiles ( [ { path : uri , text : await documents . getText ( uri ) } ] ) ;
231244 const watch = code . watchDiagnostics ( uri ) ;
232- const start = Date . now ( ) ;
233- while ( ! diagnostics . forUri ( uri ) . length && Date . now ( ) - start < 8000 )
234- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
245+ await settled ( ( ) => diagnostics . forUri ( uri ) . length ) ;
235246 expect ( diagnostics . forUri ( uri ) . map ( ( d ) => d . code ) ) . toEqual ( [ 2322 ] ) ;
236247 watch . dispose ( ) ;
237248 expect ( diagnostics . forUri ( uri ) ) . toEqual ( [ ] ) ;
238249 code . dispose ( ) ;
239- } ) ;
250+ } , 60_000 ) ;
240251
241252 it ( 'checks every file of the project while asked to, read-only packages aside' , async ( ) => {
242253 const { code, diagnostics } = setup ( ) ;
243254 const uri = 'checks/broken.ts' ;
244255 const check = code . checkProject ( ) ;
245- const start = Date . now ( ) ;
246- while ( ! diagnostics . forUri ( uri ) . length && Date . now ( ) - start < 15_000 )
247- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
256+ await settled ( ( ) => diagnostics . forUri ( uri ) . length ) ;
248257 expect ( diagnostics . forUri ( uri ) ) . toMatchObject ( [
249258 { code : 2322 , source : 'typescript' , line : 1 , column : 14 } ,
250259 ] ) ;
@@ -254,7 +263,7 @@ describe('CodeService', () => {
254263 check . dispose ( ) ;
255264 expect ( diagnostics . forUri ( uri ) ) . toEqual ( [ ] ) ;
256265 code . dispose ( ) ;
257- } ) ;
266+ } , 60_000 ) ;
258267
259268 it ( 'still checks the whole project when an edit comes before the first check ends' , async ( ) => {
260269 const { code, diagnostics } = setup ( ) ;
@@ -265,42 +274,34 @@ describe('CodeService', () => {
265274 return [ ] ;
266275 } ) ;
267276 const check = code . checkProject ( ) ;
268- const start = Date . now ( ) ;
269- while ( ! diagnostics . forUri ( 'checks/broken.ts' ) . length && Date . now ( ) - start < 15_000 )
270- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
277+ await settled ( ( ) => diagnostics . forUri ( 'checks/broken.ts' ) . length ) ;
271278 expect ( diagnostics . forUri ( 'checks/broken.ts' ) . map ( ( d ) => d . code ) ) . toEqual ( [ 2322 ] ) ;
272279 await project . fs . write ( 'checks/speed.ts' , 'export const speed = 1;\n' ) ;
273280 check . dispose ( ) ;
274281 code . dispose ( ) ;
275- } , 20_000 ) ;
282+ } , 60_000 ) ;
276283
277284 it ( 'checks everything again after an edit to global declarations' , async ( ) => {
278285 const { code, diagnostics } = setup ( ) ;
279286 const check = code . checkProject ( ) ;
280- const start = Date . now ( ) ;
281- while ( ! diagnostics . forUri ( 'checks/broken.ts' ) . length && Date . now ( ) - start < 15_000 )
282- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
287+ await settled ( ( ) => diagnostics . forUri ( 'checks/broken.ts' ) . length ) ;
283288 const refresh = vi . spyOn ( code , 'refreshDiagnostics' ) ;
284289 await project . fs . write (
285290 'checks/speed.ts' ,
286291 'export const speed = 1;\ndeclare global {\n var debugSpeed: number;\n}\n' ,
287292 ) ;
288- const edited = Date . now ( ) ;
289293 const checked = ( ) => new Set ( refresh . mock . calls . flatMap ( ( [ paths ] ) => paths ) ) ;
290- while ( ! checked ( ) . has ( 'checks/broken.ts' ) && Date . now ( ) - edited < 10_000 )
291- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
294+ await settled ( ( ) => checked ( ) . has ( 'checks/broken.ts' ) ) ;
292295 expect ( checked ( ) . has ( 'checks/broken.ts' ) ) . toBe ( true ) ;
293296 await project . fs . write ( 'checks/speed.ts' , 'export const speed = 1;\n' ) ;
294297 check . dispose ( ) ;
295298 code . dispose ( ) ;
296- } , 30_000 ) ;
299+ } , 60_000 ) ;
297300
298301 it ( 'keeps an edit made while a check reads what changed' , async ( ) => {
299302 const { code, diagnostics } = setup ( ) ;
300303 const check = code . checkProject ( ) ;
301- const start = Date . now ( ) ;
302- while ( ! diagnostics . forUri ( 'checks/broken.ts' ) . length && Date . now ( ) - start < 15_000 )
303- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
304+ await settled ( ( ) => diagnostics . forUri ( 'checks/broken.ts' ) . length ) ;
304305 const refresh = vi . spyOn ( code , 'refreshDiagnostics' ) ;
305306 /** While the check of `broken.ts` reads it, `speed.ts` changes: what imports it is checked. */
306307 const service = code as unknown as {
@@ -313,35 +314,29 @@ describe('CodeService', () => {
313314 } ) ;
314315 await project . fs . write ( 'checks/broken.ts' , 'export const n: number =\n "other";\n' ) ;
315316 const user = 'checks/uses-speed.ts' ;
316- const edited = Date . now ( ) ;
317317 const checked = ( ) => new Set ( refresh . mock . calls . flatMap ( ( [ paths ] ) => paths ) ) ;
318- while ( ! checked ( ) . has ( user ) && Date . now ( ) - edited < 10_000 )
319- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
318+ await settled ( ( ) => checked ( ) . has ( user ) ) ;
320319 expect ( checked ( ) . has ( 'checks/broken.ts' ) ) . toBe ( true ) ;
321320 expect ( checked ( ) . has ( user ) ) . toBe ( true ) ;
322321 await project . fs . write ( 'checks/broken.ts' , 'export const n: number =\n "text";\n' ) ;
323322 await project . fs . write ( 'checks/speed.ts' , 'export const speed = 1;\n' ) ;
324323 check . dispose ( ) ;
325324 code . dispose ( ) ;
326- } , 30_000 ) ;
325+ } , 60_000 ) ;
327326
328327 it ( 'after an edit, checks again only the file and what imports it' , async ( ) => {
329328 const { code, diagnostics } = setup ( ) ;
330329 const check = code . checkProject ( ) ;
331- const start = Date . now ( ) ;
332- while ( ! diagnostics . forUri ( 'checks/broken.ts' ) . length && Date . now ( ) - start < 15_000 )
333- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
330+ await settled ( ( ) => diagnostics . forUri ( 'checks/broken.ts' ) . length ) ;
334331 const refresh = vi . spyOn ( code , 'refreshDiagnostics' ) ;
335332 await project . fs . write ( 'checks/speed.ts' , 'export const speed = "fast";\n' ) ;
336333 const user = 'checks/uses-speed.ts' ;
337- const edited = Date . now ( ) ;
338- while ( ! diagnostics . forUri ( user ) . length && Date . now ( ) - edited < 10_000 )
339- await new Promise ( ( r ) => setTimeout ( r , 25 ) ) ;
334+ await settled ( ( ) => diagnostics . forUri ( user ) . length ) ;
340335 expect ( diagnostics . forUri ( user ) . map ( ( d ) => d . code ) ) . toEqual ( [ 2322 ] ) ;
341336 const checked = new Set ( refresh . mock . calls . flatMap ( ( [ paths ] ) => paths ) ) ;
342337 expect ( [ ...checked ] . sort ( ) ) . toEqual ( [ 'checks/speed.ts' , user ] ) ;
343338 await project . fs . write ( 'checks/speed.ts' , 'export const speed = 1;\n' ) ;
344339 check . dispose ( ) ;
345340 code . dispose ( ) ;
346- } ) ;
341+ } , 60_000 ) ;
347342} ) ;
0 commit comments