Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Test a React Applications using MobX with Jest

    Basarat Ali SyedBasarat Ali Syed
    reactReact
    jestJest
    mobxMobX
    typescriptTypeScript

    MobX is independent of React and really you can use whatever testing framework you want with it. Testing MobX is essentially as easy as testing simple JavaScript.

    This lesson demonstrates an example of testing mobx application state with Jest.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Here, we have a simple list application from a previous lesson. It allows us to add items and reset the items, if required. The behavior of this application is described by the application state class. In order to test the application behavior, we can go a long way by simply testing that the app state behaves as expected.

    Since all we need to do is test simple JavaScript, we can use any testing framework like Mocha, Jasmine, or Jest with MobX. Let's go with Jest as that is the most common one used by React developers.

    We already have Jest set up using the standard Typescript Jest process. We have a script target to run Jest. Installation was a simple npm install of the types, Jest, Jest itself, and ts-jest.

    We've also configured Jest with the simple copy-paste of jest.config.js from our Typescript React course. Now, let's go ahead and add a test for the app state. You will test all the features that are provided by app state in individual tests.

    We create a test file. Bring in the singleton app state. Before each test, you will go ahead and reset the app state to its initial state. To test that it is going to store a list of items, we simply check the initial items to be an empty array.

    Next, to allow maintaining a current string as it gets typed, we check that the initial value is an empty string. Then, we simulate a user type of character A, and ensure that the current item updates.

    Next, we type another character to make the value AB, and ensure that the current item updates accordingly. To test the ability to add this current string to the list of items, we will go ahead and change the current item to some value and add it to the list.

    We expect the list to contain this item. For the option to reset the items in the current string, we simply add a current item to the list. We then reset the app state and expect the initial conditions.

    Now, let's go ahead and run the test in the terminal using npm-t. You can see that the test passed with flying colors. Notice that as MobX is transparent reactive, you get to write simple JavaScript tests for your state behavior without having to go through complex APIs and patterns.

    A neat feature of Jest is built in coverage support that you can activate with the minus minus coverage flag. You can see that our tests cover 100 percent of the application state behavior.