near / sdk-docs

The book about near-sdk-rs
https://near-sdk.io
19 stars 21 forks source link

Testing event logs #108

Open veigajoao opened 2 years ago

veigajoao commented 2 years ago

On building unit tests for complex smart contracts it is very important to test correct logging of events. Events are the bridge of data between the smart contract's state history and user facing frontend applications. Usually a complex service involves a indexing service that picks up events and logs them to a DB instance, for further processing.

Therefore, it seems very relevant to test the emission of events on unit tests, asserting that they work as expected. Unfortunately, I could find no mention to such testing in the documentation for the SDK.

Am I missing something? Is there a way to perform assertions on emitted events in the current version?

If not, I believe this would be a very beneficial feature add.

austinabell commented 2 years ago

I'll move this to the high-level docs repo to cover this.

Short answer is you can retrieve all logs through https://docs.rs/near-sdk/latest/near_sdk/test_utils/fn.get_logs.html for unit tests, and workspaces has it's own way of checking logs for each transaction for e2e tests

veigajoao commented 2 years ago

Just tested the feature and it's working great, thanks for the information!