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

schema-shot

v1.9.0

Published

Framework-agnostic snapshot testing using 'schema by example' for highly dynamic data

Downloads

113

Readme

schema-shot

Greenkeeper badge

Framework-agnostic snapshot testing using "schema by example" for highly dynamic data

NPM

Build status semantic-release js-standard-style

If you like snap-shot (snapshot testing for any JS framework), but have data that is hard to pin down, maybe this package will be useful. Instead of storing literal data snapshot, it stores json-schema derived from a the snapshot object seen first time (using validate-by-example to derive it). Next time an object arrives, it will be validated against the schema. Any missing property, or new one will trigger an exception.

Read the blog post

Example

Imagine we are fetching most popular item from an API service. Obviously it changes often, so we cannot just store it as a snapshot for direct comparison. We could massage the data and derive invariant snapshots, but that is boilerplate code!

Instead, use schema-shot!

npm install --save-dev schema-shot

In your test

// spec.js
const schemaShot = require('schema-shot')
it('returns most popular item', () => {
  const top = api.getMostPopularItem()
  schemaShot(top)
})

Suppose first time it runs, the API returns top = {id: '45a12e'} - an object with just its id property. The __snapshots__/spec.js.schema-shot file will be saved with

exports['returns most popular item 1'] = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "required": true
    }
  },
  "additionalProperties": false
}

Now imagine the same day later running again. The API returns something else, but the object still has same shape, just a different id {id: 8812f0}. This object passes the schema validation step.

A week later, the new API version gets deployed. Now it returns the top item, but instead of id property, it returns uuid property. The test will fail!

$ npm test
Error: schema difference
  data has additional properties
  data.id: is required

Returned value is schema object

You can inspect or even snap-shot the schema object.

const snapshot = require('snap-shot')
const schemaShot = require('schema-shot')
it('returns schema object', () => {
  const o = {name: 'my name'}
  const schema = schemaShot(o)
  snapshot(schema)
})

Both objects in this case will be

{
  '$schema': 'http://json-schema.org/draft-04/schema#',
  type: 'object',
  properties: {
    name: { type: 'string', required: true }
  },
  additionalProperties: false
}

Inferred property formats

A more specific property formats are inferred, see validate-by-example#inferring-formats and can be overridden.

const person = {
  name: 'Joe Smith',
  email: '[email protected]'
}
// let it infer that person.email has "email" format
schemaShot(person)
// or we can set it ourselves
schemaShot(person, {email: 'email'})
/*
  schema properties
  {
    name: {type: 'string', required: true},
    email: {type: 'string', required: true, format: 'email'}
  }
*/

Dry run and showing the updates

If you just want to see what a new schema would be, without saving it, run the tests with DRY=1 npm test option.

If you want to see the schema and save it, run the tests with SHOW=1 npm test

Arrays

When training with arrays

  • All items in the array must pass the schema derived from the first object

Difference with snapshot testing

The schema shots do not store the direct information. A good example is a test using snap-shot and fake-todos in test/todos-spec.js. The snapshot test always fails on the second run, because a returned todo is different. The JSON schema on the other hand stays consistent, as long as fake-todos returns objects with same shape.

const snapshot = require('snap-shot')
const schemaShot = require('schema-shot')
const generate = require('fake-todos')
describe('fake-todos', () => {
  it.skip('returns a different todo', () => {
    const todos = generate(1)
    snapshot(todos[0])
    /*
      Fails of course, because every todo is different
        1) fake-todos returns a different todo:
           Error: snapshot difference
      {
        id: "4e040570-..." => "129a55b4-..."
        what: "do adults" => "skip chess"
      }
    */
  })
  it('returns a todo', () => {
    const todos = generate(1)
    schemaShot(todos[0])
  })
})

Small print

Author: Gleb Bahmutov <[email protected]> © 2017

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2017 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.