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

@ethronjs/assert

v0.1.2

Published

Assertion test library.

Downloads

5

Readme

@ethronjs/assert

NPM version Total downloads

An assertion test library.

Developed in Dogma, compiled to JavaScript.

Engineered in Valencia, Spain, EU by EthronLabs.

Use

The package must be imported as follows:

const assert = require("@ethronjs/assert");

To load plugins, use its method plugin() as follows:

assert.plugin(require("@ethronjs/assert.plugin.fs"));

Type assertions

To check whether a value is of a type or another, we can use:

//string
assert(value).isText()
assert(value).isNotText()

assert(value).isString()
assert(value).isNotString()

//boolean
assert(value).isBool()
assert(value).isNotBool()

assert(value).isBoolean()
assert(value).isNotBoolean()

//number
assert(value).isNum()
assert(value).isNotNum()

assert(value).isNumber()
assert(value).isNotNumber()

//null
assert(value).isNil()
assert(value).isNotNil()

assert(value).isNull()
assert(value).isNotNull()

//object
assert(value).isMap()
assert(value).isNotMap()

assert(value).isObject()
assert(value).isNotObject()

//array
assert(value).isList()
assert(value).isNotList()

assert(value).isArray()
assert(value).isNotArray()

//set
assert(value).isSet()
assert(value).isNotSet()

//function
assert(value).isFn()
assert(value).isNotFn()

assert(value).isFunction()
assert(value).isNotFunction()

//callable object
assert(value).isCallable()
assert(value).isNotCallable()

//promise
assert(value).isPromise()
assert(value).isNotPromise()

For checking if a value is instance of a given class, we can use:

assert(value).isInstanceOf(className)
assert(value).isInstanceOf(class)

assert(value).isNotInstanceOf(className)
assert(value).isNotInstanceOf(class)

Instead of isInstanceOf() and isNotInstanceOf(), we also can use their aliases is() and isNot().

eq(), ne(), lt(), le(), gt() and ge()

With eq(), ne(), lt(), le(), gt() and gt() we can check whether a value is equal to, not equal to, less than, less than or equal to, greater than and greater than or equal to another.

assert(value1).eq(value2)
assert(value1).ne(value2)
assert(value1).lt(value2)
assert(value1).le(value2)
assert(value1).gt(value2)
assert(value1).ge(value2)

sameAs() and notSameAs()

With sameAs() and notSameAs() we can check whether a value is same as other:

assert(value).sameAs(value)
assert(value).notSameAs(value)

similarTo() and notSimilarTo()

With similarTo() and notSimilarTo() we check whether an array is equal to another, not keeping in mind the order of the items:

assert(array1).similarTo(array2)
assert(array1).notSimilarTo(array2)

Examples:

assert([1, 2, 3]).similarTo([2, 1, 3])  //true
assert([1, 2, 3]).similarTo([3, 2, 1])  //true
assert([1, 2, 3]).similarTo([1, 3, 2])  //true
assert([1, 2, 3]).similarTo([1, 2, 4])  //false

between() and notBetween()

With between() and notBetween(), we check whether a value is into a range:

assert(value).between(start, end)
assert(value).notBetween(start, end)

includes(), notIncludes() and doesNotInclude()

With includes(), notIncludes() and doesNotInclude() we check whether a string contains a given text or an array contains an item:

assert(value).includes(item)
assert(value).notIncludes(item)
assert(value).doesNotInclude(item)

has(), notHas() and doesNotHave()

With has(), notHas() and doesNotHave() we can check whether an object contains one or several fields:

assert(value).has(field)
assert(value).has(fields)
assert(value).notHas(field)
assert(value).notHas(fields)
assert(value).doesNotHave(field)
assert(value).doesNotHave(fields)

Examples:

assert({one: 1, two: 2, three: 3}).has("one")
assert({one: 1, two: 2, three: 3}).has(["one", "two"])
assert({one: 1, two: 2, three: 3}).has({
  one: 1,
  two: 2
})

forEach()

When the value under assertion is a list, we can use forEach() for checking the items:

assert(value).forEach(fields)
assert(value).forEach(func)

When an object is passed as argument, the fields must have the given values. So, for example, the following one:

assert(dev.getUserWorks(work.user)).isList().isNotEmpty().forEach({user: work.user});

is similar to:

const ww = dev.getUserWorks(work.user);
assert(ww).isList().isNotEmpty();
for (let w of ww) assert(w).has({user: work.user});

When a function is passed, each item is passed to the function. Example:

assert(resp.json()).isList().isNotEmpty().forEach(function(o) {
  assert(o.req).isObject().mem("req").eq(req);
});

isEmpty() and isNotEmpty()

With isEmpty() and isNotEmpty() we can check whether an object is empty as for example a list or string:

assert(value).isEmpty()
assert(value).isNotEmpty()

len() and notLen()

With len() and notLen() we can check the string, array or object length:

assert(value).len(size)
assert(value).notLen(size)

Also possible getting the length from other list:

assert([1, 2, 3]).len([1, 3, 5])  //ok, both with length equal to 3
assert([1, 2, 3]).len([1, 5])     //nop, one with length 3; and other 2

like() and notLike()

With like() and notLike() we check whether a value matches a pattern:

assert(value).like(pattern)
assert(value).notLike(pattern)

startsWith(), notStartsWith() and doesNotStartWith()

With startsWith(), we check whether a value starts with a given prefix:

assert(value).startsWith(prefix)
assert(value).notStartsWith(prefix)
assert(value).doesNotStartWith(prefix)

endsWith(), notEndsWith() and doesNotEndWith()

With endsWith(), we check whether a value ends with a given suffix:

assert(value).endsWith(suffix)
assert(value).notEndsWith(suffix)
assert(value).doesNotEndWith(suffix)

Function assertions

To check whether a function raises an error, we can use raises(), notRaises() and doesNotRaise():

assert(fn).raises()
assert(fn).raises(err)
assert(fn).notRaises()
assert(fn).notRaises(err)
assert(fn).doesNotRaise()
assert(fn).doesNotRaise(err)

The function to check must not expect arguments.

Example:

assert(function() {
  //code
}).raises()

assert(function() {
  //code
}).raises("My custom error.")

mem() and item()

Sometimes, we need to assert multiple members of a source value. With the mem() method, we can get the value for one of its members:

mem(name:string) : ValueWrapper
mem(idx:number) : ValueWrapper
mem(...:string|number) : ValueWrapper

Examples:

value = {x: 123, y: 456};
assert(value).mem("x").eq(123);                                           //ok
assert(value).isMap().mem("x").isNum().eq(123).mem("y").isNum().eq(456);  //ok

value = ["zero", "one", "two", "three"];
assert(value).mem(1).eq("one").mem(0).eq("zero");                         //ok

value = {x: ["zero", "one", "two", "three"]};
assert(value).mem("x", 1).eq("one");                                      //ok

The mem() method always returns with respect to the source value passed to assert(). And the assertions are respect to the last member got from mem().

We can use the alias item() too. Generally it is used when accessing to a list or array.

Chaining assertions

We can chain assertion methods for the same value such as the following example:

assert(number).ge(10).le(25)
//similar to
assert(number).ge(10)
assert(number).le(25)