assert-js

Javascript simple assertion library with no dependencies.

AssertJS

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

Status:

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. doSomethingWithHtmlElementfunction 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 expectedand receivedin your messages.

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

Repository

https@github.com:Tiliqua/assert-js


上一篇:@iiif/manifold
下一篇:@pirxpilot/tip

相关推荐

  • 🙋Hanjst汉吉斯特改进+enSafeExpression安全表达式等

    Hanjst汉吉斯特模版语言及模版引擎,近期持续改进升级。 这次改进主要是增加了对安全输出表达式兼容,由于涉及到对软件开发过程中的效率和软件运行效率的平衡和取舍,所以多写了几句,以描述这个权衡利弊对...

    3 个月前
  • 🙋Hanjst汉吉斯特升级:+showImageAsync及性能改进等

    自2019年元旦🙋Hanjst汉吉斯特 模板语言及其编译引擎发布,已经过去一年多了。 这期间随着 🙋Hanjst汉吉斯特 的推广应用,我们也陆续发布了如下一些更新内容: 🛠️Hanjst/汉吉...

    4 个月前
  • 🙋Hanjst汉吉斯特优化+JsonDataFromScript等

    近日继续对 🙋Hanjst汉吉斯特优化改进。这次的改进思考是从服务器端返回的 HanjstJsonData的容器设计问题。目前的做法是服务器端的HanjstJsonData放入终端页面的一个Div元...

    2 个月前
  • 😉我用 Nuxt.js 仿了个掘金

    前言 首先肯定是要夸夸掘金啦,最开始从 CSDN 到 博客园 再到 掘金,个人感觉掘金的技术氛围非常的nice,真是个宝藏社区👏。技术文章大多以前端为主,对前端开发者非常友好,质量也是歪瑞古的。

    3 个月前
  • 😀一个原生js弹幕库

    danmujs 😀一个原生js弹幕库,基于 CSS3 Animation 地址、核心代码 本项目基于 rcbullets,项目约70%的代码基于rcbullets,首先要感谢这个项目的作者,如...

    6 个月前
  • 🕵️‍♀️由原型到JS中的“模拟类”

    讲述了有关 JavaScript 中原型相关知识,又引出了 JavaScript 中的“类“究竟是什么?,以及一系列相关问题。 一、前置知识 1、JavaScript 的面向对象(OOP) ​ 面向...

    4 个月前
  • 🔥《吊打面试官》系列 Node.js 必知必会必问!

    (/public/upload/f204a3b224d986128f1b4d9b8d06cd17) 前言 codeing 应当是一生的事业,而不仅仅是 30 岁的青春🍚 本文已收录 Git...

    5 个月前
  • 💖CSS + JS 送学妹满屏幕小爱心

    故事开始 午饭时间,暗恋已久的学妹拉着我的衣袖:“学长学长,你能不能让这些爱心变成五颜六色的吗~”。 我在旁边笑开了花~~~ image.png(/public/upload/04aaa24e...

    4 个月前
  • (干货👍)从详细操作js数组到浅析v8中array.js

    前言 最近在写面试编程题,经常用到数组,经常想偷个懒,用它提供的方法,奈何还是对数组方法使用不熟练,导致写了很多的垃圾代码,很多地方稍加修改的话肯定变得简洁高效优雅👊 所以✍这篇文章本着了解一下Ja...

    1 个月前
  • (vuejs学习)2、使用ElementUI(*)

    1.element安装 开发环境是win10,一到node官网下载node的.msi包(https://npm.taobao.org/mirrors/node/v10.16.0/nodev10.16....

    1 年前

官方社区

扫码加入 JavaScript 社区