From 4a9f42950140b36b1916ab0e0320d721223b1095 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Tue, 5 Dec 2023 09:26:06 -0700 Subject: refactor(cli): refactor bench/test for future module changes (#21460) Extracting some refactorings for the module work that will land in https://github.com/denoland/deno_core/pull/359/ --- cli/tools/test/mod.rs | 57 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 14 deletions(-) (limited to 'cli/tools/test/mod.rs') diff --git a/cli/tools/test/mod.rs b/cli/tools/test/mod.rs index 836004f86..c69c3115c 100644 --- a/cli/tools/test/mod.rs +++ b/cli/tools/test/mod.rs @@ -410,6 +410,41 @@ pub async fn test_specifier( mut sender: TestEventSender, fail_fast_tracker: FailFastTracker, options: TestSpecifierOptions, +) -> Result<(), AnyError> { + match test_specifier_inner( + worker_factory, + permissions, + specifier.clone(), + &sender, + fail_fast_tracker, + options, + ) + .await + { + Ok(()) => Ok(()), + Err(error) => { + if error.is::() { + sender.send(TestEvent::UncaughtError( + specifier.to_string(), + Box::new(error.downcast::().unwrap()), + ))?; + Ok(()) + } else { + Err(error) + } + } + } +} + +/// Test a single specifier as documentation containing test programs, an executable test module or +/// both. +async fn test_specifier_inner( + worker_factory: Arc, + permissions: Permissions, + specifier: ModuleSpecifier, + sender: &TestEventSender, + fail_fast_tracker: FailFastTracker, + options: TestSpecifierOptions, ) -> Result<(), AnyError> { if fail_fast_tracker.should_stop() { return Ok(()); @@ -439,23 +474,13 @@ pub async fn test_specifier( } // We execute the main module as a side module so that import.meta.main is not set. - match worker.execute_side_module_possibly_with_npm().await { - Ok(()) => {} - Err(error) => { - if error.is::() { - sender.send(TestEvent::UncaughtError( - specifier.to_string(), - Box::new(error.downcast::().unwrap()), - ))?; - return Ok(()); - } else { - return Err(error); - } - } - } + worker.execute_side_module_possibly_with_npm().await?; let mut worker = worker.into_main_worker(); + // Ensure that there are no pending exceptions before we start running tests + worker.run_up_to_duration(Duration::from_millis(0)).await?; + worker.dispatch_load_event(located_script_name!())?; run_tests_for_worker(&mut worker, &specifier, &options, &fail_fast_tracker) @@ -466,6 +491,10 @@ pub async fn test_specifier( worker.dispatch_beforeunload_event(located_script_name!())?; worker.dispatch_unload_event(located_script_name!())?; + // Ensure the worker has settled so we can catch any remaining unhandled rejections. We don't + // want to wait forever here. + worker.run_up_to_duration(Duration::from_millis(0)).await?; + if let Some(coverage_collector) = coverage_collector.as_mut() { worker .js_runtime -- cgit v1.2.3