Skip to main content
Frontend tutorial

6. Tests

In chapter 4 we checked the counters by hand: type 42, click +1, see 43. After the next change in the store, you'd have to click through everything again. Tests can do that clicking for us.

We use Vitest. It reuses the Vite setup the app already has. This is also where keeping the state in MobX classes pays off: a store is just a class, so a test can create one, call its actions and check the values without rendering any React.

The counter store from chapter 4 is already here. Run tests runs your tests right in the page.

Lessons​

  1. Write the first test
  2. Your turn
  3. More testing (Bonus)

06 / Tests

The test does the clicking for us

The test does the clicking for usEvery test has three parts. Arrange: create a CounterStore. Act: setValue 42 and add 1. Assert: expect 43. A new test fails once first: the new counter test expected 1 and received 0. Fix the test to expect 0 and it passes.01Arrangea newCounterStore02ActsetValue 42,add 103Assertexpect 43new counter, first runexpected 1,received 0fix 1 → 0run againexpected 0,received 0every new test fails once

Write the test, watch it fail, then make it pass.