@rbxts-js/expect v3.13.2-ts.3
expect
Upstream: https://github.com/facebook/jest/tree/v27.4.7/packages/expect
This package exports the expect function used in Jest. You can find its documentation in the Jest documentation.
:pencil2: Notes
- :warning: Since
notis a reserved keyword in Lua, we usenever. :warning: Several changes to matchers are made to maintain Lua-nativity. Corresponding changes are made to tests.
toBeInstanceOfcan be used to check that the received value is an instance (or a derived instance) of the expected value, where the expected value is a prototype class. The matcher will error if either the received or expected value isn't an object.toBeDefinedis omitted andtoBeUndefinedcheck fornilsince there is noundefinedin Lua.toBeFalsyandtoBeTruthychecks for Lua falsy and truthy values, not JS onestoHaveLengthchecks using the Lua length operator by default, but instead checks for alengthproperty if the object has one- :warning: Length is only well defined for (non-sparse) array-like tables since the Lua
#operator returns 0 for tables with key-value pairs
- :warning: Length is only well defined for (non-sparse) array-like tables since the Lua
toHaveLengthdoes not accept functions, can't get the argument count of a function in LuatoMatchmatches Lua string patterns or a LuauPolyfillRegExpwhereastoContainmatches exact substringstoStrictEqualis omitted, there is no strict equality in LuaThe
Anymatcher is used to match any instance of a type or object given a constructor function. Lua doesn't have constructors for primitive types. Our deviation for this matcher therefore accepts either a typename string (e.g. "number", "boolean") or a table representing a prototype class, and will error otherwise. If a typename string is passed in, the type is compared against the string. If a table is passed in, it checks that the object passed in is an instance (or a derived instance) of the provided prototype class.any("number"):asymmetricMatch(1) -- true any("number"):toAsymmetricMatcher() -- "Any<number>" any(ClassA):asymmetricMatch(ClassA.new()) -- true any(ClassA):asymmetricMatch(ClassB.new()) -- false any(ClassA):asymmetricMatch(ChildOfClassA.new()) -- trueStringMatchingaccepts Lua string patterns or a LuauPolyfillRegExp- :warning: Although Jest has use cases where
toHavePropertyis used to detect the existence of a property asundefined, we should never try to usetoHavePropertywithnilas the property to check for toStrictEqualdoes not check for array sparseness orundefinedproperties like in Javascript. Its only difference fromtoEqualis that it also applies a Lua type/class check based on the Lua prototypical metatable inheritance pattern
:warning: isError returns
truefor string and table types since we don't have a designated error type in Lua and these two types are what can be used to trigger an error- The throwing matchers (e.g.
toThrow()) will print out stack traces for ALL types (exceptnil) that are thrown whereas in Javascript the stack trace is only printed if you error with an Error type. In other words, executing atoThrowmatcher on something likethrow ''in Javascript will not end up printing the stack trace but doing so witherror("")will print the stack trace for our Lua equivalent. - :warning: When writing custom matchers with
expect.extend(), a first argumentselfis needed to receive thematcherContext. It can be left empty with_if thematcherContextis not be needed. - :warning: Custom throwing matchers should throw errors that follow one of three patterns. Jest Roblox will attempt to
tostringvalues that do not match these patterns and may result in undefined behavior.- strings
- tables with a
messagekey that has a string value - objects with a
__tostringmetamethod
- :warning: Currently, the spyMatchers have undefined behavior when used with jest-mock and function calls with
nilarguments, this should be fixed by ADO-1395 (the matchers may work incidentally but there are no guarantees)