xrm-mock

A fake implementation of the Xrm object model. Used for testing Dynamics 365 client-side scripts.

Usage no npm install needed!

<script type="module">
  import xrmMock from 'https://cdn.skypack.dev/xrm-mock';
</script>

README

Build Status npm version Downloads Test Coverage

:books: xrm-mock

Join the chat at https://gitter.im/xrm-mock/Lobby

xrm-mock is a fake implementation of the Dynamics 365 Client API and Xrm object model. Written in TypeScript against @types/xrm definitions.

xrm-mock-generator is an opinionated toolset for building fake Xrm objects.

Installing

For the latest stable version

npm install xrm-mock -D

Usage

Import XrmMockGenerator in your unit test file

import { XrmMockGenerator } from "xrm-mock";

Initialise a global Xrm object

XrmMockGenerator.initialise();

Customise your form by adding attributes

XrmMockGenerator.Attribute.createBool("new_havingfun", true);

Invoke your code and make your assertions

Contact.onLoad();
expect(Xrm.Page.getAttribute("new_havingfun").getValue()).toBe(true);

Example

This example demonstrates a script with an onLoad event handler registered on a contact form. When invoked, it changes the firstname attribute's value to Bob. See the Wiki for a visual demo.

src/contact.ts

export default class Contact {
 public static onLoad() {
   Xrm.Page.getAttribute("firstname").setValue("Bob");
 }
}

test/contact.test.ts

import Contact from "../src/contact";
import { XrmMockGenerator } from "xrm-mock";

describe("Contact", () => {
  beforeEach(() => {
    XrmMockGenerator.initialise();
    XrmMockGenerator.Attribute.createString("firstname", "Joe");
  });

  it("should initially be called Joe", () => {
    let name = Xrm.Page.getAttribute("firstname").getValue();
    expect(name).toBe("Joe"); // Pass
  });

  it("should change name to Bob onLoad", () => {
    Contact.onLoad();
    let name = Xrm.Page.getAttribute("firstname").getValue();
    expect(name).toBe("Bob"); // Pass
  });
});

Contribute

  • Submit bugs
  • Implement a new function by inheriting @types/Xrm
    • Test your code using npm run test
    • Lint your code using npm run lint
    • Build your code using npm run build

Roadmap

  • Increased test coverage
  • Increased implementation against different versions of @types/Xrm (8.2 and 9)
  • Automatic generation of attributes from a given Dynamics organisation