Simulation Testing

Simulation testing is such a heavy topic it deserves its own section. Unlike unit testing, simulation tests tries to emulate what really happens on chain with mockups. For our lockup contract, remember we have cross-contract calls to stacking contract, voting contract, and whitelist contract? All these wasm files need to be included to perform simulation testing, not just our lockup contract's wasm file. With simulation testing you can do more than what you can with unit testing; but it's also more complicated.

Don't confuse simulation tests with end-to-end tests though. The end-to-end tests uses the Jest Testing Suite, which is written in Typescript. Currently there's no replacement for this yet; but perhaps in the future we might use sandbox testing. Today we focus on simulation testing and forget about end-to-end test for now.

If you ever include simulation testing, you require to change something with your toml first. First, include "rlib"; second, import near-sdk-sim. Previously we don't include "rlib" because it adds size to cross contract; however, if you do perform simulation testing, forget about optimizing wasm size. You can't have it both ways.

If you can only pick one between bear paw and fish, you gotta pick the best.
-- A Chinese Proverb

These tests exist outside of your usual src folder in Cargo, just like usual Rust simulation testing. You can run these test like how you usually run unit testing with:

cargo test -- --nocapture

or just cargo test.

In this chapter we won't look at multiple simulation tests; we'll just look at how to create simulation tests, and the rest will be for you to understand. We shall leave the link to the code for you in the References section too! Let's start.

References