Unit testing your client-side customisations for Dynamics 365 just got easier.

xrm-mock is a package that lets you generate a fake implementation of the Xrm object in your client-side code, allowing you to unit test your scripts.

How do I install it?

Simply download xrm-mock's npm package by running

npm install xrm-mock -D

Then import the XrmMockGenerator module and initialise a global Xrm object

import { XrmMockGenerator } from "xrm-mock";
XrmMockGenerator.initialise();

Now you have a global Xrm object, which you can add attributes to, to simulate a real form in Dynamics 365.

Here's how you add basic attributes:

const stringAttribute = XrmMockGenerator.Attribute.createString("firstname", "Joe");
const boolAttribute   = XrmMockGenerator.Attribute.createBool("havingFun", true);
const dateAttribute   = XrmMockGenerator.Attribute.createDate("birthdate", new Date(1980, 12, 25));
const numberAttribute = XrmMockGenerator.Attribute.createNumber("units", 2);

Now for a lookup attribute:

const lookupAttribute = XrmMockGenerator.Attribute.createLookup("primarycustomerid", {
  entityType: "contact",
  id: "{00000000-0000-0000-0000-000000000001}",
  name: "Joe Bloggs",
});

And finally an option set attribute:

const optionSetAttribute = XrmMockGenerator.Attribute.createOptionSet("countries", 0, [
    { text: "Austria", value: 0 },
    { text: "France", value: 1 },
    { text: "Spain", value: 2 },
]);

See xrm-mock's readme and Wiki for examples of how to unit test against your newly created attributes.

What's changed in v3?

As part of xrm-mock v3 release, attribute create methods only need two arguments: a schema name and a value, as shown in the above code snippet.

Why xrm-mock?

Faking Xrm is hard and time consuming: it has several nested objects which require a detailed understanding to fake correctly. xrm-mock is gaining traction with the open source community, and can expect to see more releases in the coming months. In fact, Daryl LaBar has become a contributor and he's already hit the ground running.