Détail du package

@typed/assertions

TylorS16716MIT1.1.0

Well-typed functional assertion library

readme

@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

<summary id=createAssertionsEnvironment>createAssertionsEnvironment(): AssertionEnvironment</summary>

Creates an Assertions Environment where assertions can be counted

Example:

typescript import { createAssertionsEnvironment } from '@typed/assertions' const { stats, assertions } = createAssertionsEnvironment() const { ok } = assertions console.log(stats.count) // => 0 ok(true) console.log(stats.count) // => 1
<summary id=equal<A>>equal&ltA&gt(expected: A, actual: A): A</summary>

Checks if 2 values are equal in terms of value equality

Example:

typescript import { equal } from '@typed/assertions' equal({ a: 1 }, { a: 2 }) // throws AssertionError
<summary id=notEqual<A>>notEqual&ltA&gt(expected: A, actual: A): A</summary>

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

Example:

typescript import { notEqual } from '@typed/assertions' notEqual({ a: 1 }, { a: 2 }) // => { a: 2 } notEqual({ a: 1 }, { a: 1 }) // throws AssertionError
<summary id=notOk>notOk(actual: boolean): boolean</summary>

Checks if a value is false

Example:

typescript import { notOk } from '@typed/assertions' notOk(false) // => false notOk(true) // throws AssertionError
<summary id=ok>ok(actual: boolean): boolean</summary>

Checks if a value is true

Example:

typescript import { ok } from '@typed/assertions' ok(false) // throws AssertionError ok(true) // => true
<summary id=rejects<Err extends Error>>rejects&ltErr extends Error&gt(promise: Promise&ltany&gt): Promise&ltErr&gt</summary>

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

Example:

typescript import { rejects } from '@typed/assertions' rejects(Promise.reject(new Error('foo))) // => resolved Promise containing Error('foo') rejects(Promise.resolve()) // => rejected Promise
<summary id=same<A>>same&ltA&gt(expected: A, actual: A): A</summary>

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

Example:

typescript import { same } from '@typed/assertions' same({}, {}) // throws AssertionError same(1, 1) // => 1
<summary id=throws<Err extends Error>>throws&ltErr extends Error&gt(fn: () =&gt any): Err</summary>

Asserts that the given function throws

Example:

typescript import { throws } from '@typed/assertions' throws(() => { throw new Error('foo') }) // => Error('foo') throws(() => {}) // throws Error

changelog

1.1.0 (2017-07-19)

1.0.0 (2017-07-12)

Features

  • typed-assertions: implement assertions library (79bae8c)