fix(cli): side-load test modules (#11515)

This fixes a regression introduced in 1.9 where test modules became main
modules by side loading them in a generated module.
This commit is contained in:
Casper Beyer 2021-07-26 20:05:44 +08:00 committed by GitHub
parent 9e89fe2fe8
commit b2fcd3d014
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 27 deletions

View file

@ -19,6 +19,12 @@ fn no_color() {
assert!(out.contains("test result: FAILED. 1 passed; 1 failed; 1 ignored; 0 measured; 0 filtered out"));
}
itest!(meta {
args: "test test/meta.ts",
exit_code: 0,
output: "test/meta.out",
});
itest!(pass {
args: "test test/pass.ts",
exit_code: 0,

7
cli/tests/test/meta.out Normal file
View file

@ -0,0 +1,7 @@
Check [WILDCARD]/test/meta.ts
import.meta.main: false
import.meta.url: [WILDCARD]/test/meta.ts
running 0 tests from [WILDCARD]/test/meta.ts
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])

2
cli/tests/test/meta.ts Normal file
View file

@ -0,0 +1,2 @@
console.log("import.meta.main: %s", import.meta.main);
console.log("import.meta.url: %s", import.meta.url);

View file

@ -653,7 +653,7 @@ fn filter_coverages(
.filter(|e| {
let is_internal = e.url.starts_with("deno:")
|| e.url.ends_with("__anonymous__")
|| e.url.ends_with("$deno$test.ts");
|| e.url.ends_with("$deno$test.js");
let is_included = include.iter().any(|p| p.is_match(&e.url));
let is_excluded = exclude.iter().any(|p| p.is_match(&e.url));

View file

@ -35,6 +35,7 @@ use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use swc_common::comments::CommentKind;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
@ -263,10 +264,38 @@ where
pub async fn run_test_file(
program_state: Arc<ProgramState>,
main_module: ModuleSpecifier,
test_module: ModuleSpecifier,
permissions: Permissions,
quiet: bool,
filter: Option<String>,
shuffle: Option<u64>,
channel: Sender<TestEvent>,
) -> Result<(), AnyError> {
let test_module =
deno_core::resolve_path(&format!("{}$deno$test.js", Uuid::new_v4()))?;
let test_source = format!(
r#"
import "{}";
await new Promise(resolve => setTimeout(resolve, 0));
await Deno[Deno.internal].runTests({});
"#,
main_module,
json!({
"disableLog": quiet,
"filter": filter,
"shuffle": shuffle,
})
);
let test_file = File {
local: test_module.to_file_path().unwrap(),
maybe_types: None,
media_type: MediaType::JavaScript,
source: test_source.clone(),
specifier: test_module.clone(),
};
program_state.file_fetcher.insert_cached(test_file);
let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, true);
@ -293,8 +322,6 @@ pub async fn run_test_file(
None
};
worker.execute_module(&main_module).await?;
worker.execute_script(
&located_script_name!(),
"window.dispatchEvent(new Event('load'))",
@ -443,34 +470,13 @@ pub async fn run_tests(
return Ok(());
}
// Because scripts, and therefore worker.execute cannot detect unresolved promises at the moment
// we generate a module for the actual test execution.
let test_options = json!({
"disableLog": quiet,
"filter": filter,
"shuffle": shuffle,
});
let test_module = deno_core::resolve_path("$deno$test.js")?;
let test_source =
format!("await Deno[Deno.internal].runTests({});", test_options);
let test_file = File {
local: test_module.to_file_path().unwrap(),
maybe_types: None,
media_type: MediaType::JavaScript,
source: test_source.clone(),
specifier: test_module.clone(),
};
program_state.file_fetcher.insert_cached(test_file);
let (sender, receiver) = channel::<TestEvent>();
let join_handles = test_modules.iter().map(move |main_module| {
let program_state = program_state.clone();
let main_module = main_module.clone();
let test_module = test_module.clone();
let permissions = permissions.clone();
let filter = filter.clone();
let sender = sender.clone();
tokio::task::spawn_blocking(move || {
@ -478,8 +484,10 @@ pub async fn run_tests(
let future = run_test_file(
program_state,
main_module,
test_module,
permissions,
quiet,
filter,
shuffle,
sender,
);