Node.js test reporting

Using Mocha?

If you use the Mocha test framework no code is necessary to integrate. Use the Tesults Mocha plugin and ignore the instructions below.

Using Jest?

If you use the Jest test framework no code is necessary to integrate. Use the Tesults Jest plugin and ignore the instructions below.

Installation

The tesults package is hosted on npm: https://www.npmjs.com/package/tesults. Install it by running:

npm install tesults --save

Configuration

Include the tesults library by using require:

const tesults = require('tesults');

Usage

Maintain an array to store your test cases.

let cases = [];

A test case is an object containing test case properties.

let testCase = {};
testCase.name = 'Test 1';
testCase.result = 'pass';
cases.push(testCase);

Once all of your test cases have been added to the cases array create a data object for uploading.

let data = {
  target: 'token'
  results: {
    cases: cases
  }
}

The target value, 'token' above should be replaced with your Tesults target token. If you have lost your token you can regenerate one from the config menu.

The final step is to upload results to Tesults using the results function.

tesults.results(data, function (err, response) {
  // err is undefined unless there is a library error

  // response.success is a bool, true if results successfully uploaded, false otherwise
  // response.message is a string, if success is false, check message to see reason
  // response.warnings is an array of strings, if non empty then issues with file uploads
  // response.errors is an array of strings, if success is true this is empty
});

Test case properties

This is a complete list of test case properties for reporting results. The required fields must have values otherwise upload will fail with an error message about missing fields.

PropertyRequiredDescription
name*Name of the test.
result*Result of the test. Must be one of: pass, fail, unknown. Set to 'pass' for a test that passed, 'fail' for a failure.
suiteSuite the test belongs to. This is a way to group tests.
descDescription of the test
reasonReason for the test failure. Leave this empty or do not include it if the test passed
paramsParameters of the test if it is a parameterized test.
filesFiles that belong to the test case, such as logs, screenshots, metrics and performance data.
startStart time of the test case in milliseconds from Unix epoch.
endEnd time of the test case in milliseconds from Unix epoch.
durationDuration of the test case running time in milliseconds. There is no need to provide this if start and end are provided, it will be calculated automatically by Tesults." : "Duration of the build time in milliseconds. There is no need to provide this if start and end are provided, it will be calculated automatically by Tesults.
_customReport any number of custom fields. To report custom fields add a field name starting with an underscore ( _ ) followed by the field name.

Build properties

To report build information simply add another case added to the cases array with suite set to [build]. This is a complete list of build properties for reporting results. The required fields must have values otherwise upload will fail with an error message about missing fields.

PropertyRequiredDescription
name*Name of the build, revision, version, or change list.
result*Result of the build. Must be one of: pass, fail, unknown. Use 'pass' for build success and 'fail' for build failure.
suite*Must be set to value '[build]', otherwise will be registered as a test case instead.
descDescription of the build or changes.
reasonReason for the build failure. Leave this empty or do not include it if the build succeeded.
paramsBuild parameters or inputs if there are any.
filesBuild files and artifacts such as logs.
startStart time of the build in milliseconds from Unix epoch.
endEnd time of the build in milliseconds from Unix epoch.
durationDuration of the build time in milliseconds. There is no need to provide this if start and end are provided, it will be calculated automatically by Tesults.
_customReport any number of custom fields. To report custom fields add a field name starting with an underscore ( _ ) followed by the field name.

Here is an example of a data object using various test case properties.

