Skip to content

Commit a4bc41b

Browse files
authored
Align test output closer to native cargo test (#4358)
1 parent 6006599 commit a4bc41b

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* Handle stuck and failed WebDriver processes when re-trying to start them.
2626
[#4340](https://github.com/rustwasm/wasm-bindgen/pull/4340)
2727

28+
* Align test output closer to native `cargo test`.
29+
[#4358](https://github.com/rustwasm/wasm-bindgen/pull/4358)
30+
2831
### Fixed
2932

3033
- Fixed using [JavaScript keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords) as identifiers not being handled correctly.

crates/cli/src/bin/wasm-bindgen-test-runner/main.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,14 @@ fn main() -> anyhow::Result<()> {
216216

217217
let browser_timeout = env::var("WASM_BINDGEN_TEST_TIMEOUT")
218218
.map(|timeout| {
219-
timeout
219+
let timeout = timeout
220220
.parse()
221-
.expect("Could not parse 'WASM_BINDGEN_TEST_TIMEOUT'")
221+
.expect("Could not parse 'WASM_BINDGEN_TEST_TIMEOUT'");
222+
println!("Set timeout to {} seconds...", timeout);
223+
timeout
222224
})
223225
.unwrap_or(20);
224226

225-
if debug {
226-
println!("Set timeout to {} seconds...", browser_timeout);
227-
}
228-
229227
// Make the generated bindings available for the tests to execute against.
230228
shell.status("Executing bindgen...");
231229
let mut b = Bindgen::new();

crates/test/src/rt/mod.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ struct State {
165165
/// How to actually format output, either node.js or browser-specific
166166
/// implementation.
167167
formatter: Box<dyn Formatter>,
168+
169+
/// Timing the total duration.
170+
timer: Option<Timer>,
168171
}
169172

170173
/// Failure reasons.
@@ -252,6 +255,18 @@ extern "C" {
252255
// General-purpose conversion into a `String`.
253256
#[wasm_bindgen(js_name = String)]
254257
fn stringify(val: &JsValue) -> String;
258+
259+
type Global;
260+
261+
#[wasm_bindgen(method, getter)]
262+
fn performance(this: &Global) -> JsValue;
263+
264+
/// Type for the [`Performance` object](https://developer.mozilla.org/en-US/docs/Web/API/Performance).
265+
type Performance;
266+
267+
/// Binding to [`Performance.now()`](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now).
268+
#[wasm_bindgen(method)]
269+
fn now(this: &Performance) -> f64;
255270
}
256271

257272
/// Internal implementation detail of the `console_log!` macro.
@@ -328,6 +343,8 @@ impl Context {
328343
detect::Runtime::Worker => Box::new(worker::Worker::new()) as Box<dyn Formatter>,
329344
};
330345

346+
let timer = Timer::new();
347+
331348
Context {
332349
state: Rc::new(State {
333350
filter: Default::default(),
@@ -340,6 +357,7 @@ impl Context {
340357
running: Default::default(),
341358
succeeded: Default::default(),
342359
formatter,
360+
timer,
343361
}),
344362
}
345363
}
@@ -373,7 +391,6 @@ impl Context {
373391
self.state
374392
.formatter
375393
.writeln(&format!("running {} {}", tests.len(), noun));
376-
self.state.formatter.writeln("");
377394

378395
// Execute all our test functions through their Wasm shims (unclear how
379396
// to pass native function pointers around here). Each test will
@@ -523,6 +540,8 @@ impl Context {
523540
should_panic: Option<Option<&'static str>>,
524541
ignore: Option<Option<&'static str>>,
525542
) {
543+
// Split away
544+
let name = name.split_once("::").unwrap().1;
526545
// If our test is filtered out, record that it was filtered and move
527546
// on, nothing to do here.
528547
let filter = self.state.filter.borrow();
@@ -677,18 +696,25 @@ impl State {
677696
self.formatter.writeln(&format!(" {}", test.name));
678697
}
679698
}
699+
let finished_in = if let Some(timer) = &self.timer {
700+
format!("; finished in {:.2?}s", timer.elapsed())
701+
} else {
702+
String::new()
703+
};
680704
self.formatter.writeln("");
681705
self.formatter.writeln(&format!(
682706
"test result: {}. \
683707
{} passed; \
684708
{} failed; \
685709
{} ignored; \
686-
{} filtered out\n",
710+
{} filtered out\
711+
{}\n",
687712
if failures.len() == 0 { "ok" } else { "FAILED" },
688713
self.succeeded.get(),
689714
failures.len(),
690715
self.ignored.get(),
691716
self.filtered.get(),
717+
finished_in,
692718
));
693719
}
694720

@@ -813,3 +839,27 @@ fn tab(s: &str) -> String {
813839
}
814840
result
815841
}
842+
843+
struct Timer {
844+
performance: Performance,
845+
started: f64,
846+
}
847+
848+
impl Timer {
849+
fn new() -> Option<Self> {
850+
let global: Global = js_sys::global().unchecked_into();
851+
let performance = global.performance();
852+
(!performance.is_undefined()).then(|| {
853+
let performance: Performance = performance.unchecked_into();
854+
let started = performance.now();
855+
Self {
856+
performance,
857+
started,
858+
}
859+
})
860+
}
861+
862+
fn elapsed(&self) -> f64 {
863+
(self.performance.now() - self.started) / 1000.
864+
}
865+
}

0 commit comments

Comments
 (0)