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

@skpm/test-runner

v0.5.1

Published

A test runner for sketch plugins

Downloads

117

Readme

skpm Test Runner 🏃🏽

Introducing Sketch Plugin Testing

  • 👩🏻‍💻 Easy Setup: skpm-test is a complete and easy to set up Sketch Plugin testing solution. In fact, skpm-test works out of the box for any skpm project.

Installation

@skpm/test-runner requires Sketch 49 or higher.

npm install --save-dev @skpm/test-runner

Getting Started

Let's get started by writing a test for a hypothetical function that adds two numbers. First, create a sum.js file:

function sum(context) {
  return a + b;
}
exports default sum;

Then, create a file named sum.test.js. This will contain our actual test:

const sum = require('./sum')

test('adds 1 + 2 to equal 3', (context, document) => {
  expect(sum(1, 2)).toBe(3)
})

Add the following section to your package.json:

{
  "scripts": {
    "test": "skpm-test"
  }
}

Finally, run npm test and skpm-test will print this message:

PASS  ./sum.test.js

You just successfully wrote your first test that ran in Sketch using skpm-test!

This test used expect and toBe to test that two values were exactly identical. To learn about the other things that skpm-test can test, see Using Matchers.

Spying, Stubbing & Mocking ES6 classes

Install your favourite stubbing/mocking library (skpm Test Runner got battle tested using Sinon.JS)

npm install --save-dev sinon

Write test using spies or stubs. See full example below showing how to properly stub and spy on ES6 classes:

// tested class
import { TestedCtrl } from './tested.ctrl';
// whole class to be stubbed - in case like that use `import * as name` syntax!
import * as RandomNumberModule from './randomNumber.model';
// service, from which one static method will be stubbed
import { RandomService } from './random.service';

beforeEach(() => {
    // this stub will return object with value 20, so we can run the test below
    sinon.stub(RandomNumberModule, 'RandomNumber')
         .returns({value: 20});

    // spy on service method
    sinon.spy(RandomService, 'generateRandomNumber');
});

test('should call `RandomService.generateRandomNumber` only once', () => {
    const testedCtrl = new TestedCtrl();
    testedCtrl.proposeDigit();

    expect(RandomService.generateRandomNumber.calledOnce).toBe(true);
});

test('should call return false if generated number is bigger than 9', () => {
    const testedCtrl = new TestedCtrl();
    const proposedDigit = testedCtrl.proposeDigit();

    expect(SomeService.generateRandomNumber.calledOnce).toBe(false);
    expect(proposedDigit).toBe(false);
});

Running the tests on TravisCI

  • Go to Travis

  • Add your repo

  • In the settings, add an environment variable called SKETCH_LICENSE with your sketch license

  • Copy paste the following code in a new file named .travis.yml

    os: osx
    
    language: node_js
    
    node_js:
      - 'node'
    
    before_install:
      - brew update >/dev/null # brew is really verbose and we don't really care about it so shut it up
      - brew cask install sketch # install Sketch
      - mkdir -p "~/Library/Application Support/com.bohemiancoding.sketch3/Plugins" # create plugins folder
      - echo $SKETCH_LICENSE > "~/Library/Application Support/com.bohemiancoding.sketch3/.deployment" # add the Sketch license
    
    cache:
      directories:
        - 'node_modules'
        - $HOME/Library/Caches/Homebrew
    
    script:
      - npm run test -- --app=/Applications/Sketch.app
    
    after_script:
      - rm "~/Library/App Support/com.bohemiancoding.sketch3/.deployment" # remove the Sketch license
  • Commit, Push, done!

Contributing

Send issues and pull requests with your ideas.

Good First Issue is a great starting point for PRs.