Testing Main Module

You can run code from main module in isolation for testing purposes

While we encourage setting up automated testing using Mocha you can also test your main module methods manually. Typically you will want to test the output of the doLookup method to make sure it is doing what you expect. To run the doLookup method in isolation from Polarity you can create a Node.js program that requires it and then manually call doLookup.

Start by creating a file called test-runner.js and placing it in the root directory of your integration.

In the test-runner.js file require your main-module (if you are following the default guidelines this file will be called integration.js).

test-runner.js
'use strict';

let myIntegration = require('./integration');

Next, create an options object and set any option values required by your integration. These will be passed to the doLookup method.

test-runner.js
let options = {
    apiKey: '<set your api key>',
    warnOnLookupLimit: true
};

Create an array of entity objects to be passed to your doLookup method. Note that you only need to provide the properties used by your integration.

test-runner.js
let entities = [
    {
        value: 'fd904addbdfe548c22ffa5223ed9eee7',
        isHash: true
    },
    {
        value: '95.108.142.138',
        isIPv4: true
    }
];

Call the doLookup method of your integration passing in the entities, options and a callback method.

test-runner.js
myIntegration.doLookup(entities, options, function(err, result){
   if(err){
       console.info("ERRORS:");
       console.info(JSON.stringify(err, null, 4));
   }else{
       console.info("RESULTS:");
       console.info(JSON.stringify(result, null, 4));
   }
});

Execute your program from the command line on the server using node. The program will output any errors or the result. In addition, if you have syntax errors in your integration those will be caught and displayed in the console.

node test-runner.js

Final Code

The following is the full test-runner.js program which can be adapted to your needs.

test-runner.js
'use strict';

let myIntegration = require('./integration');

let options = {
    apiKey: '<set your api key>',
    warnOnLookupLimit: true
};

let entities = [
    {
        value: 'fd904addbdfe548c22ffa5223ed9eee7',
        isHash: true
    },
    {
        value: '95.108.142.138',
        isIPv4: true
    }
];

myIntegration.doLookup(entities, options, function(err, result){
   if(err){
       console.info("ERRORS:");
       console.info(JSON.stringify(err, null, 4));
   }else{
       console.info("RESULTS:");
       console.info(JSON.stringify(result, null, 4));
   }
});

In most cases you can rely on the automated error reporting from the Polarity UI. Creating a test-runner as in the example above is valuable when you are trying to trace through particular sections of code to identify issues.

Last updated