In Celestial Systems, whenever any code is written, we always think of it from two perspectives. First, whether the code suffices the requirements and was developed as per the specifications. Second, whether we can, as a developer, unit test the written code.
Considering unit testing as the benchmark of the quality of our code and the unit test coverage as the parameter of quality test; we are always very peculiar in choosing the right unit testing framework and it is based on multiple factors.
A few of them are as follows:
- Synergy between the development library/framework and the unit testing library/framework
- Speed and efficiency
- Ease in configuration
- Community support
- Features like code coverage, mocking, assertions and option to run tests
- Developers of the framework
Based on Celestial’s consideration of the above-mentioned factors and placing them in a React/React Native application unit testing scenario, JEST comes up as the best unit testing solution beating other frameworks like Mocha or Jasmine.
Factors | JEST | Mocha | Jasmine |
---|---|---|---|
Synergy with React | Both JEST and React are developed by Facebook and Facebook uses JEST to unit test everything they build on React | Requires major configuration steps to make it work with the React app. | Requires major configuration steps to make it work with the React app. |
Speed | JEST parallelizes test runs across workers | No parallel task running | No parallel task running |
Ease in configuration | Requires zero configuration to get it running | Requires configuration | Requires configuration |
Community Support | Right after its release, React developers switched from Mocha and Jasmine to JEST and created a community for it | Community size is decreasing for React apps because of the increasing interest in JEST | Community size is decreasing for React apps because of the increasing interest in JEST |
Features | Have in-built Istanbul for code coverage, have its own assertion and mocking library | Need to configure Istanbul, Chai and Sinon Stub library separately | Need to configure Istanbul, Chai and Sinon Stub library separately |
Developers | Developed and used by Facebook | Open Source community | Open Source community |
Now, let us elaborate on the features of the JEST
1. Powerful mocking library
In Unit testing, the whole project is subdivided into small units and testing them independently is the recommended way. But inside units with third party library functions a mocking library is required to mock API calls. In JEST this is provided as an inherent module. Please refer to the links below for more information.
- Mocking Functions
- Mocking Modules
2. Zero Configuration
JEST comes preconfigured with all the strengths. All that needs to be done is to install it and then run it.
• npm install --save-dev jest
• Create tests
• Add into NPM script
{
"scripts": {
"test": "jest"
}
}
3. Super-Fast
JEST provides results with coverage in no time. Its features are:
- Fast, interactive watch mode which runs only test files related to changed files.
- It is optimized to provide signals quickly.
- Sandboxed test files and automatic global state resets for every test so that no two tests conflict with each other.
- JEST parallelizes test runs across workers.
4. Snapshot Testing
Create and capture snapshots of the rendered React element and keep a check on whether it has changed or not. If it is changed in an undesired run, JEST will inform us.
5. Built-in code coverage reports
No requirement to configure Istanbul for the test coverage reports, easily create code coverage reports using –coverage. No additional setup or libraries are needed.
6. Universal for React application
A developer can enjoy the features and benefits of JEST in both and React and React-Native without ever getting worried.
7. Compatibility with Babel and other compile-to-JavaScript language
JEST works with any compile-to-JavaScript language and integrates seamlessly with Babel and with TypeScript through ts-jest.
Thus, based on our experience in working with React/React Native applications and creating and writing unit tests for them, Celestial opts to enlist JEST to get the job done. It clearly beats all the other frameworks in the scenario.