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

kings

v1.0.13

Published

A better, lightweight testing framework

Downloads

21

Readme

  A better test framework for node.

Kings offers a better testing framwork that is lighter and easy to use. To create a test, all you need to do is create a new class that has functions (which are your tests) and export a new instance of that class. When Kings is started, those functions will be called. Kings has built in assertions that will help output the results of your tests.

Below is how to initialize and run Kings:

const ks = require('kings');

ks.initialize({
  pathToTests: './tests'
});

ks.runTests();

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 8 or higher is required.

Installation is done using the npm install command:

$ npm install kings

Updates

  • Added colorful output
  • Output can now be changed to a different function (if you want to log to a file or change it to console.info)
  • Any 'destructor' method is ignored by default
  • Added warnings for improper usage
  • Minor fixes

Features

  • Robust testing framework
  • Quick and easy setup
  • Avoids using any callbacks

Security Issues

If you discover a security vulnerability in Kings, please email [email protected]

API

ks.initialize(configuration)
  • configuration (object)
    • pathToTests (string) Path to tests to be run. Default value, './suite'
    • cwd (string) current working directory for the test runner. Default value, path.dirname(require.main.filename)
    • ignore (Array of strings) test names to be ignored, constructor and destructor are ignored by default. Default value, []
    • colorful (boolean) If output should be colorful. Default true
    • output (function) Function for output. Default console.log

  Initializes KS to run tests. Must be done prior to run any tests.

ks.runAllTests()

Runs all tests found in the configuration's pathToTests

Throws:

  • 'KS has not been initialized.' if ks.initialize(configuration) has not been called
  • 'pathToTests is not a valid directory.' if pathToTests is not a valid directory
ks.runTestClass(filename)
  • filename (string) file that is located somewhere in the pathToTests directory

Runs all tests that are found in the given file.

Throws:

  • 'filename not given.' if filename is empty
  • 'filename is not a string.' if the filename is not a type of string
  • 'KS has not been initialized.' if ks.initialize(configuration) has not been called
  • 'pathToTests is not a valid directory.' if pathToTests is not a valid directory
ks.assertTrue(condition, errorMessage)
  • condition (bool) condition to be asserted
  • errorMessage (string) message to be logged

Assert given condition is true

ks.assertFalse(condition, errorMessage)
  • condition (bool) condition to be asserted
  • errorMessage (string) message to be logged

Assert given condition is false

ks.assertAreEqual(expectedValue, actualValue, errorMessage)
  • expectedValue
  • actualValue
  • errorMessage (string) message to be logged

Asserts expected value does equal actual value

ks.assertStrictEquals(expectedValue, actualValue, errorMessage)
  • expectedValue
  • actualValue
  • errorMessage (string) message to be logged

Asserts expected value does equal actual value using (===)

ks.assertNotNull(value, errorMessage)
  • value
  • errorMessage (string) message to be logged

Asserts value is not null

ks.assertNull(value, errorMessage)
  • value
  • errorMessage (string) message to be logged

Asserts value is null

ks.assertGreaterThan(value1, value2, errorMessage)
  • value1
  • value2
  • errorMessage (string) message to be logged

Asserts value 1 is greater than value 2

ks.assertGreaterThan(value1, value2, errorMessage)
  • value1
  • value2
  • errorMessage (string) message to be logged

Asserts value 1 is less than value 2

assertFailure(errorMessage) 
  • errorMessage (string) message to be logged

Throws an error when called

assertThrowsError(func, errorMessage)
  • func (function) function to run
  • errorMessage (string) message to be logged

Asserts that the given function will throw an error

Displays warning if func is an async fucntion

async assertThrowsErrorAsync(func, errorMessage)
  • func (function) async function to run
  • errorMessage (string) message to be logged

Asserts that the given async function will throw an error

Displays warning if func is not an async function

assertDoesNotThrowError(func, errorMessage)
  • func (function) function to run
  • errorMessage (string) message to be logged

Asserts that the given function does not throw an error

Displays warning if func is an async fucntion

async assertDoesNotThrowErrorAsync(func, errorMessage)
  • func (function) async function to run
  • errorMessage (string) message to be logged

Asserts that the given async function does not throw an error

Displays warning if func is not an async function

Examples

./main.js


function add(x,y) {
  return x+y;
}

function sub(x,y) {
  return x-y;
}

module.exports = {
  add,
  sub
}

./tests/testRunner.js

const ks = require('kings');
const path = require('path');

ks.initialize({
  pathToTests: './suite',
  //By default cwd = path.dirname(require.main.filename)
  ignore: ['skip'],
  colorful: true,
  output: console.info
});

ks.runAllTests(); // Both runAllTests and runTestClass run async
ks.runTestClass('sampleTests2'); // also, 'sampleTests2.js' is ok too

./tests/suite/sampleTests1.js

const ks = require('kings');

class SampleTests1 {
  test1() {
    ks.assertTrue(true, 'this should never fail');
  }

  test2() {
    ks.assertFalse(false, 'This should not fail either');
  }
}

module.exports = new SampleTests1();

./tests/suite/sampleTests2.js

const ks = require('kings');
let main;

class SampleTests2 {
  constructor() {
    main = require('../../main.js');
  }

  skip() {
    console.log('This will be ignored, and not printed');
  }

  testAdd() {
    ks.assertTrue(5 == main.add(2,3), 'Should add to be 5');
    ks.assertEqual(7, main.add(2,5), 'Should add to be 5');
  }

  testSubtract() {
    ks.assertLessThan(3, main.sub(5,1), '3 is less than (5-1)');
  }
}

module.exports = new SampleTests2();
$ node tests/testRunner.js # if running outside of tests folder
 or
$ node testRunner.js # if you are running in the tests folder

License

  MIT