Test mobx-state-tree Models by Recording Snapshots or Patches

Share this video with your friends

Send Tweet

Testing models is straightforward. Especially because MST provides powerful tools to track exactly how your state changes over time. Track snapshots, action invocations or even patches to verify the correctness of your actions!

In this lesson you will learn:

  • To obtain immutable snapshots of the state using getSnapshot
  • To record snapshots using onSnapshot
  • To store and test modifications over time using onPatch
  • Using Jest's snapshot test feature to verify snapshots and patches
  • That MST can infer the type of a snapshot for you
Gar Liu
Gar Liu
~ 7 years ago

Can the model intercept changes as a way to play animations? When the animation completes, then apply the model changes. I tried something like this:

addMiddleware(myModel, (call, next) => { // setTimeout is to simulate an animation. setTimeout(()=>{ next(call) }, 2000) });

But an error of: Error: [mobx-state-tree] Neither the next() nor the abort() callback within the middleware for the action: