-
Notifications
You must be signed in to change notification settings - Fork 589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: Events emitted from within a component cannot be spied on/asserted in tests #5861
Comments
Was just playing around and I found out that adding #[starknet::component]
pub mod counter_component {
#[event]
#[derive(Drop, Debug, PartialEq, starknet::Event)]
pub enum Event {
CounterIncreased: CounterIncreased
}
#[derive(Drop, Debug, PartialEq, starknet::Event)]
pub struct CounterIncreased {
pub new_value: u32,
}
// ...
}
#[starknet::contract]
pub mod MockContract {
// ...
#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
#[flat]
CounterEvent: counter_component::Event
}
// ...
} The tests now pass and correctly spy on events. It seems kind of counter-intuitive imo. Is this expected behavior? |
There is no bug - or need for the flat. |
@orizi I don't think that's it. If that were the case, I would receive an error message like: Instead, I'm getting the following error message: scarb cairo-test -f test_increment_event_component
Compiling test(comp_event_unittest) comp_event v0.1.0 (/home/nenad/repos/cairo_projects/comp_internals/Scarb.toml)
Finished release target(s) in 2 seconds
testing comp_event ...
running 1 test
test comp_event::tests::test_increment_event_component ... fail (gas usage est.: 1093000)
failures:
comp_event::tests::test_increment_event_component - Panicked with "assertion `starknet::testing::pop_log(counter.contract_address) == Option::Some(
counter_component::Event::CounterIncreased(
counter_component::CounterIncreased { new_value: 1 }
)
)` failed.
starknet::testing::pop_log(counter.contract_address): Option::None(())
Option::Some(
counter_component::Event::CounterIncreased(
counter_component::CounterIncreased { new_value: 1 }
)
): Option::Some(Event::CounterIncreased(CounterIncreased { new_value: 1 }))".
Error: test result: FAILED. 0 passed; 1 failed; 0 ignored Notice the line Can you please post what you regard as the fixed version of the event configuration in tests? |
this indeed means an event has not been emitted. your current pop + comparison seems correct type-wise. |
Will post a question on Discord regarding this, I may be doing something wrong. |
@orizi I got an answer on Discord that resolved my issue (see https://discord.com/channels/793094838509764618/793094838987128844/1255043119355924500). The problem was basically as you described it, I just had to update the event signature to use I guess the problem is just that confusing error message; from my previous comment:
Because of this message, I naturally assumed that... no event was emitted. So I didn't even bother tinkering with the event signatures. The error message should have stated that event signatures did not match, that would immediately point me to investigate that part of the implementation. |
Bug Report
Cairo version:
Current behavior:
Emitting events from components is permitted (see example). But writing tests for these components, and trying to assert that certain events have been emitted is impossible, as the events appear to not be emitted in test environment.
The issue does not occur if the component is converted to be a contract, events to get caught in test environments then.
The issue occurs when using both vanilla test framework and when using Foundry.
Expected behavior:
Events emitted from components can be spied on in tests.
Steps to reproduce:
For vanilla test framework version:
scarb 2.6.4+nightly-2024-06-22 (5deb60ac1 2024-06-22)
scarb cairo-test
For Foundry test version:
scarb 2.6.4+nightly-2024-06-22 (5deb60ac1 2024-06-22)
scarb test
Other information:
https://github.com/misicnenad/component-emit-bug-foundry
https://github.com/misicnenad/component-emit-bug-vanilla
The text was updated successfully, but these errors were encountered: