Skip to content

Adaptation of Jest JavaScript Testing Framework for ThingWorx Services

License

Notifications You must be signed in to change notification settings

jmmoser/thingworx-jest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Jest JavaScript Testing Framework for ThingWorx Services

Installation

  1. Create a generic thing and give it a name (e.g. JestFramework)
  2. Add a new service on the thing
  3. Give the service a name (e.g. importScript)
  4. Set the output of the service to STRING data type
  5. Copy the contents of jest.js and paste into the service script text editor
  6. Save the service
  7. Save the thing

Usage

After installation, you may reuse this script in any service on any thing/template/shape by including the following snippet at the beginning of the service:

/** import { expect, describe, test } */
eval(Things['JestFramework'].importScript())(this); // jshint ignore:line

Make sure to set the output of the service to INFOTABLE.

Note: I typically create a separate service on each of my things/templates/shapes called __UNIT_TESTS__ThingName which runs all of the unit tests for the entity's services.

Add your unit tests after the snippet:

describe('Echo service', function() {
  test('Normal input', function() {
    var input = { a: 1 };
    expect(me.EchoService(input)).toEqual(input);
  });
});

Then execute the service. The result is an infotable with unit test results.

How This Works

ThingWorx does not allow passing functions as arguments to services so all javascript functions need to be defined inside of the service. To emulate exporting a script, the entire framework is placed inside of a function and Function.prototype.toString() is utillized to get the source code for the function. To emulate importing a script, eval() is utilized to evaluate the source code returned inside of the calling service. The export of the script source code could be avoided by placing the source string directly in a property and then imported by getting the value of the property instead of calling the service.

Todo

  • toContain

Releases

No releases published

Packages

No packages published