@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<A>(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<A>(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<Err extends Error>(promise: Promise<any>): Promise<Err></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<A>(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<Err extends Error>(fn: () => 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