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 changelog.d/7101-node-test-mock-reset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed `node:test` mock tracker reset and restore behavior to preserve call
history, disassociate reset mocks, and reset mock timers like Node.js.
22 changes: 15 additions & 7 deletions crates/perry-runtime/src/node_submodules/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ enum MockRestoreTarget {

struct MockState {
id: i64,
tracked: bool,
original: f64,
implementation: f64,
once: Vec<f64>,
Expand Down Expand Up @@ -701,6 +702,7 @@ fn create_mock_function(original: f64, implementation: f64, restore: MockRestore
MOCK_STATES.with(|states| {
states.borrow_mut().push(MockState {
id,
tracked: true,
original: original.get_nanbox_f64(),
implementation: implementation.get_nanbox_f64(),
once: Vec::new(),
Expand All @@ -720,6 +722,7 @@ fn create_restore_context(restore: MockRestoreTarget) -> f64 {
MOCK_STATES.with(|states| {
states.borrow_mut().push(MockState {
id,
tracked: true,
original: undefined_value(),
implementation: undefined_value(),
once: Vec::new(),
Expand All @@ -743,9 +746,9 @@ fn restore_mock_state(id: i64) {
let Some(state) = states.iter_mut().find(|state| state.id == id) else {
return None;
};
state.implementation = state.original;
state.once.clear();
reset_mock_state_calls(state);
if matches!(state.restore, MockRestoreTarget::None) {
state.implementation = state.original;
}
Some(state.restore.clone())
});
match restore {
Expand Down Expand Up @@ -1161,27 +1164,32 @@ extern "C" fn mock_property_thunk(
}

extern "C" fn mock_reset_thunk(_closure: *const ClosureHeader) -> f64 {
restore_tracked_mocks();
MOCK_STATES.with(|states| {
for state in states.borrow_mut().iter_mut() {
state.implementation = state.original;
state.once.clear();
reset_mock_state_calls(state);
state.tracked = false;
}
});
crate::timer::js_mock_timers_reset();
undefined_value()
}

extern "C" fn mock_restore_all_thunk(_closure: *const ClosureHeader) -> f64 {
fn restore_tracked_mocks() {
let ids = MOCK_STATES.with(|states| {
states
.borrow()
.iter()
.filter(|state| state.tracked)
.map(|state| state.id)
.collect::<Vec<_>>()
});
for id in ids {
restore_mock_state(id);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

extern "C" fn mock_restore_all_thunk(_closure: *const ClosureHeader) -> f64 {
restore_tracked_mocks();
undefined_value()
}

Expand Down
Loading