-
Notifications
You must be signed in to change notification settings - Fork 57
Matchers
A matcher is a special object that can be used with expectThat
and
expectCall
to match only particular kinds of values. This is most clear from
some examples:
// Get a number from somewhere.
var someNumber = 17;
// Express some expectations about its value.
expectThat(someNumber, equals(17)); // Passes
expectThat(someNumber, lessOrEqual(17)); // Passes
expectThat(someNumber, greaterThan(17)); // Fails
expectThat(someNumber, isNull); // Fails
// Expect calls to a mock function with values matching particular regular
// expressions. Return different numbers from each.
var myMockFunc = createMockFunction();
expectCall(myMockFunc)(containsRegExp(/^t.*o$/))
.willOnce(returnWith(19));
expectCall(myMockFunc)(containsRegExp(/^b.*o$/))
.willOnce(returnWith(23));
myMockFunc('taco'); // Returns 19
myMockFunc('burrito'); // Returns 23
In the example above, equals(17)
, lessOrEqual(17)
, isNull
,
containsRegExp(/^t.*o$/)
, and so on are all matchers.
Google JS Test comes with a large number of built-in matchers, documented below. For information on how to define your own matchers, see the section further down in this document.
Equality:
-
_
(underscore) — Match anything. This does not include missing arguments to mock functions. -
isNull
— Matchnull
. -
isUndefined
— Matchundefined
. -
equals(x)
— Match valuesy
wherex
===y
. Note that this means matches must be of the same type, and ifx
is an object (or a function, etc) then matches must be a reference tox
itself. Additionally,x
andy
are considered equal if they are of the same type,x
has agjstestEquals
method, andx.gjstestEquals(y)
is true.
Booleans:
-
evalsToTrue
— Match values which, when casted to a boolean, result intrue
. -
evalsToFalse
— Match values which, when casted to a boolean, result infalse
.
Strings:
-
containsRegExp(r)
— Match strings that match the regular expressionr
. -
hasSubstr(s)
— Match strings that contain the substrings
.
Functions and function arguments:
-
throwsError(r)
— Match functions which, when executed with zero arguments, throw an error whose string form matches the regular expressionr
. -
notPresent
— Match missing arguments to mock functions. (This is distinct from present arguments with a value ofundefined
.) -
maybePresent
— Equivalent toanyOf([_, notPresent])
. -
isMissingArgSentinel
— Matchgjstest.missingArgSentinel
, for use in testing user-defined matchers. See the comments inmissing_arg_matchers.js
for more details.
Arrays and objects:
-
elementsAre(A)
— Given an arrayA
of values and/or matchers, match arrays of the same length where each elements is equal to or matches the corresponding element inA
. -
contains(x)
— Match arrays that contain an element equal tox
, or ifx
is a matcher, that contain an element that matchesx
. -
whenSorted(M)
— Match arrays whose elements, when sorted, match the matcherM
. -
recursivelyEquals(x)
— Given an object or arrayx
, match objects or arrays whose values are equal to the corresponding value inx
, recursing into contained objects and arrays.
Combining matchers:
-
not(x)
— Invert the meaning of another matcher. Ifx
is not a matcher, it is treated as the matcherequals(x)
. -
allOf([x1, x2, x3, ...])
— Match objects that matchx1
and matchx2
and … . If anyxn
is not a matcher, it is treated asequals(xn)
. -
anyOf([x1, x2, x3, ...])
— Match objects that matchx1
or matchx2
or … . If anyxn
is not a matcher, it is treated asequals(xn)
.
Numbers:
greaterOrEqual(x)
greaterThan(x)
lessOrEqual(x)
lessThan(x)
-
isNearNumber(x, err)
— Given a numberx
, match numbersy
such that|x - y| <= err
.
There are also some convenient shorthands:
-
expectEq(expected, actual)
— equivalent toexpectThat(actual, equals(expected))
-
expectNe(x, actual)
— equivalent toexpectThat(actual, not(equals(x)))
-
expectGe(a, b)
— equivalent toexpectThat(a, greaterOrEqual(b))
-
expectGt(a, b)
— equivalent toexpectThat(a, greaterThan(b))
-
expectLe(a, b)
— equivalent toexpectThat(a, lessOrEqual(b))
-
expectLt(a, b)
— equivalent toexpectThat(a, lessThan(b))
-
expectTrue(x)
— equivalent toexpectThat(x, equals(true))
-
expectFalse(x)
— equivalent toexpectThat(x, equals(false))
A matcher is simply an instance of gjstest.Matcher
, which requires the
following parameters to construct:
- A description of the values matched, used in forming readable error messages.
- An inverted description of the values matched, used in forming readable
error messages when the
not
matcher is involved. - A function that decides whether any give value should be matched, used for evaluating expectations.
For more information on defining your own matchers see the documentation in matcher_types.js, and use the built-in matchers as examples.