Gathering detailed insights and metrics for cypress-slack-notify
Gathering detailed insights and metrics for cypress-slack-notify
Gathering detailed insights and metrics for cypress-slack-notify
Gathering detailed insights and metrics for cypress-slack-notify
Post messages in Slack channels when specific Cypress tests and specs fail
npm install cypress-slack-notify
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
6 Stars
107 Commits
4 Forks
2 Watchers
22 Branches
1 Contributors
Updated on Dec 09, 2024
Latest Version
1.14.10
Package Id
cypress-slack-notify@1.14.10
Unpacked Size
35.70 kB
Size
10.16 kB
File Count
9
NPM Version
8.19.2
Node Version
20.18.0
Published on
Dec 09, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
6
Post messages in Slack channels when specific Cypress tests and specs fail
Add this plugin as a dev dependency
1$ npm i -D cypress-slack-notify 2# or add using Yarn 3$ yarn add -D cypress-slack-notify
Register this plugin from your cypress.config.js
file (or from your plugins file is using Cypress v9)
1// cypress.config.js 2const { defineConfig } = require('cypress') 3 4// describe the notification of each failed spec 5// you want to notify about 6const notificationConfiguration = { 7 // if this spec fails, post a message to the channel "e2e-tests" 8 'spec-a.cy.js': '#e2e-tests', 9 // if this spec fails, post a message and notify Gleb 10 'spec-b.cy.js': '#e2e-tests @gleb', 11 // if this spec fails, notify several users 12 'spec-c.cy.js': '#e2e-tests @gleb @john @mary', 13} 14 15// describe when to notify. We probably want to notify 16// only when running on CI and recording the test runs 17// and using certain run tags 18const notifyWhen = { 19 whenRecordingOnDashboard: true, 20 whenRecordingDashboardTag: ['notify'], 21} 22 23module.exports = defineConfig({ 24 projectId: '...', 25 e2e: { 26 setupNodeEvents(on, config) { 27 // https://github.com/bahmutov/cypress-slack-notify 28 require('cypress-slack-notify')(on, notificationConfiguration, notifyWhen) 29 }, 30 }, 31})
To use this plugin, you will need to get yourself a SLACK_TOKEN
by making a new Slack App. This app needs a bot token with the scope "chat:write" to post messages using chat.postMessage
method. If you want to tag specific users in the messages, you will need to give the app the permission scope "users:read" too; it allows the plugin to call users.list
method to include the user ID in the messages. You will need to invite the registered and installed Slack App to each channel you would like to post messages by this plugin.
You can confirm the Slack application integration by calling:
$ SLACK_TOKEN=... npx cypress-slack-notify --test-channel #my-channel-name
If the application has been installed correctly, the message will be posted.
You can provide your own synchronous predicate function to decide if this plugin should send Slack notifications on failed specs.
1const notifyWhen = { 2 whenISaySo({ runDashboardUrl, runDashboardTags }) { 3 // look at the provided arguments, or any logic like process.env.CI 4 // etc to determine if you want to send Slack notifications 5 return true | false 6 }, 7} 8 9// https://github.com/bahmutov/cypress-slack-notify 10require('cypress-slack-notify')(on, notificationConfiguration, notifyWhen)
You can list spec files by the filename / end of the filepath. You can also rely on minimatch to find the target Slack channel.
1const notificationConfiguration = { 2 // equivalents 3 'spec-a.cy.js': '#one', 4 'e2e/spec-a.cy.js': '#one', 5 'cypress/e2e/spec-a.cy.js': '#one', 6 // use minimatch with spec paths 7 // https://github.com/isaacs/minimatch 8 // In this case, any failed specs directly in the "sub" folder 9 // will post notification to '#cypress-slack-notify-minimatch' 10 // https://github.com/isaacs/minimatch 11 '**/sub/*.cy.js': '#two', 12}
In the above situation, any failed test in the spec like cypress/e2e/sub/home.cy.js
will be posted to #two
.
You can post a message about every failed spec into a single channel by using a config shortcut
1// cypress.config.js 2// https://github.com/bahmutov/cypress-slack-notify 3const registerSlackNotify = require('cypress-slack-notify') 4... 5setupNodeEvents(on, config) { 6 // any recorded run tagged "sanity" should notify #sanity-tests channel 7 // on each failed spec 8 registerSlackNotify(on, '#sanity-tests', { 9 whenRecordingDashboardTag: ['sanity'], 10 }) 11})
If you use cypress-grep plugin to tag suites or individual tests, you can direct the messages based on the effective test tags.
1describe('Login tests', { tags: ['@auth'] }, () => { 2 // a failing spec with effective tag "@auth 3 it('fails', () => ... ) 4})
1// cypress.config.js
2// https://github.com/bahmutov/cypress-slack-notify
3const registerSlackNotify = require('cypress-slack-notify')
4
5setupNodeEvents(on, config) {
6 // any recorded run tagged "sanity" should notify #sanity-tests channel
7 // on each failed spec
8 registerSlackNotify(on,
9 {
10 testTags: {
11 // list each tag and the Slack target
12 '@auth': '#cypress-slack-notify-effective-tags @gleb',
13 },
14 // only send notifications when recording
15 // on Cypress Dashboard with the tag "user"
16 {
17 whenRecordingDashboardTag: ['user'],
18 }
19 }
20 )
21}
See cypress.effective.config.js
Tip: you can define the test tags to Slack targets in a JSON file and load it using a require
command. For example, if the JSON file below is used notify.json
we can do:
1{ 2 "@auth": "#auth-tests @gleb", 3 "@sell": "#sell-tests @gleb" 4}
1registerSlackNotify( 2 on, 3 { 4 testTags: require('./notify.json'), 5 }, 6 // only send notifications when recording 7 // on Cypress Dashboard with the tag "nightly" 8 { 9 whenRecordingDashboardTag: ['nightly'], 10 }, 11)
You can register this plugin multiple times and direct messages based on the recorded Dashboard tags.
1// cypress.config.js 2// https://github.com/bahmutov/cypress-slack-notify 3const registerSlackNotify = require('cypress-slack-notify') 4... 5setupNodeEvents(on, config) { 6 // any recorded run tagged "sanity" should notify #sanity-tests channel 7 // on each failed spec 8 registerSlackNotify(on, '#sanity-tests', { 9 whenRecordingDashboardTag: ['sanity'], 10 }) 11 // any recorded run tagged "user" should notify #user-tests channel 12 // on each failed spec 13 registerSlackNotify(on, '#user-tests', { 14 whenRecordingDashboardTag: ['user'], 15 }) 16})
To notify users in the message, this plugin needs to find Slack user ID from the username. You can see the found user by running the cypress-slack-notify
bin alias
1$ npx cypress-slack-notify --find-user @gleb 2found Slack user @gleb ID: U12345678 3# use Yarn to call the "bin/cypress-slack-notify.js" script 4# You can also skip the "@" at the start of the username 5$ yarn cypress-slack-notify --find-user gleb
When searching the users, we consider both the property "name" and "profile.display_name" fields.
You can pass multiple usernames separating them with a comma
1$ npx cypress-slack-notify --find-user @gleb,slackbot 2found Slack user @gleb ID: U12345678 3...
If you pass Slack user IDs (they start with U
), they will simply be returned
1$ npx cypress-slack-notify --find-user @U12345678 2already is a Slack user ID U12345678 3...
1$ npx cypress-slack-notify --find-user-by-slack-id U12345678
Prints the user's display and real name if found.
Enable verbose log messages by setting an environment variable DEBUG=cypress-slack-notify
See bahmutov/cypress-slack-example
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2022
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
No vulnerabilities found.
No security vulnerabilities found.