Skip to content

Commit 7ed6067

Browse files
committed
chore: add slack-mock-typed
1 parent b6c4798 commit 7ed6067

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1892
-0
lines changed

slack-mock-typed/.gitignore

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Dependency directories
27+
node_modules
28+
jspm_packages
29+
30+
# Optional npm cache directory
31+
.npm
32+
33+
34+
# Optional REPL history
35+
.node_repl_history
36+
37+
build
38+
yarn.lock

slack-mock-typed/.npmignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
coverage
2+
examples
3+
test
4+
.gitignore
5+
.travis.yml
6+
*.log
7+
yarn.lock
8+
src
9+
.nyc_output
10+
tslint.json
11+
tsconfig.json
12+
jest.config.js
13+
scripts
14+
renovate.json
15+
commitlint.config.js
16+
slack-mock-typed.tar.gz

slack-mock-typed/.travis.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
language: node_js
2+
node_js:
3+
- '12'
4+
- '8'
5+
- lts/*
6+
- node
7+
os:
8+
- linux
9+
matrix:
10+
fast_finish: true
11+
cache: yarn
12+
env:
13+
global:
14+
- secure: U2ybS/D9sR2enaT/u5jXBJHrXAInixz8q37scXJVsHGMyp+0eVH+WPLP1iFYfAfNQg9JUlZ5GGBqAmv8vROW1Mw/vlM5WRY3FsVYde/sqA2YqNfQmOhR6UIfuBDeJvVLlMKd+m2dC2dB5AyoxdO5PY58PmpMNORY4fL6P+jQfeFp9ZsIyDv+uMmDbv7jpamlWmbF2rRaoY1fD3uXncnEpgDkcULf5e4Wk29gffnLE/wShdEBEG3+JDiG4Mh6tqmwaaEiHExNLc8T1xMVIht/MoPl8DMG2ZoTzR04ztBlyU+0tIg5DJEsdrwp6WGjSbsAQiIE84+bz/Nr71M0draeWgnOpqBAMVbZFiBcXh6X/5HnTvSs8k+FTEirm9q6VHlHZtB/tBhJfGYioX6raMnSCUWWnOhwK0zG/YDDZIo70TNgQOc8JKMHA480kiNCUl4h7pSsed3entWltSl97VVc8qlu4NeEROE2MrH+LRXrRDeSWwj3Z84A8U/u71/eGigPj81AqLq/9mRoOdoOt/WQo3S4kvWhJaf9Rtn44U7iYC1Lh0AYVD7DANprAC9Z/58BkPhSw/yTqGwnklBCjq5Umw+C+lKPB9ouc5G2aE4n/EY2Vz9fM2ygzsY4wN+SqZ7FXc+VKucWiRF82FRChFkzhXPyTD5Z9/PRnr3hsycXwd0=
15+
install: yarn install --ignore-engines
16+
script: npm run-script ci
17+
before_deploy:
18+
- npm prune --production
19+
- tar -czvf slack-mock-typed.tar.gz package.json LICENSE README.md CHANGELOG.md index.js index.d.ts lib/* mocker/*
20+
- npm run deploy:prepare
21+
deploy:
22+
- provider: releases
23+
api_key:
24+
secure: MSdEBJeOPCmggAwInDaabk0gKLwyFKxE8DdQfoKwNRcxv74nhE6pR2gXr5lggiIB8mOjjXJuerU6QvC94BPwiDmqm9RkVefgStoEgwKFMblNMP+W6ZYBvSyPxz+1dBTVbEMTXPyOoAHgO+bhfIIvBh9YBGoYH3LVbzE1evgFH8aYv8113GWn1X6v+/lmxGRFCWnfxjSGIKvOMTWkYH+jDYhEnonmERWF7HiXUkDuShZhv/tKOen2rM+NAhc37MCmkbfB/aZWBGLUWb/Yn8V4W940/uCh3f2EUijgaPaWUWNPwDdy/xfUHPW/ys2FfwX9rMtq1Vpj967jPZVm5WP7V1VLqiV+djF/HHzPnntZ6ZidxOuaxI/XtmnrZH/Pd3RK/znAhMRY+ZhmW8hoPGwlYSqLFq6eN0i5ECB2MQy63XCkaI5FbwpTIOdOcaVlD9p5as3LJ4rHh65t+LoiyMtVzAgewI4GYMzFYtEWqA2IyPK8MfOsqtvIADBAM+AxNXrNqt/ONJry6vfJWF0HYjbuupVF5NS5918DVC4u1LqXZEw2sEeDc1LkfalN2Dwlgwaq8yKjvFNByl+s5Upugo2bUQNooRvqJ6kqCZe7r4SIl34evPOr5uTRNKR/9YukpzPV+kcqnmE+21rZEVdvq66v5KIXBzqTM/TBYPwapupk1uI=
25+
file: slack-mock-typed.tar.gz
26+
skip_cleanup: true
27+
on:
28+
tags: true
29+
repo: YOU54F/slack-mock-typed
30+
branch: master
31+
node: "8"
32+
- provider: script
33+
skip_cleanup: true
34+
script: ./scripts/publish.sh
35+
on:
36+
tags: true
37+
branch: master
38+
node: "8"

slack-mock-typed/CHANGELOG.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4+
5+
## [2.4.0](https://github.com/you54f/slack-mock-typed/compare/v2.2.4...v2.4.0) (2020-04-21)
6+
7+
8+
### Features
9+
10+
* add events and web api ([#230](https://github.com/you54f/slack-mock-typed/issues/230)) ([2eb2cea](https://github.com/you54f/slack-mock-typed/commit/2eb2cea58d1af74c39072284a324e4f9909d6b52))
11+
12+
## [2.3.0](https://github.com/you54f/slack-mock-typed/compare/v2.2.4...v2.3.0) (2020-04-21)
13+
14+
15+
### Features
16+
17+
* add events and web api ([f6cae46](https://github.com/you54f/slack-mock-typed/commit/f6cae46d855783ac0919eaba255d6a575cbe241e))
18+
19+
### [2.2.4](https://github.com/you54f/slack-mock-typed/compare/v2.2.3...v2.2.4) (2020-02-12)
20+
21+
22+
23+
### [2.2.3](https://github.com/you54f/slack-mock-typed/compare/v2.2.2...v2.2.3) (2020-02-12)
24+
25+
26+
27+
### [2.2.2](https://github.com/you54f/slack-mock-typed/compare/v2.2.1...v2.2.2) (2019-10-17)
28+
29+
30+
31+
### [2.2.1](https://github.com/you54f/slack-mock-typed/compare/v2.2.0...v2.2.1) (2019-08-07)
32+
33+
34+
35+
## [2.2.0](https://github.com/you54f/slack-mock-typed/compare/v1.1.1...v2.2.0) (2019-07-08)
36+
37+
38+
### Features
39+
40+
* add shutdown method to incoming webhooks ([bab4e30](https://github.com/you54f/slack-mock-typed/commit/bab4e30))
41+
* add shutdown method to incoming webhooks ([9a4b66b](https://github.com/you54f/slack-mock-typed/commit/9a4b66b))
42+
43+
44+
45+
### [2.1.2](https://github.com/you54f/slack-mock-typed/compare/v2.1.1...v2.1.2) (2019-06-03)
46+
47+
48+
49+
### [2.1.1](https://github.com/you54f/slack-mock-typed/compare/v2.1.0...v2.1.1) (2019-06-03)
50+
51+
52+
53+
## [2.1.0](https://github.com/you54f/slack-mock-typed/compare/v1.1.1...v2.1.0) (2019-06-03)
54+
55+
56+
### Build System
57+
58+
* automated release pipeline ([4851fa3](https://github.com/you54f/slack-mock-typed/commit/4851fa3))
59+
60+
61+
### Features
62+
63+
* add shutdown method to incoming webhooks ([bab4e30](https://github.com/you54f/slack-mock-typed/commit/bab4e30))
64+
* add shutdown method to incoming webhooks ([9a4b66b](https://github.com/you54f/slack-mock-typed/commit/9a4b66b))
65+
66+
67+
68+
### 1.2.7 (2019-05-03)
69+
70+
71+
72+
## [2.0.0](https://github.com/you54f/slack-mock-typed/)
73+
74+
- Rewrite of Webhook implementation in typescript.
75+
76+
77+
### Fixed
78+
79+
## [1.1.1](https://github.com/Skellington-Closet/slack-mock/compare/v1.1.0...v1.1.1)
80+
81+
- Closes the express server when the RTM websocket server is closed. Thanks for the PR [finferflu](https://github.com/finferflu)!
82+
83+
## [1.1.0](https://github.com/Skellington-Closet/slack-mock/compare/v1.0.2...v1.1.0)
84+
85+
### Changed
86+
87+
- Starts RTM server on a web API request to `rtm.connect`. Thanks for the PR [Lowry99](https://github.com/Lowry99)!
88+
89+
## [1.0.2](https://github.com/Skellington-Closet/slack-mock/compare/v1.0.1...v1.0.2)
90+
91+
### Changed
92+
93+
- Fixed wrong parameter name in README web API examples. Thanks for the PR [bmajz](https://github.com/bmajz)!
94+
95+
## [1.0.1](https://github.com/Skellington-Closet/slack-mock/compare/v1.0.0...v1.0.1)
96+
97+
### Added
98+
99+
- `params` property in the Events API recorded response. This was incorrectly stored as `body`.
100+
`body` will continue to work for the Events API calls, though will be deprecated and removed in a future major release.
101+
102+
### Changed
103+
104+
- Fixed incorrect docs for the Events API
105+
- Fixed incorrect docs and README example for the RTM API.
106+
107+
## [1.0.0](https://github.com/Skellington-Closet/slack-mock/tree/v1.0.0)
108+
109+
### Added
110+
111+
- Every initial feature for all your mocking needs!

slack-mock-typed/LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Yousaf Nabi
4+
Copyright (c) 2016 Cole Furfaro-Strode
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

slack-mock-typed/README.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# slack-mock
2+
3+
![npm version](https://img.shields.io/npm/v/slack-mock-typed.svg)
4+
![npm](https://img.shields.io/npm/dm/slack-mock-typed.svg)
5+
[![Build Status](https://travis-ci.org/YOU54F/slack-mock-typed.svg?branch=master)](https://travis-ci.org/YOU54F/slack-mock-typed)
6+
[![Dependency Status](https://img.shields.io/david/you54f/slack-mock-typed.svg?style=flat-square)](https://david-dm.org/you54f/slack-mock-typed)
7+
[![devDependency Status](https://img.shields.io/david/dev/you54f/slack-mock-typed.svg?style=flat-square)](https://david-dm.org/you54f/slack-mock-typed#info=devDependencies)
8+
[![Coverage Status](https://coveralls.io/repos/github/YOU54F/slack-mock-typed/badge.svg?branch=master)](https://coveralls.io/github/YOU54F/slack-mock-typed?branch=master)
9+
10+
11+
12+
A Slack API mocker for all your Slack bot and Slack app integration tests.
13+
14+
## Mock All Slack APIs
15+
16+
Slack Mock will mock a way of pushing data into Slack. You can use it to mock calls to
17+
- [Incoming Webhooks](https://api.slack.com/incoming-webhooks)
18+
19+
You can use your API calls as is without changing any URLs or tokens. Slack Mock will capture all outbound HTTP requests to `https://slack.com` and `https://hooks.slack.com`, so Slack will never receive your API calls.
20+
21+
With Slack-Mock you can inspect all outbound requests and trigger incoming requests to make sure your bot is doing the right thing.
22+
23+
## No Magic Included
24+
25+
OK, there's a little magic included in capturing HTTP requests, but that's it. No timeouts, magic promises, or events. Integration tests are hard, trying to make them easy with "convenience" abstractions that are out of your control only makes them harder.
26+
27+
Integration test by their nature are testing a closed system: you are inspecting from the outside a complex flow between at least two entities (your bot and the Slack API) and there is no guaranteed way to know when that flow is complete by observing from the outside. Any attempt to guess when the communication is complete will be wrong some of the time and just cause you frustration.
28+
29+
That's why Slack Mock provides simple, synchronous methods to queue, trigger, and inspect messages to and from Slack. No magic included.
30+
31+
To write a Slack Mock integration test queue up responses from Slack to your bot, then use Slack Mock to send a message from Slack to your bot to trigger a bot action, wait some time, then assert that your bot made the correct calls to Slack in order. How long do you wait? It depends on what your bot is doing. Play around a little and see what works. I find a 50 millisecond wait is more than enough for most flows.
32+
33+
## Usage
34+
35+
### Incoming Webhooks
36+
37+
```ts
38+
// incoming webhooks
39+
40+
import * as SlackMock from "slack-mock-typed";
41+
const mock: SlackMock.Instance = SlackMock.SlackMocker({ logLevel: "debug" });
42+
43+
function setup() {
44+
beforeAll(async () => {
45+
jest.setTimeout(60000);
46+
await mock.incomingWebhooks.start();
47+
await mock.incomingWebhooks.reset();
48+
});
49+
50+
beforeEach(async () => {
51+
jest.resetModules();
52+
await mock.incomingWebhooks.reset();
53+
expect(mock.incomingWebhooks.calls).toHaveLength(0);
54+
});
55+
afterEach(async () => {
56+
await mock.incomingWebhooks.reset();
57+
});
58+
}
59+
60+
function returnMockedSlackWebhookCall() {
61+
expect(mock.incomingWebhooks.calls).toHaveLength(1);
62+
const firstCall = mock.incomingWebhooks.calls[0];
63+
expect(firstCall.url).toEqual(process.env.SLACK_WEBHOOK_URL);
64+
const body = firstCall.params;
65+
return body;
66+
}
67+
```
68+
69+
## API Conventions
70+
71+
Slack Mock will intercept all requests to `https://hooks.slack.com`. There's no need to change any URLs in your bot.
72+
73+
Here are the method conventions. Not every API wrapper supports each of these methods, see the [API docs](#api) below:
74+
- `addResponse` will add the next response returned. You can call mutlitple times to queue responses. If you set a `url` option, then the response will only be returned for that url. URL specific responses take precedence over unspecified responses
75+
- `calls` will be in order received and always contain params, headers, and url. Params contain both query params and body properties.
76+
- `reset` will always clear calls and any queued responses you have.
77+
78+
There is also a top level `reset` convenience method that will call reset on each API wrapper.
79+
80+
Slack mock will respond to all requests with a 200 OK unless a custom response has been queued. For web requests, a the default body will be `{ok: true}`.
81+
82+
## API
83+
84+
### `require('slack-mock')`: `function(config)`
85+
86+
The exported function used to start the Slack Mock server. Returns an instance of the server.
87+
88+
Slack Mock is a singleton so can only be configured once per process. Subsequent calls to SlackMocker() will return
89+
the same instance.
90+
91+
Config options are:
92+
- `logLevel` (String, optional) The log level to use. One of `error`, `warn`, `info`, `verbose`, `debug`, or `silly`. Defaults to `info`.
93+
94+
---
95+
96+
### `instance`
97+
98+
The configured instance of the Slack Mock `SlackMocker.instance` object. This is the same object returned from `require('slack-mock')(config)`.
99+
100+
---
101+
102+
### `instance.incomingWebhooks` (Incoming Webhooks)
103+
104+
The `incomingWebhooks` object mocks receiving payloads from you Slack App to all Incoming Webhooks at `https://hooks.slack.com/`.
105+
106+
- `addResponse`: `function(opts)` Queues a response payload that Slack Mock will use to respond upon
107+
receiving a post to a registered endpoint. This method can be called multiple times. Responses
108+
will be used in a FIFO order. Options are:
109+
- `url` (String, optional) The Incoming Webhook URL your app will be POSTing to.
110+
- `statusCode` (Number, optional) The HTTP status code to reply with. Defaults to 200.
111+
- `body` (Object, optional) The response body to reply with. Defaults to `OK`.
112+
- `headers` (Object, optional) The HTTP headers to reply with. Defaults to `{}`.
113+
114+
- `reset`: `function()` Empties the `incomingWebhooks.calls` array and clears any queued responses.
115+
116+
- `calls`: `Array` An array of payloads received your from Slack app to an Incoming Webhook url.
117+
- `url` The url of the call that was intercepted.
118+
- `params` The POST body merged with any query string parameters captured from the intercepted request as an Object.
119+
- `headers` The headers of the intercepted request as an Object.
120+
121+
122+
### `instance.reset`: `function()`
123+
124+
Resets all mocks. A convenience method for calling reset on individual API mocks.

slack-mock-typed/commitlint.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional']
3+
};

slack-mock-typed/index.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Events } from "./mocker/events";
2+
import { IncomingWebhooks } from "./mocker/incoming-webhooks";
3+
import { Web } from "./mocker/web";
4+
export declare function SlackMocker(config?: ConfigOptions): Instance;
5+
export interface Instance {
6+
incomingWebhooks: IncomingWebhooks<any>;
7+
events: Events<any>;
8+
web: Web<any>;
9+
reset: () => void;
10+
}
11+
export interface ConfigOptions {
12+
logLevel?: string;
13+
}

0 commit comments

Comments
 (0)