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

sketch-test-inspector

v0.0.2

Published

Helper utils and plugin for running unit tests on sketch plugins

Downloads

3

Readme

sketch-test-inspector

🕵️‍♀️ Helper utils and plugin for running unit tests on sketch plugins.

Why?

The Sketch plugin environment is great, and cocoascript giving you access to (almost) all Objective C classes makes it super powerful as well.

Unfortunately it is not reliable. When using Sketch core classes (because, lets be honest, the JS API is not far enough yet to completely replace those) you always risk running into issues when new versions introduce breaking changes. Also, for macOS and the usage of NS* classes it comes down to pretty much the same issue on every system update. That leads to sweat and long nights of testing and debugging on every new OS or Sketch version.

Sketch plugins are very hard to write unit tests for. This helper library (+ plugin) tries to solve that problem by providing a bunch of helpers that let you write tests for your plugin commands more easily, using your favorite test library as you are used to.

Why can't I just use sketchtool?

Because sketchtool makes it easy to get information about a certain file, or to run a certain plugin command in sketch, but its quite hard to do both. This helper library is build on top of sketchtool (which comes with every Sketch version) to overcome these difficulties.

NOTE: Problems with async

I couldn't find a proper way yet to ensure a plugin command has fished, in order to use it as reference when to save the document and resolve the promise returned by the runPluginCommand and runScript methods. So, for now as a quick fix, I just use timeouts, which will work as long as you don't have any long running processes in your script.

I am currently working on an alternative handling and happy for any ideas 😊

Getting started

yarn add sketch-test-inspector --dev

# or
npm i sketch-test-instepctor --dev

How to use it

To use it, e.g. with jest, simply import the inspector, set your plugin that you want to test, open a file that you want to test on and shoot plugin actions at it ... that's it, simple 😊

const inspector = require('sketch-plugin-inspector');
const path = require('path');

// Set you plugin that you want to test
inspector.setPlugin('my-awesome-plugin');

// Have a test file somewhere, that you want to test your plugin on
const myTestFile = path.resolve(__dirname, 'Test.sketch');

describe('My awesome plugin', () => {
  // Test a specific command of your plugin
  describe('Test action', () => {
    it('Does something', done => {
      // Open the test file in sketch
      inspector.openFile(myTestFile);

      // Run a plugin command
      inspector.runPluginCommand('AwesomeCommand')
        .then(() => {
          // Check if everything is as you'd expect it
          // e.g. if the command was supposed to create 100 pages in the sketch file...
          expect(inspector.pages().length).toBe(100);
          done();
        })
        .catch(done.fail);
    });
  });

});

Example

# Clone repo
gitclone https://github.com/julianburr/sketch-test-inspector.git

# Change into repos example dir
cd sketch-test-inspector/example

# Install dependencies and run test
yarn
yarn test

This will run the jest tests that use sketch-test-inspector to run plugin commands on a prepared test file.

sketch-test-inspector

Methods

setPlugin(name)

Sets the plugin for your inspector.

Params:

  • name: The file name of your sketch plugin (with or without the .sketchplugin extension)

openFile(path)

Opens the specified file in sketch so you can run commands on it.

Params:

  • path Absolute sketch file path that you want to open

selectLayers(layers)

Changes the selection in the current document to specified layers.

Params:

  • layers Array of layers you want to select

saveDocument

Saves the document in its current state. The Inspector will always work with temporary copies of the original file that you specified in openFile, so you never actually overwrite it. You do want to save the temporary file if you made changes (programatically) and want to test the output (sketchtool can only read the file from the disc!). NOTE: runPluginCommand automatically saves the temp. file before resolving the promise!

closeDocument

Closes the current document. Mainly because right now the inspector physically opens Sketch. This function lets you close the opened document window.

runPluginCommand(identifier)

Runs specified command identifier on plugin that has been specified before via setPlugin.

Params:

  • identifier The plugin command identifier you want to run on the currently opened file.

runScript(fnc)

Runs specified custom script as plugin command. The function will receive the command context and will then be run within Sketch, giving it access the whole cocoascript environent of Sketch plugins.

Params:

  • fnc The function you want to run as plugin command

dump

sketchtool dump ${currentFilePath}

list(type)

sketchtool list ${type} ${currentFilePath}

Params:

  • type Sketchtool list type.

pages

sketchtool list pages ${currentFilePath}

Todos / Roadmap

  • [ ] Change to running sketchtool in the background rather than physically having to open the test file(s) in Sketch!
  • [ ] Add more utils, like Sketch action listeners, observer for logs and document messages, etc.
  • [ ] Optimise plugin command handling and promise resolving
  • [ ] Optimise automated resetting and garbage collection of temporary files
  • [ ] Think about test coverage