- Create a generic thing and give it a name (e.g. JestFramework)
- Add a new service on the thing
- Give the service a name (e.g. importScript)
- Set the output of the service to STRING data type
- Copy the contents of jest.js and paste into the service script text editor
- Save the service
- Save the thing
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.
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.
- toContain