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

jest-plugin-action

v2.9.0

Published

Declarative method for testing actions in jest.

Downloads

677

Readme

jest-plugin-action

npm npm npm

Declarative method for testing actions using jest.

Getting Started

Install jest-plugin-action using yarn:

yarn add --dev jest-plugin-action

Motivation

If you haven't already, check out jest-set. jest-plugin-action builds on jest-set by providing the action keyword which allows you to define a function. These functions are lazily defined and are useful for passing directly into expect to test errors, warnings, etc, or for passing into beforeEach to easily set up a scenario before defining expectations. Here's an example:

describe('User', () => {
  describe('.update', () => {
    set('user', () => new User({firstName: 'Mary', lastName: 'Lamb'}));

    describe('with valid firstName and lastName', () => {
      set('firstName', () => 'Test');
      set('lastName', () => 'User');
      action('updateUser', () => user.update({firstName, lastName}));
      beforeEach(updateUser);

      it('should set firstName', () => {
        expect(user.firstName).toEqual('Test');
      });

      it('should compute name', () => {
        expect(user.name).toEqual('Test User');
      });
    });

    describe('with invalid firstName', () => {
      set('firstName', () => null);
      set('lastName', () => null);
      action('updateUser', () => user.update({firstName, lastName}));

      it('should throw an error', () => {
        expect(updateUser).toThrow(ValidationError);
      });
    });
  });
});

Even in this trivial example, it's easy to see the power of action.

  1. We can declare actions in the same way we declare our variables. That keeps all of our changes in one place.
  2. We can create easy-to-read expectations for actions: expect(userUser).toThrow(ValidationError);. It doesn't get any better than this.
  3. Using lazy actions, we can declare them in one scope and access / override variables in another scope, just like set!

Usage

If you want, you can import action from jest-plugin-action at the top of every test:

import action from 'jest-plugin-action';

If you want to install action as a global, you can modify the jest section of your package.json to include:

"jest": {
  "setupFiles": [
    "jest-plugin-action/setup"
  ]
}

Example

Here's an example test that tests action itself:

describe('action', () => {
  set('a', () => 1);
  set('b', () => 2);
  action('add', () => a + b);
  action('multiply', () => a * b);
  action('divide', () => {
    if (b === 0) {
      throw new Error('Cannot divide by zero');
    } else {
      return a / b;
    }
  });

  describe('add', () => {
    it('should not throw an error', () => {
      expect(add).not.toThrow();
    });
  });

  describe('multiply', () => {
    it('should not throw an error', () => {
      expect(multiply).not.toThrow();
    });
  });

  describe('division', () => {
    context('with b < 0', () => {
      set('b', () => -42);

      it('should not throw an error', () => {
        expect(divide).not.toThrow();
      });
    });

    context('with b = 0', () => {
      set('b', () => 0);

      it('should throw an error', () => {
        expect(divide).toThrow('Cannot divide by zero');
      });
    });

    context('with b > 0', () => {
      set('b', () => 100);

      it('should not throw an error', () => {
        expect(divide).not.toThrow();
      });
    });
  });
});