Matchstick is a testing framework for The Graph Protocol that enables developers to write unit tests for subgraph event and function handlers by creating mock events in a sandbox environment, then verifying that handler functions correctly transform event data into stored entities using assertion methods.
Subgraph Testing with Matchstick: Unit Test Guide for The Graph
Added:hello everyone and welcome to the first episode of what will be a mini-series on using matchstick to write unit tests so what matchstick allows you to do is test your event handlers and function handlers in a sandbox environment and what that means is you can mark out your event um objects how you expect your events coming into the subgraph to look like and then test if your handler functions really do what you expect them to do so over here you can see we've provided a little demo subgraph to give a quick example of unit testing and uh in this subgraph inside of the mapping file we have two event handlers and one function handler but today we're only going to be focusing on this event handler handle new gravatar which um expects an event uh in this event we can just speak into the generated folder it just have a bun it just has a bunch of fields and what the handler function is doing is it takes those fields and then it saves the entity to the store um now what's important here is that this is a really simple event handler but essentially what most event handlers do is they take an event they run some logic on it and then they save their respective entities to the store so this is not far from most use cases of course the hand information can get really big but at the end it takes an event and then saves and then a generated entity from things from the event uh to the store so let's actually write our first unit test now before we do that we will do yarn ads matchstick a s which is a helper library that will aid us in writing these tests so if we peek into the package.json file there we go match the ks is the newest version uh we will also add a quick script here your test which will do graph test there we go now into in the root of my subgraph i will do a directory called tests and then inside i will create a gravity test ts cs5 now um first thing we need to do first thing i'm gonna do is i'm going to paste a helper function uh and i highly recommend you to write helper function for your specific events because it makes the process much easier uh let me just import the things that we need to import um by the way a lot of things will show up as errors in my id but don't worry about that that's just because the id thinks that this is typescript but we know that this is actually assembly script um cool so what this helper function does is it expects a couple of values which are then used in order to create a mark event so a mock new gravatar event with these values uh i'm not going to go in depth because it's actually really simple it's just it's just creating a mock event in a nutshell um so we also need this new mark event function but that one is going to come from the library that we've just installed so we are going to do we are going to do import new mock newmark events from and this is a little a little this is kind of a weird syntax but uh we need to say from match to ks dash assembly dash index just get used to that uh and there we go we actually have all of the boilerplate necessary to write our first unit test so we can say we also need to import the test function from match the ks and we can say test can handle new gravatar this will also need a callback which is essentially our test and then inside we will say new let new gravatar event route our event equals our helper function create new gravatar event and we're gonna give it everything it needs so first off it needs a uh event id which will be something like uh i don't know one two three four then we need the owner address which can be whatever we want let's just say it's something like this then we're going to need what was it display name so the display name uh in the event which will be my gravatar name and last but not least we need our image url which can be whatever we want even an empty string will do but we will say myimage.jpg or something there we go now uh we will use our handler function so let's actually import our handler function handle new gravatar from source gravity there we go now we will say handle new gravatar with our newly generated mock event and that's really all there is to it now just to make sure that this is working we're going to say we're going to bring in the assert class i don't know from match the ks and we will call assert field equals uh then it wants the entity type in our case that it's a graph it's a gravatar i believe let me just double check um yep that's what we need uh then i can grab a gravatar then we need a um entity id which is this thing as a string then we need to give it the name of the field whose value we want to check so in our case that will be display name and then the expected value which is my gravatar my grammatical name in our case let's do the same thing for the image url let's say image url we want that to equal this all right now in order to run our test we just do yarn test and what this is going to do is it's going to check our operating system and then it's going to fetch the respective matchstick binary for our os in the near future this approach will be shifted to docker so you won't be experiencing any any issues with it there we go um our test has passed can handle your gravatar is working let me just mess up something real quick just to show you guys what it looks like when a test fails and there we go expected video image url to equal this but was my image.jpg so i hope you guys like that um that's all i have for this video so see you in the next one
Up Next

The Bootstrap Paradox Explained | Time Travel Theory
@timeywimeywho
391.3K views•2024-10-02

IFS Therapy Demonstration: Complete Session with Unburdening
@IFSCA
95.9K views•2021-01-13

How to Deploy a Subgraph to Subgraph Studio: A Step-by-Step Guide for Blockchain Developers
@GraphProtocol
7.5K views•2023-05-19

Game of Thrones Opening Credits: A Cinematic Analysis
@gameofthrones
46.3M views•2011-04-18
Related Study Plans & Knowledge Roadmaps
Structured learning paths in General & Interdisciplinary Studies



































