Implementing On-Chain Tests for an NFT Marketplace Example Repo #134
idea404
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Adding On-Chain Tests to
nft-tutorial
I've been looking into way to implement simulation/integration tests into the nft-tutorial repository from near-examples and was told to have a look at this repo. I'm very happy I found it as it's been manageable to implement and intuitive in its syntax. For the readers interested in the code feel free to browse around this PR.
I'll share some takeaways from my experience implementing this testing framework which I hope can help people reading (and me too since I'm sure there's plenty that can be done better!):
__tests__
folder containing your.ts
tests and helpers, aava.config.cjs
file, aava.testnet.config.cjs
file, atsconfig.json
file and of course apackage.json
.call
,call_raw
and thetest
functions to assert output.Configuration
Running the default
npx near-workspaces-init
test script commandnear-workspaces-ava
directly would not run thetest:tesnet
script in mypackage.json
file, so I simplified it to something I could understand and yet still made use ofnear-workspaces-ava
:Setup
As
nft-tutorial
had two smart contracts, my initialization function would need to deploy both smart contracts to the chain to setup the testing environment. While coding this, I looked at thelib.rs
files for both contract folders to see what function needed to be called (and with parameters) to initialize the contract once it is deployed. Here's what I ended up with:Development Flow
I started with simple cases such as minting an NFT, like here this one (in
test_contract.ava.ts
):And coding further, I realized I'd use the same code repeatedly to mint an NFT so I refactored that piece and sent it to a
utils.ts
folder and imported to my testing module. I was able to use it in a more involved failing test for cross contract transfers (intest_cross_contract.ava.ts
) like here:Let me know your thoughts! I'm always happy to see cleaner, more efficient and better-organized code 🤓
Beta Was this translation helpful? Give feedback.
All reactions