ReportPortal JavaScript Client
reportportal-js-client is a dedicated Node.js client library for integrating test automation results with the ReportPortal platform. It enables the creation of custom listeners and reporters to programmatically interact with a ReportPortal instance, facilitating the reporting of test launches, suites, and individual test cases. The current stable version is 2.2.1, with major version 2.x focusing exclusively on compatibility with Report Portal 5 and later, having dropped support for Report Portal 4 in version 2.0.0. This client is a fork of the official `client-javascript` and is specifically tailored for Node.js environments, providing methods to start and finish launches, test items, and logs. It emphasizes an asynchronous reporting model, allowing for efficient data transmission without blocking the main execution thread, and offers both synchronous and asynchronous modes for endpoint interaction. Its key differentiator is its robust API for deep integration within Node.js test frameworks for comprehensive test result visualization and analysis within ReportPortal.
Common errors
-
Error connection to server
cause Incorrect `endpoint` URL, invalid `token`, or network connectivity issues to the ReportPortal server.fixVerify the `endpoint` URL format (e.g., `/api/v1`), confirm the `token` is correct, and ensure the ReportPortal server is running and accessible from the client's environment. -
TypeError: RPClient is not a constructor
cause Attempting to use `import { RPClient } from 'reportportal-js-client';` in an ES Module context. The library exports `RPClient` as a default export.fixUse the correct ES Module import syntax: `import RPClient from 'reportportal-js-client';`. -
Error: Not all parameters are specified.
cause Missing one or more required configuration parameters (`token`, `endpoint`, `launch`, `project`) when instantiating `RPClient`.fixEnsure all required parameters (`token`, `endpoint`, `launch`, `project`) are provided as properties in the configuration object when creating a new `RPClient` instance.
Warnings
- breaking Version 2.0.0 and subsequent versions dropped support for Report Portal 4. The client now exclusively supports Report Portal 5 and newer versions.
- gotcha The `endpoint` configuration parameter must specifically point to the ReportPortal API base URL, typically ending with `/api/v1` (or `/api/v2` for asynchronous reporting). Providing the UI URL (e.g., `http://server:8080/ui`) will lead to connection errors.
- gotcha For asynchronous reporting, the endpoint URL must be modified to use `/api/v2` instead of `/api/v1`. This is a specific behavior to enable a different reporting mode.
- gotcha The `token` parameter is mandatory for authentication and must be a valid user token obtained from the user's profile page within the ReportPortal UI. Incorrect or missing tokens will result in authentication failures.
- gotcha While the client API calls (e.g., `startLaunch`, `startTestItem`) are synchronous in the sense that they return immediately, the actual network requests are asynchronous and return Promises. It's crucial to handle these promises for operations like `checkConnect` or for ensuring data is sent before application exit.
Install
-
npm install reportportal-js-client -
yarn add reportportal-js-client -
pnpm add reportportal-js-client
Imports
- RPClient
const RPClient = require('reportportal-js-client'); - RPClient
import { RPClient } from 'reportportal-js-client';import RPClient from 'reportportal-js-client';
- rpClient.helpers.now
const startTime = rpClient.helpers.now();
Quickstart
import RPClient from 'reportportal-js-client';
const rpClient = new RPClient({
token: process.env.REPORTPORTAL_TOKEN ?? 'YOUR_REPORTPORTAL_TOKEN',
endpoint: process.env.REPORTPORTAL_ENDPOINT ?? 'http://your-instance.com:8080/api/v1',
launch: process.env.REPORTPORTAL_LAUNCH_NAME ?? 'MyTestLaunch',
project: process.env.REPORTPORTAL_PROJECT_NAME ?? 'MyProject'
});
rpClient.checkConnect().then((response) => {
console.log('Successfully connected to ReportPortal server.');
console.log(`Using account: ${response.fullName}`);
const launchObj = rpClient.startLaunch({
name: 'Client Test Launch',
startTime: rpClient.helpers.now(),
description: 'Example launch from quickstart',
attributes: [{ key: 'env', value: 'dev' }]
});
console.log(`Launch started with tempId: ${launchObj.tempId}`);
// You would typically add test items here and finish the launch later.
// For this quickstart, we'll just finish it immediately for demonstration.
rpClient.finishLaunch(launchObj.tempId, { endTime: rpClient.helpers.now() });
console.log(`Launch ${launchObj.tempId} finished.`);
}, (error) => {
console.error('Error connecting to ReportPortal server:');
console.error(error);
});