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-wrench/blocks-to-image

v2.0.2

Published

Render Slack Block Kit json as images

Downloads

12

Readme

Blocks To Images

Render Slack Block Kit json as images.

As a side effect, you are also validating the correctness of the JSON you're using, since it won't render if given invalid JSON.

This pairs amazingly with jest-image-snapshot for image-based snapshot testing. No more staring at long json files for hours!

Generated Failed Diff Example

Block Kit Builder is authenticated, if you use this package and want to help make this package easier to use, Send Slack a Message asking them to prioritize them making it unauthenticated.

Table of Contents

Install

yarn add --dev @slack-wrench/blocks-to-image
# or
npm install --save-dev @slack-wrench/blocks-to-image

Usage

import BlockKitRenderer from '@slack-wrench/blocks-to-image';
import { toMatchImageSnapshot } from 'jest-image-snapshot';

// Import how you generate Slack Block kit JSON.
import MySlackBlock from './MySlackBlock';

expect.extend({ toMatchImageSnapshot });

const imageCompareOptions = { customDiffConfig: { threshold: 0.1 } };

describe('My Awesome App', () => {
  // We use the block kit builder, it can take a while
  jest.setTimeout(30000);
  let blockKitRenderer: BlockKitRenderer;

  beforeAll(async () => {
    blockKitRenderer = new BlockKitRenderer();

    // Log into a Slack Workspace that's installed block kit builder
    await blockKitRenderer.login(
      process.env.SLACK_DOMAIN || '', // eg: `community` (emit the `.slack.com)
      process.env.SLACK_EMAIL || '',
      process.env.SLACK_PASSWORD || '',
    );
  });

  afterAll(async () => {
    // Cleanup resources
    await blockKitRenderer.close();
  });

  it('Looks beautiful!', async () => {
    const blockImage = await blockKitRenderer.imageFromBlocks(MySlackBlock());

    expect(blockImage).toMatchImageSnapshot(imageCompareOptions);
  });
});

The function toMatchImageSnapshot takes in an optional configuration object customDiffConfig that customizes the snapshot comparison. To avoid test failures due to small differences in the image snapshot comparison, increase the threshold value that is used by jest-image-snapshot. With the default threshold value of 0.01, minor color differences in screenshots would trigger a test failure. For additional information about the configuration, visit the API documentation of jest-image-snapshot.

login

blockKitRenderer.login(
    domain: string, // The domain to your slack workspace eg: `community` (emit the `.slack.com)
    email: string, // The email you use to log into the workspace above
    password: string, // The password you use for the account
) => Promise<Screenshot>

This package uses Slack's Block Kit Builder to render images. You need to log into a workspace that has Block Kit Builder installed.

imageFromBlocks

blockKitRenderer.imageFromBlocks(
    blocks: KnownBlock[], // Blocks that you want to render
    options: ScreenshotOptions = {}, // Screenshot Options
) => Promise<Screenshot>

For details on additional options, see Puppeteer docs

imageFromView

blockKitRenderer.imageFromView(
    view: View, // Modal or App Home that you want to render
    options: ScreenshotOptions = {}, // Screenshot Options
) => Promise<Screenshot>

For details on additional options, see Puppeteer docs

imageFromAttachments

blockKitRenderer.imageFromAttachments(
    attachments: MessageAttachment[], // Attachments that you want to render
    options: ScreenshotOptions = {}, // Screenshot Options
) => Promise<Screenshot>

For details on additional options, see Puppeteer docs

Advanced Configuration

blocks-to-image uses Puppeteer to generate images. If you need to pass additional configuration to Puppeteer, like maybe a custom chrome executable, you can do so in the constructor:

blockKitRenderer = new BlockKitRenderer({
  puppeteer: {
    product: 'firefox',
    headless: false,
    executablePath: '/path/to/exe',
  },
});

Use an existing browser

If you're looking to connect to an existing browser instance, (for example, to speed up jest), you can do so with connect, passing additional configuration.

blockKitRenderer.connect(options: ConnectOptions) => Promise<void>

You can do this before or after logging in depending on your use case.

jest globalSetup

To save some time if you're running this in multiple test files with jest, you can with some additional setup use jest to login only once at the beginning of testing.

Follow [the jest instructions for custom puppeteer hookup], providing blockKitRenderer.browser as the browser in the setup.js and doing the login as part of the setup in the puppeteer_environment.js file. This can speed things up pretty dramatically.