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

@meeshkanml/freddo

v1.2.2

Published

A minimal assertion testing framework for APIs

Downloads

5

Readme

Freddo

Minimal assertion testing framework for APIs

Build Status Install Size XO code style

Install

~ ❯❯❯ npm install @meeshkanml/freddo --save-dev

Usage

const { freddo, expr, exists } = require('@meeshkanml/freddo');

(async () => {
  const isSvg = str => str.trim().startsWith('<svg ')
  /*
    <svg width="120.5" height="20" viewBox="0 0 1205 200" xmlns="http://www.w3.org/2000/svg">
    ...
    </svg>
  */
  await freddo('https://badgen.net/packagephobia/install/sha-regex')
    .status(200)
    .header('content-type', 'image/svg+xml;charset=utf-8')
    .body(isSvg)
    .ensure()
})();

(async () => {
  /*
    {
      "hash":"0000000000000538200a48202ca6340e983646ca088c7618ae82d68e0c76ef5a",
      "time":1325794737,
      "block_index":841841,
      "height":160778,
      "txIndexes":[13950369,13950510,13951472]
    }
  */
  await freddo('https://blockchain.info/latestblock')
    .status(200)
    .body(exists, expr('.hash'))
    .body(exists, expr('.time'))
    .body(([time]) => {
      const DAY = 24 * 60 * 60 * 1000
      return {
          result: time > Date.now()/1000 - DAY,
          error: 'Most recent blockchain block is unrealistically old'
      }
    }, expr('.time'))
    .body(exists, expr('.block_index'))
    .body(([blockHeight]) => {
      return {
        result: blockHeight >= 500000,
        error: 'Block height of blockchain tip is insufficient'
      }
    }, expr('.height'))
    .body(exists, expr('.txIndexes'))
    .ensure()
})();

(async () => {
  /*
    HTTP/1.1 301 Moved Permanently
  */
  await freddo('https://httpstat.us/301')
    .redirectsTo('https://httpstat.us/301')
    .ensure()
})();

Example uses with testing frameworks

AVA

import * as test from 'ava'
import m from '.'
import { freddo, expr, exists } from '@meeshkanml/freddo'
import micro from 'micro'
import testListen from 'test-listen'
import validator from 'validator'

let url
test.before(async () => {
  url = await testListen(micro(m))
})

test('/ip/json', async t => {
  t.is(await freddo(url)
    .status(200)
    .header('content-type', 'application/json; charset=utf-8')
    .body(validator.isJSON)
    .body(exists, expr('.ip')), true)
})

Mocha

import { freddo, expr, exists } from '@meeshkanml/freddo'
import validator from 'validator'
import assert from 'assert'

describe('/ip/json', function() {
  it('should serve a JSON response', async function() {
    assert.strict.ok(await freddo("https://locate.now.sh/ip/json/")
      .status(200)
      .header('content-type', 'application/json; charset=utf-8')
      .body(validator.isJSON)
      .body(exists, expr('.ip')))
  })
})

API

freddo(url[, options])

Returns a Promise for a modified got request object.

url

Type: string | object

The URL to request, as a string, a https.request options object, or a WHATWG URL. (Adapted from got's documentation)

options

Type: object

Any of the https.request options. (Adapted from got's documentation)

freddo.status(expected)

Compares equality of status-code with expected, and returns a boolean.

expected

Type: number | function

freddo.header(entity, expected)

Compares equality of entity header with expected, and returns a boolean.

entity

Type: string

e.g. content-type, content-length, origin, etc.

expected

Type: string | number | function

freddo.body(expected[, expression])

Compares equality of body with expected, and returns a boolean.

expected

Type: string | function

expression

Type: Expression object

Note: When an expression is given, an array containing the matched values is returned. Therefore, in this case, an expected parameter function should treat its argument as an array and destructure it accordingly (e.g. ([x]) => x == 'bar').

freddo.redirectsTo(url)

Checks whether status code contains a redirection code (i.e. 301, 302, 303, 307, or 308) and whether there exists a location header entity containing url.

url

Type: string

freddo.expect(key, expected)

Compares equality of response.key (where response is got's response object) with expected, and returns a boolean.

Note: freddo.ensure('body', expected) is the same as freddo.body(expected) (and the same applies for freddo.status).

key

Type: string

Any of got's response object keys.

expected

Type: string | function

freddo.ensure()

Asserts the boolean response of the preceding functions.

expr(pattern)

Returns an Expression object that can be passed as an expression parameter to the freddo.body function (see above) so as to find a value matching the pattern.

pattern

Type: JSPath path expression

exists()

Returns a function that can be passed as an expected parameter to the freddo.body function to check whether a pattern match is found.

Contributing

Thanks for wanting to contribute! We will soon have a contributing page detailing how to contribute. Meanwhile, feel free to star this repository, open issues, and ask for more features and support.

Please note that this project is governed by the Meeshkan Community Code of Conduct. By participating in this project, you agree to abide by its terms.

License

MIT © Meeshkan