@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<A>(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<A>(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<Err extends Error>(promise: Promise<any>): Promise<Err>
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<A>(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<Err extends Error>(fn: () => any): Err
Asserts that the given function throws
Example:
import { throws } from '@typed/assertions'
throws(() => { throw new Error('foo') }) // => Error('foo')
throws(() => {}) // throws Error