TestUtils module

class TestUtils()

TestUtils provides generally useful test utilities. It can be used from mochitests, browser mochitests and xpcshell tests alike.

static TestUtils.consoleMessageObserved(checkFn)

Waits for a console message matching the specified check function to be observed.

Arguments:
  • checkFn (function) – [optional] Called with the message as its argument, should return true if the notification is the expected one, or false if it should be ignored and listening should continue.

Returns:

Promise

static TestUtils.listenForConsoleMessages()

Listens for any console messages (logged via console.*) and returns them when the returned function is called.

Returns:

function – Returns an async function that when called will wait for a tick, then stop listening to any more console messages and finally will return the messages that have been received.

static TestUtils.screenshotArea(eltOrRect, win)

Takes a screenshot of an area and returns it as a data URL.

Arguments:
  • eltOrRect (Element|Rect) – The DOM node or rect ({left, top, width, height}) to screenshot.

  • win (Window) – The current window.

static TestUtils.topicObserved(topic, checkFn)

Waits for the specified topic to be observed.

Arguments:
  • topic (string) – The topic to observe.

  • checkFn (function) – [optional] Called with (subject, data) as arguments, should return true if the notification is the expected one, or false if it should be ignored and listening should continue. If not specified, the first notification for the specified topic resolves the returned promise.

Returns:

Promise

static TestUtils.waitForCondition(condition, msg, interval, maxTries)

Will poll a condition function until it returns true.

Arguments:
  • condition – A condition function that must return true or false. If the condition ever throws, this function fails and rejects the returned promise. The function can be an async function.

  • msg – A message used to describe the condition being waited for. This message will be used to reject the promise should the wait fail. It is also used to add a profiler marker.

  • interval – The time interval to poll the condition function. Defaults to 100ms.

  • maxTries – The number of times to poll before giving up and rejecting if the condition has not yet returned true. Defaults to 50 (~5 seconds for 100ms intervals)

Returns:

Promise Resolves with the return value of the condition function. Rejects if timeout is exceeded or condition ever throws. NOTE: This is intentionally not using setInterval, using setTimeout instead. setInterval is not promise-safe.

static TestUtils.waitForPrefChange(prefName, checkFn)

Waits for the specified preference to be change.

Arguments:
  • prefName (string) – The pref to observe.

  • checkFn (function) – [optional] Called with the new preference value as argument, should return true if the notification is the expected one, or false if it should be ignored and listening should continue. If not specified, the first notification for the specified topic resolves the returned promise.

Returns:

Promise