This repo is showcasing Azure DevOps React based UI Extension unit testing for an advanced example.
The focus is on how to successfully mock the JavaScript/TypeScript based Azure DevOps Extension API and SDK and how create unit tests to get a statement coverage of 100%. Including the use of "advanced" features like Azure DevOps event hooks and integrating with an public Rest API endpoint external to Azure DevOps.
You can copy the mocking implementation and have a look into the unit tests to get ideas on how to sucessfully mock and spy on the Azure DevOps SDK/API features your project is using.
-
Install Docker and VSCode for dev container environment [alternatively install node.js 14]
-
Clone this repo
-
Open the folder in the dev container
-
Install modules
npm install
-
Run unit tests
npm run test
-
You should see the following result
-
If you open now the
coverage/cobertura-coverage.xml
file you should see following result
Alternatively you can check out the steps in the provided pipeline build template.
The Azure DevOps Extension package contains two Work Item Form Control components.
The VersionedItemLink component can link versioned items (files) from a destinct Azure Git repo branch (configurable in component options) to the work item. In order to do so it will save the state via a Rest API external to Azure DevOps Services.
The MultiIdentityPicker component let's you select multiple identites from the Azure DevOps Organisation associate AAD Tenant and saves the information into a text field of the work item.
Main difference between both components is that one will save the state externally the other internally to Azure DevOps Services.
Both have in common that they will use the JS/TS based Azure DevOps Extension SDK and API to communicate with Azure DevOps Service and do logging towards an Azure based Application Insights instance.
There are following 5 challenges for the unit test:
- Mocking return values of the Azure DevOps Extension SDK methods
- Trigger the Azure DevOps Extension WorkItemForm events of the control (part of SDK)
- Mocking return values and classes of the Azure DevOps Extension API
- Mock the external RestAPI calls and spy on the values transmitted
- Mocking and spy on the logging methods (Application Insights SDK)