JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

How to - Unit Test the JavaScript in Microsoft Dynamics 365 Using XrmPage-Mock Framework

Unit Test the JavaScript in Microsoft Dynamics 365 Using XrmPage-Mock Automated Unit Testing is has now become one of the most important aspects of any project implementation.

Unit Testing makes sure that code works as expected and also if there are any changes or any new functionality is implemented, running unit test makes sure that code is still behaving properly and addition of changes hasn’t broken it.

In this article we will cover unit testing CRM’s JavaScript in Microsoft dynamics 365 services, through the use of XrmPage-Mock framework.

Download it from the below location.

https://github.com/ambek/XrmPage-Mock

XrmPage-Mock as the name implies mocks the XrmPage object and it functions.

To get started, create a blank ASP.NET Web Application.

JavaScript & Microsoft Dynamics 365

Select the Empty template.

JavaScript & Microsoft Dynamics 365

Along with XrmPage-Mock we will be using QUnit, JavaScript Unit testing framework.

It can be used to test JQuery based projects along with generic JavaScript code.

https://qunitjs.com/

Download the latest version.

JavaScript & Microsoft Dynamics 365

Create a new Script and CSS folder in the solution to add the Qunit and XrmPage-Mock libraries

Also add 2 JavaScript file one which needs to be tested i.e. Contact.js and other is the test file i.e. Test_Contact.js, along with a HTML page used for test results as shown below.

JavaScript & Microsoft Dynamics 365

Below is the sample of JavaScript script that we are going to unit test.

Based on gender code, we are showing and hiding the birthdate field on the form.

Contact.js file

JavaScript & Microsoft Dynamics 365

Below is our Test File. It mocks the attributes used and then calls the function.

Test_Contact.js file

JavaScript & Microsoft Dynamics 365

Below is the HTML page which will display the test result.

Add all the required css and js files along with the DIV as shown below.

JavaScript & Microsoft Dynamics 365

Open the page in the browser to check the Test Results

JavaScript & Microsoft Dynamics 365

In this article we saw how easy it is to mock XrmPage using XrmPage-Mock library and then use JavaScript Testing Framework like QUnit to be up and running in no time.

 
Ast Note

Some of our clients

team