npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slack-testing-library

v1.3.3

Published

A mock server and library for testing interactive Slack apps

Downloads

6

Readme

Slack Testing Library

A mock server and library for testing interactive Slack apps

Installation

# NPM
npm install --save-dev slack-testing-library

OR

# Yarn
yarn add --dev slack-testing-library

How it works

Slack Testing Library allows you to run integration tests against your Slack app's API server, replicating the behaviour of Slack's API to give you quicker tests.

It is designed to use simple methods that describe how real users will interact with your Slack app (e.g. openHome() or interactWith("button")), helping you to test the expected behaviour of your application, not the implementation details.

### Active screen

Slack Testing Library maintains an understanding of the currently active screen, just like a user in Slack would. By having an 'active screen', interaction and assertions can be made against that screen. Using methods like openHome() or openChannel() will change the active screen accordingly.

Getting started

  1. Set up your API server to route Slack API requests to the Slack Testing Library intercept server
import { WebClient } from "@slack/web-api";

return new WebClient(botToken, {
  slackApiUrl: "http://localhost:8123/slack/api",
});

Note: if you pass a custom port value to the SlackTestingLibrary constructor, change the port provided here in the slackApiUrl.

  1. Import the library and initialize an instance of Slack Testing Library at the top of your tests:

    import { SlackTestingLibrary } from "slack-testing-library";
    
    const sl = new SlackTestingLibrary({
      // This is your URL + path to your API server that handles Slack events
      baseUrl: "http://localhost:3000/api/event",
    });

    Note: you'll need to have started your application server before running your tests.

  2. Configure your test suite to start and stop the Slack Testing Library server:

    describe("Your test suite...", () => {
      beforeAll(async () => {
        // Starts the intercept server
        await sl.init();
      });
    
      afterAll(async () => {
        // Stops the intercept server
        await sl.teardown();
      });
    
      beforeEach(() => {
        // Resets the state of the intercept server
        sl.reset();
      });
    
      // ...
    });
  3. Test your Slack app

    it("should show a refresh button on the app home screen", async () => {
      await sl.openHome();
      await sl.interactWith("button", "Refresh");
    });

API

Setup and teardown

init(): Promise<void>

You will need to do this before all your tests start, to kick off the Slack Testing Library server. It's easiest to do this using the beforeAll hook.

teardown(): Promise<void>

You will need to do this after all your tests have run. It's easiest to do this using the afterAll hook.

Mocking or intercepting Slack API responses

Sometimes you'll want to provide custom responses from Slack API calls. To do so, you can use the intercept() method, which allows you to provide a custom response from Slack.

intercept(): void

In this example, we intercept the conversations.info API call and retrn a custom response, forcing an error. This allows us to then test the error handling behaviour of our application.

sl.intercept("conversations.info", () => ({
  ok: false,
  error: "channel_not_found",
  channel: null,
}));

Note: at the moment .intercept() intercepts all requests to the Slack API for that method (e.g. conversations.info). If you want to reset this, use sl.reset(). In the near future we expect to add support for single use intercepts.

Navigation

openHome(): Promise<void>

This triggers the app_home_opened event, and waits for a views.publish request from your application server. The active screen will be set to the App Home.

Note: This method requires an actor to be passed to the SlackTestingLibrary initializer, or via the actAs method before your test is run.

const sl = new SlackTestingLibrary({
  baseUrl: "https://localhost:3000/api/event",
  actor: {
    userId: "U12345678",
    teamId: "T12345678",
  },
});

OR

sl.actAs({
  userId: "U12345678",
  teamId: "T12345678",
});

openChannel(channelId: string): Promise<void>

This sets the active screen to be the channel with the given ID. See more about setting the active screen.

mentionApp({ channelId: string }): Promise<void>

This mentions the current app (bot ID can be configured in the SlackTestingLibrary initializer) in the given channel. This can be useful when combined with the openChannel() method, to open the channel and mention the app, and asserting on a response message.

Example usage:

sl.openChannel(channelId);

await sl.mentionApp({
  channelId,
});

await sl.getByText("Some text");

Finding elements and interacting with them

getByText(): Promise<void>

This allows you to find a specific string or piece of text within a the current active view (e.g. after your app has called views.publish for the app home).

// This would look for any text block in the active view containing the words "Hello, world!". This would fail if the text could not be found
await sl.getByText("Hello, world!");

Note: At the moment this is limited to "section" and "header" blocks, and can only look at the App Home view, or within standard messages in channels.

interactWith(): Promise<void>

This allows you to find an interactive element (e.g. buttons) and interact with it.

// This would click the button with the text "Refresh", and fail if the button could not be found
await sl.interactWith("button", "Refresh");

Fixtures

Slack Testing Library ships with some helper methods for generating common fixture data, useful for mocking out Slack responses.

SlackTestingLibrary.fixtures.buildChannel(overrides: Partial<SlackChannel>): SlackChannel

This builds a Slack Channel, useful for responding to conversations.info requests.

Example usage:

sl.intercept("conversations.info", () => ({
  channel: SlackTestingLibrary.fixtures.buildChannel({
    name: "my-custom-private-channel",
    is_private: true,
  }),
}));

SlackTestingLibrary.fixtures.buildTeam(overrides: Partial<SlackTeam>): SlackTeam

This builds a Slack Team or Workspace, useful for responding to team.info requests.