Expect
Usage
Node.js
var expect = require('minitest').expect;
Browser
Once the HTML file is setup, you can create a global shortcut, like so:
<script>
var expect = minitest.expect;
</script>
API
All expectations have the following form:
expect(actual).toEqual(expected);
expect(test).toNotBempty();
.toBeEmpty([message])
Also aliased as: mustBeEmpty
Succeeds if test is empty (eg: empty string, array or object). Also succeeds if test is null or undefined.
expect("").toBeEmpty();
expect([]).toBeEmpty();
expect(null).toBeEmpty();
.toNotBeEmpty([message])
Also aliased as: wontBeEmpty
Succeeds if test isn't empty (eg: empty string, array or object).
expect("html".toNotBeEmpty();
expect([1, 2, 3]).toNotBeEmpty();
expect({a: 1}).toNotBeEmpty();
.toInclude(obj, [message])
Also aliased as: mustInclude
Succeeds if collection includes obj.
expect([1, 2, 3]).toInclude(1);
expect("hello world").toInclude("world");
.toNotInclude(obj, [message])
Also aliased as: wontInclude
Succeeds unless collection includes obj.
expect([1, 2, 3]).toNotInclude(4);
.toEqual(expected, [message])
Also aliased as: mustEqual
Succeeds if actual equals expected; but instead of relying on the (unreliable)
==
operator, it conforms to the deep equality definition of CommonJS Unit
Testing/1.0. So Dates, arrays, objects, NaNs can be safely compared and found
equal to each others.
expect(1).toEqual('1');
expect({colors: ['red', 'green']}).toEqual({colors: ['red', 'green']});
.toNotEqual(expected, [message])
Also aliased as: wontEqual
Succeeds unless actual equals expected. Again, it doesn't use the ==
operator, but the CommonJS Unit Testing/1.0 definition of deep equality.
expect(1).toNotEqual(2);
expect({colors: ['red', 'green']}).toNotEqual({colors: ['blue', 'green']});
.toBe(expected, [message])
Also aliased as: toBeSameAs, mustBe, mustBeSameAs
Succeeds if actual ===
expected.
var obj = {a: 1};
expect(obj).toBe(obj);
expect(1).toBe(1);
.toNotBe(expected, [message])
Also aliased as: toNotBeSameAs, wontBe, wontBeSameAs
Succeeds unless actual ===
expected.
expect({a: 1}).toNotBe({a: 1});
expect(1).toNotBe(2);
.toBeWithinDelta(expected, delta = 0.001, [message])
Also aliased as: mustBeWithinDelta, toBeCloseTo and mustBeCloseTo
For comparing floats, succeeds if actual is within delta of expected.
expect(0.999).toBeWithinDelta(1.0, 0.001);
This method is also aliased as .toBeCloseTo()
.
.toNotBeWithinDelta(expected, delta = 0.001, [message])
Also aliased as: wontBeWithinDelta, toNotBeCloseTo and wontBeCloseTo
For comparing floats, succeeds if actual is outside delta of expected.
expect(0.997).toBeWithinDelta(1.0, 0.001);
This method is also aliased as .toNotBeCloseTo()
.
.toBeWithinEpsilon(expected, delta = 0.001, [message])
Also aliased as: mustBeWithinEpsilon
For comparing floats, succeeds if actual is within relative error (epsilon) of expected.
expect(9991).toBeWithinEpsilon(10000, 0.001);
expect(10000).toBeWithinEpsilon(9999.1, 0.0001);
.toNotBeWithinEpsilon(expected, delta = 0.001, [message])
Also aliased as: wontBeWithinEpsilon
For comparing floats, succeeds if actual is outside relative error (epsilon) of expected.
expect(9990 - 1).toNotBeWithinEpsilon(10000);
.toMatch(pattern, [message])
Also aliased as: mustMatch
Succeeds if actual matches the pattern regular expression.
var BLANK = /^\s*$/;
expect(" ").toMatch(BLANK);
.toNotMatch(pattern, [message])
Also aliased as: wontMatch
Succeeds unless actual matches the pattern regular expression.
var BLANK = /^\s*$/;
expect("content").toNotMatch(BLANK);
.toBeTypeOf(expected, [message])
Also aliased as: mustBeTypeOf
Succeeds if typeof actual == expected. It also conveniently supports an 'array'
type that isn't supported natively, and takes care to correctly compare Number
and String object instances with their primitive counterparts.
expect([]).toBeTypeOf('array');
expect({}).toBeTypeOf('object');
expect(new String("str")).toBeTypeOf('string');
.toNotBeTypeOf(expected, [message])
Also aliased as: wontBeTypeOf
Succeeds unless typeof actual == expected. Again, it conveniently supports an
'array'
type that isn't supported natively, and takes care to correctly
compare Number and String object instances with their primitive counterparts.
expect("").toNotBeTypeOf('array');
expect(new String()).toBeTypeOf('object');
expect(new String()).toBeTypeOf('string');
.toBeInstanceOf(expected, [message])
Also aliased as: mustBeInstanceOf
Succeeds if actual is an instance of expected.
expect([]).toBeInstanceOf(Array);
expect({}).toBeInstanceOf(Object);
.toNotBeInstanceOf(expected, [message])
Also aliased as: wontBeInstanceOf
Succeeds unless actual is an instance of expected.
expect("").toNotBeInstanceOf(Array);
expect(123).toNotBeInstanceOf(Object, 123);
.toRespondTo(method, [message])
Also aliased as: mustRespondTo
Succeeds if object has a callable property named method.
expect(Array).toRespondTo('isArray');
expect(Array.prototype).toRespondTo('forEach');
expect("str").toRespondTo('toUpperCase');
.toNotRespondTo(method, [message])
Also aliased as: wontRespondTo
Succeeds unless object has a callable property named method.
expect("str").toNotRespondTo('upcase');
expect(MyObject.prototype).toNotRespondTo('methodName');
.toThrow([error], [message])
Also aliased as: mustThrow
Succeeds if callback throws an exception of type error then returns the exception. If no error type is specified, then all expections are catched.
var callback = function () {
throw new Error("oops");
};
var err = expect(callback).toThrow(Error);
expect(err.message).toEqual("oops");
Please note that .toThrow()
has no refutation counterparts.