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

json-tests

v0.1.2

Published

Simple test runner using JSON files

Downloads

4

Readme

json-tests stable

npm install json-tests

Load JSON tests and run each suites and tests inside mocha.

You can test http requests, class methods, functions, etc., using only JSON files.

Usage

You can see some examples in directory examples with differente test types.

First, you must create a script for configurating the runner and setting directories with JSON files:

// File: my-project-runner.js

const path   = require('path');
const Runner = require('json-tests/src/runner');
const runner = new Runner(
    [
        path.join(__dirname, 'unit')
    ]
);
runner.run();

Then, run tests using mocha:

$ mocha my-project-runner.js

Test types

Request

json-tests use jf-http-request so you can configure request using options with the keys supported by this module.

In expected key you can use headers if you want to check if some header is present in response.

{
    "description" : "Testing posts",
    "tests"       : [
        {
            "description" : "Get post 1",
            "options"     : {
                "url" : "http://jsonplaceholder.typicode.com/posts/1"
            },
            "expected"    : {
                "statusCode" : 200,
                "body"       : {
                    "userId" : 1,
                    "id"     : 1,
                    "title"  : "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
                    "body"   : "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
                }
            }
        }
    ]
}

Class

If you want to do some unit tests for a class, this is the right type.

// my-class.js
module.exports = class MyClass {
    constructor(value)
    {
        this.value = value;
    }
    sum(value)
    {
        return this.value + value;
    }
}
// test.json
{
    "class" : "./my-class.js",
    "tests" : [
        {
            "description" : "Testing constructor",
            "construct"   : [ 100 ],
            "property"    : "value",
            "expected"    : 100
        },
        {
            "description" : "Testing `sum` method",
            "construct"   : [ 1 ],
            "method"      : "sum",
            "params"      : [ 2 ]
            "expected"    : 3
        }
    ]
}

Is equivalent to:

const MyClass = require('../my-class');
// Testing constructor
const instance = new MyClass(100);
assert.strictEqual(instance.value, 100);
// Testing `sum` method
const instance = new MyClass(1);
assert.strictEqual(instance.sum(2), 3);

You can reuse the same instance between tests setting construct key in JSON root:

// test2.json
{
    "class"     : "./my-class.js",
    "construct" : [ 250 ],
    "tests"     : [
        {
            "description" : "Testing constructor",
            "property"    : "value",
            "expected"    : 250
        },
        {
            "description" : "Testing `add` method",
            "method"      : "sum",
            "params"      : [ 20 ]
            "expected"    : 270
        }
    ]
}

Similar to:

const MyClass = require('../my-class');
const instance = new MyClass(250);
// Testing constructor
assert.strictEqual(instance.value, 250);
// Testing `sum` method
assert.strictEqual(instance.sum(20), 270);

Factory

You can use a factory for evaluating configuration in JSON file.

// sq.js
module.exports = function(value) {
    return value * value;
}
// factory.js
module.exports = function(test, done, suiteConfig, testConfig) {
    const _fn     = require(test.resolvePath(suiteConfig.module));
    const _result = test.check(_fn(...testConfig.params), testConfig.expected);
    if (_result instanceof Error)
    {
        done(_result);
    }
    else
    {
        done();
    }
}
// test.json
{
    "factory" : "./factory.js",
    "module"  : "./my-module",
    "tests"   : [
        {
            "description" : "Testing with factory",
            "params"      : [ 100 ],
            "expected"    : 10000
        }
    ]
}

All keys in JSON except factory, description and tests can be used by factory.