SPFx Unit Test With React using Jest and Enzyme - Working Solution
Testing is an important phase of all software development lifecycle. It should be a part of continuous development and achieve better results. SharePoint Framework is no exception to this. In this article we can learn initial setup for SPFx unit testing using Jest and Enzyme.
Importance of unit testing
Unit testing is important because of the following reasons
- Early bug detection
- Makes the process agile
- Improves the quality of code
- Facilitates changes and simplifies integration
- Provides documentation of the system
- Simplifies debugging process
Create SPFx Web Part
Open command prompt and create a new Directory
- md spfx-react-unit-test
Navigate to the created directory
- cd spfx-react-unit-test
Run the Yeoman SharePoint Generator to create the SPFx solution.
- yo @microsoft/sharepoint
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
Selected Choice: Hit Enter
Target for the component: Here we can select the target environment where we are planning to deploy the client web part i.e. SharePoint Online or SharePoint On-Premises (SharePoint 2016 onwards).
Selected Choice: SharePoint Online only (latest)
Place of files: We may choose to use the same folder or create a sub-folder for our solution.
Selected Choice: Same folder
Deployment option: Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected Choice: N (install on each site explicitly)
Permissions to access web APIs: Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice: N (solution contains unique permissions)
Type of client-side component to create: We can choose to create a client-side web part or an extension. Choose web part option.
Selected Choice: WebPart
Web part name: Hit enter to select the default name or type in any other name.
Selected Choice: default(Enter)
Web part description: Hit enter to select the default description or type in any other value.
Selected Choice: Unit test SPFx with Jest
Framework to use: Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected Choice: React
It's take some time to create scaffold. Once the scaffolding process is completed it will ready to add jest in this SPFx for default HelloWorld Component.
In the command prompt type the below command to open the solution in the code editor of your choice.
- code .
NPM Packages Used
Developed by Airbnb, it represents test utilities for React. On the command prompt, run the below command.
- npm install enzyme enzyme-adapter-react-16 react-test-renderer @types/enzyme --save-dev --save-exact
It supports asserts, mocking, code coverage, coverage threshold for continuous deployment, and summary report.
- npm install jest jest-junit ts-jest @types/jest --save-dev --save-exact
Allows to test SASS / LESS / CSS imports.
- npm install identity-obj-proxy --save-dev --save-exact
Setup Jest with SPFx
We will use Jest to install npm packages in our SPFx solution.Open the package.json file.
Under the “Scripts” section for “test” configuration, replace “gulp test” with “jest” or you can add new script seperately for both gulp and jest like below.
In newer version of SPFx, There are lot of issues generating during the test. To acheive this setup without error do below,
Add below mentioned config files in your project root folder,
- npm install enzyme-to-json @babel/preset-env @babel/preset-typescript @babel/preset-react --save-dev --save-exact
Add Tests to SPFx WebPart
In Code Editor, follow below steps to add some tests to our SPFx solution.
Add “__tests__” folder under “src\webparts\helloWorld\”.
Under “__tests__” folder, add a file “HelloWorld.test.ts”.
Run Test Cases
On the command prompt and run the below command to execute the test cases.