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

periphery

v0.1.2

Published

Generate peripheral scenarios from an object's schema

Downloads

20

Readme

periphery

periphery is a library for generating edge cases for the sake of testing.

This library is still in development and is not ready for production use! Until the first official release of v1.0.0, the api may change rapidly in potentially breaking ways. Furthermore, features and functionality will be added over time.

Feel free to help and contribute, though!

Imagine you are writing an API where your consumers can post JSON data. Likely this data should conform to some format in order for the API to do something meaningful, but theoretically any arbitrary data can be sent over the wire. You want your program to be robust in the face of all the possible types of requests.

periphery can generate many different scenarios given a schema the data should conform to. You can then use these scenarios to write tests against your API in order to ensure its robustness.

Installation

npm install --save-dev periphery

Usage

You will notice the syntax for periphery is very similar to Joi. Periphery can be used completely independently of Joi, but Joi users should be familar with how periphery works.

import periphery from 'periphery';

const schema = periphery.object({
  firstname: periphery.string().max(32),
  lastname: periphery.string().max(32)
});

//  Generate a value which passes validation given by the schema
const value = schema.validValue();

//  Generate a list of scenarios
//  Each scenario will fail for one reason
const scenarios = schema.scenarios();

How to use

periphery is used by first defining a schema and then generating scenarios.

  • Schema: A definition for how an object ought to be structured.
  • Scenario: A particular case where a single rule is violated, thereby theoretically leading to schema invalidation.

Schemas are defined using types and constraints like in Joi. In general, you define the type of a field and then any additional constraints on that field.

const schema = periphery.object({
  apple: periphery.string()
});

When you have a schema object, you can get a list of various scenarios:

schema.scenarios();

This returns a list of scenario objects. Each scenario has two fields:

{
  name, // human-readable description of the scenario
  data  // an object reflecting the scenario
}

You may then use these scenarios at your discretion, for instance in tests. One scenario for the schema defined above might look like:

{
  name: 'apple is a number',
  data: {
    apple: 1.1
  }
}

As you can see, we defined apple to be a string, but the scenario tries to make apple a number.

Example

This is a full use case demonstrated. In this example, we are testing that our API returns a proper 400 response if the consumer attempts to post bad data. We use mocha as a testing framework.

import { expect } from 'chai';
import axios from 'axios';
import periphery from 'periphery';

describe('API', () => {
  const schema = periphery.object({
    title: periphery.string().min(8).max(32)
  });

  describe('should respond with 400 when given an invalid request body', () => {
    schema.scenarios().forEach(scenario => {
      it(scenario.name, () => {
        return axios.post('/resource', scenario.data).then(() => {
          throw new Error('Expected promise to be rejected, but it was resolved')
        }).catch(err => {
          expect(err.response.status).to.equal(400);  
        });
      });
    });
  });
});