How to choose right unit test library for JavaScript frameworks?

Parthasarathy S

November 10, 2020 -

  • 7 min read
  • “A drone view of the trees changing colors during Autumn in Grayling, Michigan, United States” by Aaron Burden on Unsplash

    There are multiple unit test frameworks for JavaScript available today and choosing the right one for you is solely based on the following criteria (that suites you)

    Criteria

    • Flexibility — Ability to extend or choose your own plugins for testing like assertions, mocks, async handling…or you prefer the tool has built it all in and no need for any 3rd party plugins, configurations.
    • Configuration — Minimalistic configuration but maximum features. This again is based on how flexible the framework should be like above and so your configuration.
    • Community — How vast is the community and how frequently they update their toolset with new features / bug fixes
    • Universal — Support for multiple languages, UI frameworks such as ES6, Typescript, Angular, React, Vue…..
    • Performance — Ability to run test quicker and more important parallel execution
    • Learnability — Quick to learn and ready to go in no time.
    • Support for State Management — Ability test stores if you have used any of Redux, Mobx…

    Now let us look at the unit test frameworks one by one (only the popular ones compared)

    Jasmine

    The most popular and matured unit test library. Frameworks like Jest are based on Jasmine

    Pros

    • Brings in all basic features like assertions, spies, mocks to start writing your test cases immediately
    • Integrates well with AngularJS & Angular frameworks and preferred one too
    • Follows BDD structure (Behaviour Driven Development)

    Cons

    • Need to rely on other libraries for coverage reports
    • No snapshot testing possible where we can easily compare a data structure to the one expected
    • Tends to degrade performance for larger application and no. of test cases grows beyond 200
    Choose Jasmine for Angular applications and for small — medium scale apps.

    Mocha

    Although Jasmine is most popular, Mocha is the most used unit test library. The reason being it is highly flexible, configurable and a lot of plugins available to suit your needs, complexity of your application

    Pros

    • Highly configurable & extensible with many plugins exist for various needs such as assertion, spies, mocks, parallel execution, code coverage
    • Backed by a lot of community and increasing no. of plugins
    • Out performs all other unit test libraries in performance when you choose the right set of plugins
    • Works well with AngularJs, Angular, React, Vue, nodejs
    • Great tooling support for popular IDEs such as VS Code, Webstorm…

    Cons

    • Steep learning curve due to heavy configurations and setup required
    • Need expertise and experience to choose the right set of plugins
    • Need to write a lot of config and scripts to perform unit test and integrate with CI / CD tools
    Choose Mocha if you need a high configurable and extensible toolset for unit test features. Also, you are aware of what you are doing and have good experience in unit testing

    Jest

    Jest is becoming more popular and out run Jasmine for non Angular apps. It is based on Jasmine and competes with Mocha.

    Pros

    • Minimalistic or no config / setup required to start your unit testing
    • Easy to learn as required features are built in and comes out of the box
    • Ability to do snapshot testing for regressions and easily update Jest of change in component rendering
    • Built in coverage report generation
    • Preferred for ReactJS apps and supports Redux, Mobx
    • Good tooling support for IDEs such as VS Code, Webstorm
    • Can use for Angular, React, Vue, nodejs, typescript based applications

    Cons

    • Closed community support of those using ReactJS based apps
    • Still less preferred and used for non ReactJS apps (Angular, Vue, nodejs…)
    • Better performing than Jasmine but far behind Mocha
    Choose Jest for ReactJS based application and more so if you use Redux or Mobx. Can give it a go for others (Angular, nodejs, Vue) if you are well experienced with unit testing

    Sinon

    Sinon is a standalone test library for testing with spies, mocks, stubs and basic set of assertions. It is typically used on top of Mocha

    Pros

    • Great for testing with Stubs, Fakes, Mocks, Spies when you reply heavily on APIs
    • Integrates well with Mocha, Jest, Jasmine
    • Can use for Angular, React, Vue, nodejs based applications

    Cons

    • Need Mocha or Jest or Jasmine to work along with it
    • Less community support
    Use Sinon if you already use Mocha and need single library for Stubs, Fakes, Spies, Mocks….