@typed/assertions
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@typed/assertions

Get It

npm install --save-dev @typed/assertions
# or
yarn add --dev @typed/assertions

Basic usage

import { equal, ok } from '@typed/assertions'

class Foo {
  constructor() {
    this.bar = 'bar'
  }
}

// with mocha
describe('Foo', () => {
  it(`does bar`, () => {
    const foo = new Foo()

    ok(foo instanceof Foo)
    equal('bar', foo.bar)
  })
})

API

createAssertionsEnvironment(): AssertionEnvironment

Creates an Assertions Environment where assertions can be counted

Example:

import { createAssertionsEnvironment } from '@typed/assertions'

const { stats, assertions } = createAssertionsEnvironment()

const { ok } = assertions

console.log(stats.count) // => 0

ok(true)

console.log(stats.count) // => 1
equal&ltA&gt(expected: A, actual: A): A

Checks if 2 values are equal in terms of value equality

Example:

import { equal } from '@typed/assertions'

equal({ a: 1 }, { a: 2 }) // throws AssertionError
notEqual&ltA&gt(expected: A, actual: A): A

Checks if two values are not equal in terms of value equality

Example:

import { notEqual } from '@typed/assertions'

notEqual({ a: 1 }, { a: 2 }) // => { a: 2 }

notEqual({ a: 1 }, { a: 1 }) // throws AssertionError
notOk(actual: boolean): boolean

Checks if a value is false

Example:

import { notOk } from '@typed/assertions'

notOk(false) // => false
notOk(true) // throws AssertionError
ok(actual: boolean): boolean

Checks if a value is true

Example:

import { ok } from '@typed/assertions'

ok(false) // throws AssertionError
ok(true) // => true
rejects&ltErr extends Error&gt(promise: Promise&ltany&gt): Promise&ltErr&gt

Asserts that a Promise has rejected, returning a resolved Promise containing the error.
If the promise does not reject returns a rejected Promise.

Example:

import { rejects } from '@typed/assertions'

rejects(Promise.reject(new Error('foo))) // => resolved Promise containing Error('foo')
rejects(Promise.resolve()) // => rejected Promise
same&ltA&gt(expected: A, actual: A): A

Checks if 2 values are the same in terms of ===

Example:

import { same } from '@typed/assertions'

same({}, {}) // throws AssertionError
same(1, 1) // => 1
throws&ltErr extends Error&gt(fn: () =&gt any): Err

Asserts that the given function throws

Example:

import { throws } from '@typed/assertions'

throws(() => { throw new Error('foo') }) // => Error('foo')
throws(() => {}) // throws Error

Readme

Keywords

none

Package Sidebar

Install

npm i @typed/assertions

Weekly Downloads

6

Version

1.1.0

License

MIT

Last publish

Collaborators

  • typed