Testing

Although the presentation framework itself is thoroughly tested, consumers should still verify they get expected results for their iModel + ruleset combinations. The @bentley/presentation-testing package is delivered purely for that reason.

The package delivers an API that allows creating hierarchies for supplied iModels and rulesets. Consumers can then verify the result using tools of their liking. Our recommendation is to use snapshot testing for 2 reasons:

  1. resulting hierarchies get rather large - testing the them in code might be difficult
  2. snapshots protect against regressions

Example

An example of setting up snapshot tests with the @bentley/presentation-testing package:

describe("RulesetTesting", () => {
  let imodel: IModelConnection;
  const imodelPath = "assets/datasets/Properties_60InstancesWithUrl2.ibim";

  before(async () => {
    // initialize presentation-testing
    await initialize();
  });

  after(() => {
    // terminate presentation-testing
    terminate();
  });

  beforeEach(async () => {
    // set up for testing imodel presentation data
    imodel = await IModelConnection.openSnapshot(imodelPath);
  });

  afterEach(async () => {
    await imodel.closeSnapshot();
  });

  it("generates correct hierarchy for 'Items' ruleset", async () => {
    const builder = new HierarchyBuilder(imodel);
    // generate the hierarchy using a ruleset id
    const hierarchy = await builder.createHierarchy("Items");
    // verify through snapshot
    expect(hierarchy).to.matchSnapshot();
  });

  // tslint:disable-next-line:only-arrow-functions
  it.skip("generates correct content for 'Items' ruleset", async function () {
    const builder = new ContentBuilder(imodel);
    // generate content using ruleset id
    const instances = await builder.createContentForInstancePerClass("Items");

    // verify through snapshot
    // we loop through each instance and create a separate snapshot file
    // because snapshot engine has difficulties parsing big files
    for (const instance of instances) {
      // not providing filename and snapshot name to the 'matchSnapshot', because it seems
      // to ignore them when ChaiJestSnapshot.setFilename and setTestName is used
      configureSnapshotLocation(this.test!, "ruleset-testing-content-snaps", instance);
      expect(instance.records).to.matchSnapshot();
    }
  });

  it("generates correct hierarchy for 'default' ruleset", async () => {
    const builder = new HierarchyBuilder(imodel);
    // import ruleset from file
    const ruleset = require("../../test-rulesets/Rulesets/default.json");
    // generate the hierarchy
    const hierarchy = await builder.createHierarchy(ruleset);
    // verify through snapshot
    expect(hierarchy).to.matchSnapshot();
  });

});

Things to keep in mind

  • Run initialize() before and terminate() after the tests
  • Don't forget to close the iModel connection
  • Ruleset can be provided either as an ID of already registered ruleset or as a Ruleset object. The object can even be imported from a JSON file:
    await builder.createHierarchy(require("rulesets/YourRuleset"))

Last Updated: 08 January, 2020