Skip to content

Latest commit

History

407 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

assertron

NPM versionNPM downloads

GitHub NodeJSCodecov

Changesets

Visual Studio Code

A supplementary assertion library that runs on both NodeJS and browser.

Install

# npm
npm install assertron
# yarn
yarn add assertron
# pnpm
pnpm install assertron
#rush
rush add -p assertron

Usage

assertron provides a collection of assertion methods:

  • assertron.false(value): asserts the provided value is false.
  • assertron.falsy(value): asserts the provided value is falsy.
  • assertron.isInstanceof(value, ClassType): asserts value instanceof ClassType.
  • assertron.pathEqual(actual, expected): asserts the two paths are equal regardless of operating system differences.
  • assertron.rejects(promise): asserts the promise rejects.
  • assertron.repeat(fn, times): repeat the specified function n times and return the last result.
    If the result is a promise, it will run the function sequentially.
  • assertron.resolves(promise): asserts the promise resolves.
  • assertron.satisfies(actual, expected): checks if actual meets the requirements specified by expected.
    Each property in expected can be a value, a RegExp, or a predicate function.
    It uses satisfier internally to check for validity.
    Check out satisfier for more detail.
importafrom'assertron'// assertron is also exported as default.// these passesa.satisfies({a: 1,b: 2},{a: 1})a.satisfies({a: 'foo',b: 'boo'},{a: /foo/})a.satisfies({a: 1, b,2},{a: n=>n===1})// these failsa.satisfies({a: 1},{a: 2})a.satisfies({a: 1},{a: 1,b: 2})a.satisfies({a: 'foo'},{a: /boo/})a.satisfies({a: 1},{a: ()=>false})
  • assertron.throws(...): asserts the promise, function, or async function throws (or rejects) an error.
import{assertron}from'assertron'awaitassertron.throws(Promise.reject(newError('foo')))assertron.throws(()=>{thrownewError('foo')})awaitassertron.throws(()=>Promise.reject(newError('foo')))conste1=awaitassertron.throws(Promise.reject(newSpecificError('foo')),SpecificError)conste2=assertron.throws(()=>{thrownewSpecificError('foo')},SpecificError)conste3=awaitassertron.throws(()=>Promise.reject(newSpecificError('foo')),SpecificError)
  • assertron.true(value): asserts the provided value is true.
  • assertron.truthy(value): asserts the provided value is truthy.

AssertOrder

Assert the code is executed in the expected order.

  • order.once(step: number): asserts the step step executed once.
import{AssertOrder}from'assertron'consto=newAssertOrder()functionfoo(){o.once(1)}foo()foo()// throws
import{AssertOrder}from'assertron'consto=newAssertOrder()functionfoo(){o.once(1)}functionboo(){o.once(2)}foo()boo()
  • order.atLeastOnce(step: number): assert the step executed at least once.
import{AssertOrder}from'assertron'consto=newAssertOrder()for(leti=0;i<10;i++)o.atLeastOnce(1)o.once(2)
  • order.exactly(step: number, times: number): asserts the step step executed exactly n times
import{AssertOrder}from'assertron'consto=newAssertOrder()for(leti=0;i<4;i++)o.exactly(1,3)// throws at i === 3
  • order.any(steps: number[], handler?: (step: number) => void): asserts any of the steps steps executed.
import{AssertOrder}from'assertron'consto=newAssertOrder()for(leti=1;i<=4;i++){if(i%2)o.any([1,3])elseo.any([2,4])}

There are other methods available. Use TypeScript to discover them!

Contribute

yarn
# start making changes
git checkout -b <branch>
yarn watch
# after making change(s)
git commit -m "<commit message>"
git push
# create PR

NPM Commands

There are a few useful commands you can use during development.

yarn test
yarn watch
yarn verify

About

A supplementary assertion library

Topics

Resources

Code of conduct

Security policy

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages