Skip to content

Commit

Permalink
fix: don't error if Deno.bench() or Deno.test() are used in run subco…
Browse files Browse the repository at this point in the history
…mmand (#14946)
  • Loading branch information
bartlomieju authored Jun 24, 2022
1 parent 215f3d4 commit d390949
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/lsp/testing/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ async fn test_specifier(
},
);

worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].enableTestAndBench()"#,
)?;

worker
.execute_script(
&located_script_name!(),
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2728,3 +2728,8 @@ fn running_declaration_files() {
assert!(output.status.success());
}
}

itest!(test_and_bench_are_noops_in_run {
args: "run test_and_bench_in_run.js",
output_str: Some(""),
});
5 changes: 5 additions & 0 deletions cli/tests/testdata/test_and_bench_in_run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deno.test(function foo() {
});

Deno.bench(function bar() {
});
5 changes: 5 additions & 0 deletions cli/tools/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ async fn bench_specifier(
Default::default(),
);

worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].enableTestAndBench()"#,
)?;

if options.compat_mode {
worker.execute_side_module(&compat::GLOBAL_URL).await?;
worker.execute_side_module(&compat::MODULE_URL).await?;
Expand Down
5 changes: 5 additions & 0 deletions cli/tools/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,11 @@ async fn test_specifier(
},
);

worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].enableTestAndBench()"#,
)?;

let mut maybe_coverage_collector = if let Some(ref coverage_dir) =
ps.coverage_dir
{
Expand Down
17 changes: 17 additions & 0 deletions runtime/js/40_testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,18 @@
const tests = [];
/** @type {BenchDescription[]} */
const benchDescs = [];
let isTestOrBenchSubcommand = false;

// Main test function provided by Deno.
function test(
nameOrFnOrOptions,
optionsOrFn,
maybeFn,
) {
if (!isTestOrBenchSubcommand) {
return;
}

let testDef;
const defaults = {
ignore: false,
Expand Down Expand Up @@ -669,6 +674,10 @@
optionsOrFn,
maybeFn,
) {
if (!isTestOrBenchSubcommand) {
return;
}

core.opSync("op_bench_check_unstable");
let benchDesc;
const defaults = {
Expand Down Expand Up @@ -1043,6 +1052,13 @@
return core.opSync("op_bench_now");
}

// This function is called by Rust side if we're in `deno test` or
// `deno bench` subcommand. If this function is not called then `Deno.test()`
// and `Deno.bench()` become noops.
function enableTestAndBench() {
isTestOrBenchSubcommand = true;
}

async function runTests({
filter = null,
shuffle = null,
Expand Down Expand Up @@ -1507,6 +1523,7 @@

window.__bootstrap.internals = {
...window.__bootstrap.internals ?? {},
enableTestAndBench,
runTests,
runBenchmarks,
};
Expand Down

0 comments on commit d390949

Please sign in to comment.