Skip to content

Tiliqua/assert-js

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

AssertJS

Javascript, battle tested, simple assertion library with no dependencies.

Status:

npm bundle size Hackage-Deps Build Status NPM npm

Example:

/**
 * @param {HTMLElement} element
 */
function doSomethingWithHtmlElement(element)
{
    Assert.instanceOf(element, HTMLElement);

    // do your job
}

Now you are covered by the Assertion, and you don't need to be worried that someone might pass empty object {} to doSomethingWithHtmlElement. doSomethingWithHtmlElement function was designed to accept only HTMLElement, nothing more!

Usage

npm install assert-js --save
let Assert = require('assert-js')

Assert.true(true);
Assert.false(false);
Assert.instanceOf(new String("test"), String);
Assert.instanceOneOf(new String("test"), [String, Number]);
Assert.containsOnly([new String("test"), new String("test")],String);
Assert.containsOnlyString(["test", "test1"]);
Assert.containsOnlyInteger([1, 2]);
Assert.containsOnlyNumber([2, 10.25]);
Assert.integer(1);
Assert.number(0.5);
Assert.oddNumber(3);
Assert.evenNumber(4);
Assert.greaterThan(1, 10);
Assert.greaterThanOrEqual(1, 1);
Assert.lessThan(10, 5);
Assert.lessThanOrEqual(1, 1);
Assert.string("string");
Assert.boolean(true);
Assert.equal(1, 1);
Assert.objectEqual({"key":"value"}, {"key":"value"});
Assert.object({id: 1});
Assert.hasFunction("testFunction", {testFunction: () => { alert('test'); } });
Assert.hasProperty("test", {test: 'value'});
Assert.isFunction(() => { alert('test'); });
Assert.array([1, 2, 3, 4, 5]);
Assert.count(0, []);
Assert.notEmpty(0, [1, 2, 3]);
Assert.jsonString('{"key": "value"}');
Assert.email('norbert@orzechowicz.pl');
Assert.url('https://github.com/Tiliqua/assert-js');
Assert.uuid('3e9009a0-4b2f-414e-bf02-ec0df56fc864');
Assert.hasElement('#div', window.document);
Assert.hasAttribute('data-test', window.document.querySelector('#test'));
Assert.hasAttributes(['data-test', 'id'], window.document.querySelector('#test'));
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));

Assert.true(true);

Asserts that expression or value is equal to true.

Example:

Assert.true(1 === 2); // this will throw an Error.
let falseValue = false;
Assert.true(falseValue); // this will throw an Error.

Assert.false(false);

Asserts that expression or value is equal to false.

Example:

Assert.false(1 !== 2); // this will throw an Error.
let falseValue = true;
Assert.false(falseValue); // this will throw an Error.

Assert.instanceOf(new String("test"), String);

Asserts that value is an instance of specific class.

Example:

let div = window.document.querySelector('#my-div');
Assert.instanceOf(element, HTMLDivElement);

Assert.instanceOneOf(new String("test"), [String, Number]);

Asserts that value is an instance of at least one specific class.

Example:

let div = window.document.querySelector('#my-div');
Assert.instanceOneOf(element, [HTMLDivElement, HTMLElement]);

Assert.containsOnly([new String("test"), new String("test")],String);

Asserts that array contains only instances of specific class.


Assert.containsOnlyString(["test", "test1"]);

Asserts that array contains only strings.


Assert.containsOnlyInteger([1, 2]);

Asserts that array contains only integers.


Assert.containsOnlyNumber([2, 10.25]);

Asserts that array contains only numbers.


Assert.integer(1);

Asserts that value is valid integer.


Assert.number(0.5);

Asserts that value is valid number (integer, float).


Assert.oddNumber(3);

Asserts that value is odd number.


Assert.evenNumber(4);

Asserts that value is event number.


Assert.greaterThan(1, 10)

Asserts that number is greater than.


Assert.greaterThanOrEqual(1, 1)

Asserts that number is greater than or equal.


Assert.lessThan(10, 5)

Asserts that number is less than.


Assert.lessThanOrEqual(1, 1)

Asserts that number is less than or equal.


Assert.string("string");

Assert that value is valid string.


Assert.boolean(true);

Asserts that value is valid boolean.


Assert.object(1, 1);

Asserts that value is equal to expected value.


Assert.objectEqual({"key":"value"}, {"key":"value"});

Asserts that object is equal to expected object.


Assert.object({id: 1});

Asserts that value is valid object.


Assert.hasFunction("testFunction", {testFunction: () => { alert('test'); }});

Asserts that object has function.


Assert.hasProperty("test", {test: 'value'});

Asserts that object has property (it can also be a function).


Assert.isFunction(() => { alert('test'); });

Asserts that value is valid function.


Assert.array([1, 2, 3, 4, 5]);

Asserts that value is valid array.


Assert.oneOf(4, [1, 2, 3, 4, 5]);

Asserts that value is one of expected values.


Assert.count(0, []);

Asserts that array have specific number of elements.


Assert.notEmpty(0, [1, 2, 3]);

Asserts that array is not empty.


Assert.jsonString('{"key": "value"}');

Asserts that value is valid json string.


Assert.email('norbert@orzechowicz.pl');

Asserts that string is valid email address.


Assert.url('https://github.com/Tiliqua/assert-js');

Asserts that string is valid url.


Assert.uuid('3e9009a0-4b2f-414e-bf02-ec0df56fc864');

Asserts that string is valid UUID.


Assert.hasElement('#div', window.document);

Asserts that element has other element under selector.

Example:

let dom = new JSDOM(`<body><div id="div"></div></body>`);

Assert.hasElement('#div', dom.window.document);

Assert.hasAttribute('data-test', window.document.querySelector('#div'));

Asserts that element has expected attribute (it might be empty).

Example:

let dom = new JSDOM(`<body><div id="div" data-test></div></body>`);

Assert.hasAttribute('data-test', dom.window.document.querySelector('#div'));

Assert.hasAttributes(['data-test', 'foo'], window.document.querySelector('#div'));

Asserts that element has expected attributes (it might be empty).

Example:

let dom = new JSDOM(`<body><div id="div" data-test></div></body>`);

Assert.hasAttributes(['data-test','id'], dom.window.document.querySelector('#div'));

Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));

Asserts that function throws expected exception.

Example:

Assert.throws(() => { throw new String('expected message'); }, new String('expected message'));
Assert.throws(() => { throw 'expected message'; }, 'expected message');
Assert.throws(() => { throw new Error(); });
Assert.throws(() => { throw new Error('some not relevant error message'); }, new Error());
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));

Custom exception message

In order to customize error messages (for easier debugging) you can pass error message as a last parameter into all assertions.

Examples:

Assert.uuid('test', 'This value is not valid UUID.');

you can also use variables expected and received in your messages.

Assert.string(1234, 'Expected ${expected} but got ${received}'); // it throws Error("Expected string but got int[1234]")

About

Javascript simple assertion library with no dependencies

Resources

License

Stars

Watchers

Forks

Packages

No packages published