1.0.13 • Published 6 years ago

kings v1.0.13

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

  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 kings-npm@mail.com

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

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago