Sending Allure Test Results to Slack Using ES Modules in Node.js with Gitlab CI

Pradap Pandiyan
5 min readSep 15, 2024

--

In modern CI/CD workflows, keeping the team informed about test results is crucial for maintaining high code quality. One common practice is sending test result summaries to communication platforms like Slack. If you’re using Allure to generate test reports, you may want to parse the results and send them directly to Slack after your CI pipeline completes.

This article will walk you through how to parse Allure results from a JSON file using ES modules in Node.js and send a Slack notification with the summarized test data.

Prerequisites

Before diving into the code, ensure you have the following:

  • Allure is set up in your CI pipeline to generate reports in JSON format.
  • Slack Incoming Webhook URL to send notifications.
  • Node.js (v12+ preferred) with ES module support.
  • Project with a pipeline running for a test with allure report.

Why ES Modules?

By default, Node.js has used the CommonJS module system, which relies on the require() function to load dependencies. However, with the introduction of ES modules (ECMAScript modules), Node.js now supports the import syntax that is familiar from modern JavaScript.

Using ES modules allows you to write cleaner, more modern JavaScript code, and aligns with current best practices for modularity.

Problem Scenario

Let’s say you have Allure test results saved in a JSON file (allure-report/widgets/summary.json) in the following format:

{
"reportName": "Allure Report",
"testRuns": [],
"statistic": {
"failed": 1,
"broken": 0,
"skipped": 0,
"passed": 5,
"unknown": 0,
"total": 6
},
"time": {
"start": 1726345166260,
"stop": 1726345168100,
"duration": 1840,
"minDuration": 1,
"maxDuration": 766,
"sumDuration": 1832
}
}

We want to:

  1. Parse this JSON file.
  2. Summarize the test results (e.g., total tests, passed, failed, and test duration).
  3. Send a notification to a Slack channel with this summary.

Step-by-Step Solution

Step 1: Parse Allure JSON Results in Node.js

First, let’s create a Node.js script to parse the JSON file and extract relevant test data. Since we’re using ES modules, we will use the import statement instead of require.

Script: send_slack_notification.mjs

import fs from 'fs';
import axios from 'axios';
const reportData = JSON.parse(fs.readFileSync('allure-results/report.json', 'utf-8'));
const {
reportName,
statistic: { failed, broken, skipped, passed, total },
time: { duration }
} = reportData;
const formatTime = (ms) => {
const minutes = Math.floor(ms / 60000);
const seconds = ((ms % 60000) / 1000).toFixed(0);
return `${minutes}:${seconds < 10 ? '0' : ''}${seconds} min`;
};
const slackMessage = {
text: `*${reportName} Summary*\n
*Total Tests*: ${total}
*Passed*: ${passed}
*Failed*: ${failed}
*Broken*: ${broken}
*Skipped*: ${skipped}
*Duration*: ${formatTime(duration)}`
};
const sendSlackNotification = async () => {
const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
try {
await axios.post(slackWebhookUrl, slackMessage);
console.log('Slack notification sent successfully!');
} catch (error) {
console.error('Error sending Slack notification:', error);
}
};
sendSlackNotification();

Explanation:

  • ES Module Imports: We import fs to read files and axios to send HTTP POST requests.
  • Parsing JSON: The fs.readFileSync function reads the report.json file, and JSON.parse() converts the string into a JavaScript object.
  • Extracting Test Data: We destructure the JSON object to get statistics like total, passed, failed, and duration.
  • Formatting Time: The formatTime() function converts the duration (in milliseconds) to a minutes:seconds format.
  • Slack Message: We create a Slack message using the parsed data and send it using axios.post() to a Slack Webhook URL.

We need to generate a webhook for a particular channel for slack notifications. In the line of code we need to replace it with the slack Webhook URL process.env.SLACK_WEBHOOK_URL

Here is the documentation from slack on how to generate the webhook and test it. https://api.slack.com/messaging/webhooks

Step 2: Configure ES Module Support

In Node.js, files with a .js extension are treated as CommonJS modules by default. To use ES modules, you have two options:

  1. Rename the file extension to .mjs. This tells Node.js to treat the file as an ES module.
  2. Set "type": "module" in package.json. If you want to keep the .js extension, you can add the following line to your package.json:
{   "type": "module" }

Step 3: Set Up Slack Incoming Webhook

To send a message to Slack, you need an Incoming Webhook URL. Follow these steps:

  1. Go to your Slack workspace and create an Incoming Webhook from the Slack API.
  2. Once created, Slack will provide you with a Webhook URL. You can store this URL as an environment variable (SLACK_WEBHOOK_URL) in your CI pipeline or local environment.

Step 4: Running the Script in a CI Pipeline

You can now integrate this script into your GitLab CI or GitHub Actions pipeline. Here’s a basic configuration for GitLab CI:

send-slack-notification:
stage: notify
image: node:18
script:
- npm install axios
- node send_slack_notification.mjs
dependencies:
- generate-allure-result
only:
- main

This is how my full pipeline looks like for running a test, generating an allure report, and sending Slack notifications. The automation test is build in with Webdriver.io.

stages:
- install-and-test
- generate-allure-report
- send-slack-notification

install_dependencies:
stage: install-and-test
image: node:20
artifacts:
paths:
- allure-results
when: always
expire_in: 1 days
script:
- apt-get update
- apt-get install -y --no-install-recommends wget gnupg2
- wget -qO - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
- sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
- apt-get update
- apt-get install -y google-chrome-stable libnss3 libatk-bridge2.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libxrandr2 libgbm1 libasound2
- npm install
- npm run wdio

generate-allure-result:
stage: generate-allure-report
image: pradapjackie/allure-reports-generation:1.0
artifacts:
paths:
- allure-report
- allure-results
when: always
expire_in: 1 days
script:
- allure generate allure-results --clean -o allure-report
when: always
allow_failure: true

send-slack-notification:
stage: send-slack-notification
image: node:18
script:
- npm install axios
- node send_slack_notification.js
dependencies:
- generate-allure-result

Step 5: Output in Slack

Once the script runs successfully, it will post a message to your Slack channel with a summary of the test results, formatted as follows:

*Allure Report Summary*
Total Tests: 6
Passed: 5
Failed: 1
Broken: 0
Skipped: 0
Duration: 0:01 min

This is how the real-time notification looks like and it's very effective. We can build colorful notifications if we want and it's all customizable.

Conclusion

By using Node.js ES modules, you can easily parse Allure test results and send a formatted summary to Slack. This approach ensures that your team stays updated with the latest test results directly in their workspace, helping maintain high quality in the development pipeline.

With the flexibility of Node.js, this solution can be easily extended to include more detailed information, custom reports, or even charts in Slack messages. Happy coding!

Feel free to hit clap if you like the content. Happy Automation Testing :) Cheers. 👏

--

--

Pradap Pandiyan

I’m a passionate QA Engineer. I’m a motovlogger, content creator, off-roader and freelancer. Buy me a coffee here https://www.buymeacoffee.com/pradappandiyan