Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/test/writing-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ The following command runs only tests #2 and #3.
bun test --only
```

`.only` has no effect when it selects no test that can run. For example, Bun skips a `test.only()` inside `describe.skip()`, and the other tests in the file still run. Unlike in Jest, a `describe.only()` inside `describe.skip()` also stays skipped.

### test.if

To run a test conditionally, use `test.if()`. The test runs if the condition is truthy. Use it for tests that should only run on a specific architecture or operating system.
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/test_runner/Collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ impl Collection {
) -> HandleUncaughtExceptionResult {
let _g = group::begin();

self.active_scope_mut().failed = true;
let scope = self.active_scope_mut();
scope.failed = true;
// What it would have declared is unknown, so the `.only` around it keeps the file focused.
scope.base.mark_focus();

HandleUncaughtExceptionResult::ShowUnhandledErrorInDescribe // unhandled because it needs to exit with code 1
}
Expand Down
7 changes: 2 additions & 5 deletions src/runtime/test_runner/ScopeFunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,6 @@ impl ScopeFunctions {
}
}

if !matches_filter {
base.self_mode = SelfMode::FilteredOut;
}

debug_assert!(!bun_test.collection.locked);
group_log::log(format_args!(
"enqueueTestCallback / {} / in scope: {}",
Expand All @@ -420,12 +416,13 @@ impl ScopeFunctions {

let _ = bun_test.collection.active_scope_mut().append_test(
description,
if matches_filter { callback } else { None },
callback,
bun_test::ExecutionEntryCfg {
has_done_parameter,
timeout: options.timeout,
retry_count: options.retry.unwrap_or(0),
repeat_count: options.repeats,
filtered_out: !matches_filter,
},
base,
bun_test::AddedInPhase::Collection,
Expand Down
75 changes: 57 additions & 18 deletions src/runtime/test_runner/bun_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,15 @@ impl ScopeMode {
Self::FilteredOut => "filtered_out",
}
}

/// `.skip`, or `.todo` without `--todo`: the test file says this scope does not run.
pub(crate) fn is_disabled(self) -> bool {
match self {
Self::Skip => true,
Self::Todo => !Jest::runner().is_some_and(|runner| runner.run_todo),
Self::Normal | Self::Failing | Self::FilteredOut => false,
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
}
}
}

#[derive(Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -1695,33 +1704,60 @@ impl BaseScope {
ConcurrentMode::No => false,
ConcurrentMode::Inherit => parent_base.is_some_and(|p| p.concurrent),
},
mode: if let Some(p) = parent_base {
if p.mode != ScopeMode::Normal { p.mode } else { cfg.self_mode }
} else {
cfg.self_mode
},
mode: Self::inherited_mode(parent_base, cfg.self_mode),
only: if cfg.self_only { Only::Yes } else { Only::No },
has_callback,
test_id_for_debugger: cfg.test_id_for_debugger,
line_no: cfg.line_no,
}
}

/// A scope inside a skip or todo describe takes that mode.
fn inherited_mode(parent: Option<&BaseScope>, own: ScopeMode) -> ScopeMode {
match parent {
Some(p) if p.mode != ScopeMode::Normal => p.mode,
_ => own,
}
}

pub(crate) fn propagate(&mut self, has_callback: bool) {
self.has_callback = has_callback;
if let Some(parent) = self.parent {
// SAFETY: parent backref valid; tree is single-threaded and parent
// outlives child. Borrows are scoped to each call.
unsafe {
if self.only != Only::No {
(*parent).mark_contains_only();
}
if self.has_callback {
(*parent).mark_has_callback();
}
}
}
}

/// Focus comes from tests, not from `describe.only`: marks the ancestors of the innermost `.only` at or above this scope.
pub(crate) fn mark_focus(&self) {
if self.mode.is_disabled() {
return;
}
let mut scope: &BaseScope = self;
loop {
match scope.only {
Only::Yes => {
if let Some(parent) = scope.parent {
// SAFETY: parent backref valid; tree is single-threaded and parent
// outlives child. `scope` is a different node than the ones marked.
unsafe { (*parent).mark_contains_only() };
}
return;
}
// `mark_contains_only` marks up to the root, so every ancestor is marked already.
Only::Contains => return,
Only::No => {}
}
let Some(parent) = scope.parent else { return };
// SAFETY: parent backref valid; tree is single-threaded and parent outlives child.
scope = unsafe { &(*parent).base };
}
}
}
// deinit: only frees `name` → Box<[u8]> drops automatically; no explicit Drop needed.

Expand Down Expand Up @@ -1791,8 +1827,7 @@ impl DescribeScope {
name_not_owned: Option<&[u8]>,
base: BaseScopeCfg,
) -> &mut DescribeScope {
let mut child = Self::create(BaseScope::init(base, name_not_owned, Some(std::ptr::from_mut(self)), false));
child.base.propagate(false);
let child = Self::create(BaseScope::init(base, name_not_owned, Some(std::ptr::from_mut(self)), false));
Comment thread
robobun marked this conversation as resolved.
Resolved
self.entries.push(TestScheduleEntry::Describe(child));
match self.entries.last_mut().unwrap() {
TestScheduleEntry::Describe(d) => &mut **d,
Expand All @@ -1809,6 +1844,13 @@ impl DescribeScope {
phase: AddedInPhase,
) -> JsResult<&mut ExecutionEntry> {
let mut entry = ExecutionEntry::create(name_not_owned, callback, cfg, Some(std::ptr::from_mut(self)), base, phase);
// Decided before `-t` applies, as in Jest: the filter never changes which tests focus a file.
if callback.is_some() {
entry.base.mark_focus();
}
if cfg.filtered_out {
entry.base.mode = BaseScope::inherited_mode(Some(&self.base), ScopeMode::FilteredOut);
}
let has_cb = entry.callback.is_some();
entry.base.propagate(has_cb);
self.entries.push(TestScheduleEntry::TestCallback(entry));
Expand Down Expand Up @@ -1861,6 +1903,8 @@ pub struct ExecutionEntryCfg {
pub(crate) retry_count: u32,
/// Number of times to repeat a test (0 = run once, 1 = run twice, etc.)
pub(crate) repeat_count: u32,
/// The `-t` filter does not match this test.
pub(crate) filtered_out: bool,
}

#[derive(Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -1913,14 +1957,9 @@ impl ExecutionEntry {
});

if let Some(c) = cb {
entry.callback = match entry.base.mode {
ScopeMode::Skip => None,
ScopeMode::Todo => {
let run_todo = Jest::runner().is_some_and(|runner| runner.run_todo);
if run_todo { Some(strong_create(c)) } else { None }
}
_ => Some(strong_create(c)),
};
if !cfg.filtered_out && !entry.base.mode.is_disabled() {
entry.callback = Some(strong_create(c));
}
}
entry
}
Expand Down
Loading
Loading