Replay Test
Once you've worked on your game for a while, it's a good idea to add some tests to avoid things breaking in the future. It gives you the confidence to keep publishing new updates!
The @replay/test package is useful for writing tests in Jest for your Replay game. It provides a test platform, which works the same as any other like web and iOS, but returns helpful utility functions for testing.
testSprite(sprite, gameProps, options)#
Parameters#
sprite: The Sprite you want to test called with its props, e.g.Player(playerProps).gameProps: The props defined for your top-levelGame. This sets the device size during tests.options: (Optional) An object with the following properties:initInputs: (Optional) The inputsgetInputsreturns. Match with the platforms you're targeting.mapInputCoordinates: (Optional) A mapping function to adjust an input's (x, y) coordinate to its relative value within a Sprite. The Web package exports this for pointer values.contexts: (Optional) An array of mock context values to inject usingmockContext.initRandom: (Optional) An array of numbers thatrandom()will call, starting from index 0 and looping if it reaches the end. Allows for predictable randomness.size: (Optional) Set the size parameter passed into Sprites.initStore: (Optional) Set the init store for local storage.networkResponses: (Optional) Mock network responses by URL, e.g:
initAlertResponse: (Optional) Set which choice is chosen for OK / cancel alerts. Defaulttrue.nativeSpriteNames: (Optional) A list of Native Sprite names to mock.isTouchScreen: (Optional) Set theisTouchScreenparameter.throwAssetErrors: (Optional) Set if errors are thrown if an asset isn't loaded. Defaulttrue.
testSprite returns an object with the following fields:
nextFrame()#
Increment game by one frame.
jumpToFrame(condition, maxFrames)#
Parameters#
condition: A function that can return a boolean or throw an error.maxFrames: (Optional) Set the maximum number of frames that will run. Default1800.
Asynchronously progress frames of the game until condition is met and no errors are thrown. Condition can also return a Texture (useful for throwing methods like getTexture). Rejects if 30 gameplay seconds (1800 frames) pass and condition not met / still errors.
Note that this will run at almost synchronous speed, but doesn't block the event loop.
resolvePromises()#
An async function that can be used to flush Promises - such as loading all files specified by Sprites using preloadFiles and accessing storage.
setRandomNumbers(array)#
Reset the array of random numbers.
updateInputs(inputs)#
Update the input state for the next frame, such as to indicate the pointer is pressed.
getTextures()#
Returns an array of textures that were just rendered to the screen.
getTexture(testId)#
Get a Texture with a matching prop testId. Throws if there are no matches.
textureExists(testId)#
Boolean of whether a Texture with a testId prop exists.
getByText(text)#
Get an array of text Textures which include text content. Case insensitive. Returns empty array if no matches found.
log#
A Jest mock function to detect if log was called by a Sprite.
audio#
An object of Jest mock functions for testing audio calls.
network#
An object of network mock functions for testing network responses.
store#
A mock local storage store.
alert#
A mock alert object.
updateAlertResponse(isOk)#
Update whether OK / cancel alert chooses OK or cancel.
clipboard#
A mock clipboard object.
mockContext(context, mockValue) / mockMutContext(context, mockValue)#
Mock a Context value, to be passed into options.contexts of testSprite.
Example#
- JavaScript
- TypeScript