var data = {
  target: 'token',
  results: {
    cases: [
    {
      name: 'Test 1',
      desc: 'Test 1 description.',
      suite: 'Suite A',
      result: 'pass'
    },
    {
      name: 'Test 2',
      desc: 'Test 2 description.',
      suite: 'Suite A',
      result: 'fail',
      reason: 'Assert fail in line 203, example.js',
      files: ['/full/path/to/file/log.txt', 'full/path/to/file/screencapture.png'],
      _CustomField: 'Custom field value',
      steps: [
        {
          name: 'Step 1',
          desc: 'Step 1 description.',
          result: 'pass'
        },
        {
          name: 'Step 2',
          desc: 'Step 2 description.',
          result: 'fail'
          reason: 'Assert fail in line 203, example.js'
        }
      ]
    },
    {
      name: 'Test 3',
      desc: 'Test 3 description.',
      suite: 'Suite B',
      start: Date.now() - 60000,
      end: Date.now(),
      result: 'pass',
      params: {
        param1: 'value1',
        param2: 'value2'
      }
    }
    ]
  }
}

Files

The example above demonstrates how to upload files for each test case. In practice you would generate the array of file paths for each test case programatically.

To make this process simpler we suggest you write a helper function to extract files for each test case and this can be easily achieved by following a couple of simple conventions when testing.

1. Store all files in a temporary directory as your tests run. After Tesults upload is complete you can delete the temporary directory or overwrite it on the next test run.

2. Within this temporary directory create subdirectories for each test case so that files for each test case are easily mapped to a particular test case.

If you do not use test suites then this structure may be as simple as this:

  • expanded temporary
    • expanded Test suite name
      • expanded Test case name
        • file 1
        • file 2

For example:

  • expanded temporary
    • expanded Test Suite A
      • expanded Test 1
        • test.log
        • screenshot.png
      • expanded Test 2
        • test.log
        • screenshot.png
    • expanded Test Suite B
      • expanded Test 3
        • metrics.csv
        • error.log
      • expanded Test 4
        • test.log

If not using test suites then omit the suite directories:

  • expanded temporary
    • expanded Test 1
      • test.log
      • screenshot.png
    • expanded Test 2
      • test.log
      • screenshot.png

Then all your helper function needs to do is take the test name and/or suite as parameters and return an array of files for that particular test case.

const tesults = require('tesults');
const fs = require('fs');
const temp = '/files-temp-dir';

function filesForTestSync (suite, testCase) {
  let filePaths = [];
  let dir = temp + "/" + suite + "/" + testCase;
  let files = [];
  try {
    files = fs.readdirSync(dir);
  } catch (error) {
     // dir may not exist
  }
  files.forEach(function (file) {
    if (file !== ".DS_Store") { // Exclude os files
      filePaths.push(dir + "/" + file);
    }
  });
  return filePaths;
}

//testCase.files = filesForTestSync(testCase.suite, testCase.name);

Caution: If uploading files the time taken to upload is entirely dependent on your network speed. Typical office upload speeds of 100 - 1000 Mbps should allow upload of even hundreds of files quite quickly, just a few seconds, but if you have slower access it may take hours. We recommend uploading a reasonable number of files for each test case. The upload method blocks while uploading test results and files. When starting out test without files first to ensure everything is setup correctly.

Consolidating multiple test results submissions into one test run

If you execute multiple test runs in parallel or serially for the same build or release and results are submitted to Tesults within each run, separately, you will find that multiple test runs are generated on Tesults. This is because the default behavior on Tesults is to treat each results submission as a separate test run. This behavior can be changed from the configuration menu. Click 'Results Consolidation By Build' from the Configure Project menu to enable and disable consolidation by target. Enabling consolidation will mean that multiple test runs submitted with the same build name will be consolidated into a single test run.

Dynamically created test cases should have static names

If you dynamically create test cases, such as test cases with variable values, we recommend that the test suite and test case names themselves be static. Provide the variable data information in the test case description or other custom fields but try to keep the test suite and test name static. This way you will you benefit from a range of features Tesults has to offer including test case failure assignment and historical results analysis. You need not make your tests any less dynamic, variable values can still be reported within test case details.

Proxy servers

Does your corporate/office network run behind a proxy server? Contact us and we will supply you with a custom API Library for this case. Without this results will fail to upload to Tesults.

Have questions or need help? Contact us