ci-slack-reporter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +70 -0
  2. package/package.json +22 -0
package/index.js ADDED
@@ -0,0 +1,70 @@
1
+ // my-reporter.js
2
+
3
+ 'use strict';
4
+
5
+ const Mocha = require('mocha');
6
+ const { IncomingWebhook } = require('@slack/webhook');
7
+
8
+ const {
9
+ EVENT_RUN_BEGIN,
10
+ EVENT_RUN_END,
11
+ EVENT_TEST_FAIL,
12
+ EVENT_TEST_PASS,
13
+ EVENT_SUITE_BEGIN,
14
+ EVENT_SUITE_END
15
+ } = Mocha.Runner.constants;
16
+
17
+ if (!process.env.CI_SLACK_REPORTER_WEBHOOK) {
18
+ console.error('CI_SLACK_REPORTER_WEBHOOK env variable is not set, reported will not work, please do export CI_SLACK_REPORTER_WEBHOOK=...')
19
+ }
20
+
21
+ const webhook = new IncomingWebhook(process.env.CI_SLACK_REPORTER_WEBHOOK);
22
+
23
+ let out;
24
+
25
+ // this reporter outputs test results, indenting two spaces per suite
26
+ class MyReporter {
27
+ constructor(runner) {
28
+ this._indents = 0;
29
+ const stats = runner.stats;
30
+
31
+ runner
32
+ .once(EVENT_RUN_BEGIN, () => {
33
+ out = '';
34
+ })
35
+ .on(EVENT_SUITE_BEGIN, () => {
36
+ this.increaseIndent();
37
+ })
38
+ .on(EVENT_SUITE_END, () => {
39
+ this.decreaseIndent();
40
+ })
41
+ .on(EVENT_TEST_PASS, test => {
42
+ // Test#fullTitle() returns the suite name(s)
43
+ // prepended to the test title
44
+ out += `${this.indent()} ✅ ${test.fullTitle()}\n`;
45
+ })
46
+ .on(EVENT_TEST_FAIL, (test, err) => {
47
+ out += `${this.indent()} ⛔ ${test.fullTitle()} - error: ${err.message}\n`;
48
+ })
49
+ .once(EVENT_RUN_END, () => {
50
+ out += `Test finished. ${stats.passes}/${stats.passes + stats.failures} ok`;
51
+ webhook.send({
52
+ text: out,
53
+ })
54
+ });
55
+ }
56
+
57
+ indent() {
58
+ return Array(this._indents).join(' ');
59
+ }
60
+
61
+ increaseIndent() {
62
+ this._indents++;
63
+ }
64
+
65
+ decreaseIndent() {
66
+ this._indents--;
67
+ }
68
+ }
69
+
70
+ module.exports = MyReporter;
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "ci-slack-reporter",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/devforth/ci-slack-reporter.git"
12
+ },
13
+ "author": "",
14
+ "license": "ISC",
15
+ "bugs": {
16
+ "url": "https://github.com/devforth/ci-slack-reporter/issues"
17
+ },
18
+ "homepage": "https://github.com/devforth/ci-slack-reporter#readme",
19
+ "dependencies": {
20
+ "@slack/webhook": "^6.1.0"
21
+ }
22
+ }