cypress-send-to-teams
v1.0.1
Published
A module to send cypress test results to a MS Teams webhook using Adaptive Cards.
Downloads
10
Maintainers
Readme
cypress-send-to-teams
A module to send Cypress test results to a Microsoft Teams webhook using Adaptive Cards.
Installation
You can install this module using npm:
npm install cypress-send-to-teams
Usage
Basic Usage
Import the module and call it with the required parameters:
const sendToTeams = require("cypress-send-to-teams");
// Example usage
sendToTeams(
"https://your-webhook-url",
"Test Results",
[
{
title: ["Test 1", "Sub-test 1"],
state: "failed",
displayError: "An error occurred",
},
// More test results...
],
{
name: "test-file.js",
}
);
Parameters
webhookUrl
(string): The Microsoft Teams webhook URL.title
(string): The title of the message to be sent.tests
(Array): An array of test result objects. Each object should have:title
(Array): An array with two strings, the main title and sub-title.state
(string): The state of the test ('passed'
or'failed'
).displayError
(string): The error message if the test failed.
spec
(object): An object representing the test file spec. It should have aname
property.isOnlySendingBugs
(boolean, optional): Whether to send only failed tests. Default istrue
.testItemTemplateHandler
(function, optional): A function to customize the test item format. It defaults todefaultCreateTestItem
.
Example
// cypress.config.ts
const { defineConfig } = require("cypress");
const sendToTeams = require('cypress-send-to-teams');
// Custom test item handler
const customTestItemHandler = (test, spec) => ({
type: "Container",
spacing: "Medium",
items: [
{
type: "TextBlock",
text: `Test: ${test.title[0]}`,
wrap: true,
size: "Medium",
weight: "Bolder",
},
{
type: "TextBlock",
text: `File: ${spec.name}`,
wrap: true,
size: "Medium",
weight: "Lighter",
},
{
type: "TextBlock",
text: `Status: ${test.state.toUpperCase()}`,
wrap: true,
size: "Medium",
color: "Attention",
weight: "Lighter",
},
{
type: "TextBlock",
text: test.displayError,
wrap: true,
size: "Medium",
color: "Attention",
weight: "Lighter",
},
],
});
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on("after:spec", async (spec, results) => {
if (
results &&
results.stats &&
results.stats.failures > 0
) {
console.log("spec", spec);
await sendToTeams(
'https://your-webhook-url',
'Custom Test Results',
[
{
title: ['Custom Test 1', 'Sub-test 1'],
state: 'failed',
displayError: 'Custom error message'
},
// More test results...
],
{
name: 'custom-test-file.js'
},
true, // only send bugs
customTestItemHandler // custom handler
);
.then((response) => {
console.log("Adaptive Card posted to Teams:", response.data);
})
.catch((error) => {
console.error("Error posting Adaptive Card to Teams:", error);
});
}
});
},
},
});
Dependencies
- axios: ^1.7.2
License
ISC License. See the LICENSE file for more details.
Author
John Patrick Valera
Feel free to adjust any part to better suit your needs or preferences!