Introduction

 

JSONNUNIT is a BDD assertion style unit testing framework for JSONNET through an ‘expect’ interface. Basically you chain together natural language assertions which makes testing much easier by giving you lots of assertions you can run against your code. This framework is based on Mocha: a simple, flexible, fun javascript test framework for node.js & the browser.

Requirements

Only Active LTS Node versions (above 8.5.x) are supported. You can add the node package to your CLI to ensure users are on a specific Node version.

Installation

Install with npm globally:

$ npm install --global jsonnunit

Or add it as a development dependency for your project:

$ npm install --save-dev jsonnunit

Quickstart

Creating a new testcase for your project:

$ npm install --global jsonnunit
$ mkdir tests
$ $EDITOR tests/my-jsonnet-object.jsonnet # or open with your favorite editor

In your editor:

local JSONNUNIT = import "../jsonnet/jsonnunit.libsonnet";
local TESTED_OBJECT = import "../my-jsonnet-file.jsonnet";

JSONNUNIT
  .describe('Test default functionality',
    JSONNUNIT
      .it('tests that my jsonnet object has a key with "foo" that is empty',
        JSONNUNIT.expect(TESTED_OBJECT.foo).to.be.empty,
      )
  )

Next in your terminal you can execute the testcase:

$ jsonnunit


  /tests/my-jsonnet-object.jsonnet
    Test default functionality
      ✔ tests that my jsonnet object has a key with "foo" that is empty


  1 ✔ passing (068.40 ms)
  0 ✗ failing

Now if your project is using NPM, you can add JSONNUNIT as part of your package so that NPM can test your JSONNET objects. Add the following lines to your package.json file:

"scripts": {
  "test": "jsonnunit"
}

Now you can execute your tests with:

$ npm test