Assert module

For XPCShell tests and mochitests, Assert is already present as an instantiated global to which you can refer - you don’t need to construct it yourself. You can immediately start using Assert.ok and similar methods as test assertions.

The full class documentation follows, but it is perhaps worth noting that this API is largely identical to NodeJS’ assert module, with some omissions/changes including strict mode and string matching.

class Assert(reporterFunc, isDefault)

This module is based on the CommonJS spec

When you see a jsdoc comment that contains a number, it’s a reference to a specific section of the CommonJS spec.

1. The assert module provides functions that throw AssertionError’s when particular conditions are not met.

To use the module you may instantiate it first.

Arguments:
  • reporterFunc (reporterFunc) – Allows consumers to override reporting for this instance.

  • isDefault (boolean) – Used by test suites to set reporterFunc as the default used by the global instance, which is called for example by other test-only modules. This is false when the reporter is set by content scripts, because they may still run in the parent process.

Assert.ok(value, message)

4. Pure assertion tests whether a value is truthy, as determined by !!guard. assert.ok(guard, message_opt); This statement is equivalent to assert.equal(true, !!guard, message_opt);. To test strictly for the value true, use assert.strictEqual(true, guard, message_opt);.

Arguments:
  • value (*) – Test subject to be evaluated as truthy.

  • message (string) – Short explanation of the expected result.

Assert.equal(actual, expected, message)

5. The equality assertion tests shallow, coercive equality with ==. assert.equal(actual, expected, message_opt);

Arguments:
  • actual (*) – Test subject to be evaluated as equivalent to expected.

  • expected (*) – Test reference to evaluate against actual.

  • message (string) – Short explanation of the expected result.

Assert.notEqual(actual, expected, message)

6. The non-equality assertion tests for whether two objects are not equal with !=

Arguments:
  • actual (*) – Test subject to be evaluated as NOT equivalent to expected.

  • expected (*) – Test reference to evaluate against actual.

  • message (string) – Short explanation of the expected result.

Examples:

assert.notEqual(actual, expected, message_opt);
Assert.strictEqual(actual, expected, message)

9. The strict equality assertion tests strict equality, as determined by ===. assert.strictEqual(actual, expected, message_opt);

Arguments:
  • actual (*) – Test subject to be evaluated as strictly equivalent to expected.

  • expected (*) – Test reference to evaluate against actual.

  • message (string) – Short explanation of the expected result.

Assert.notStrictEqual(actual, expected, message)

10. The strict non-equality assertion tests for strict inequality, as determined by !==. assert.notStrictEqual(actual, expected, message_opt);

Arguments:
  • actual (*) – Test subject to be evaluated as NOT strictly equivalent to expected.

  • expected (*) – Test reference to evaluate against actual.

  • message (string) – Short explanation of the expected result.

Assert.deepEqual(actual, expected, message)

7. The equivalence assertion tests a deep equality relation. assert.deepEqual(actual, expected, message_opt);

We check using the most exact approximation of equality between two objects to keep the chance of false positives to a minimum. JSON.stringify is not designed to be used for this purpose; objects may have ambiguous toJSON() implementations that would influence the test.

Arguments:
  • actual (*) – Test subject to be evaluated as equivalent to expected, including nested properties.

  • expected (*) – Test reference to evaluate against actual.

  • message (string) – Short explanation of the expected result.

Assert.notDeepEqual(actual, expected, message)

8. The non-equivalence assertion tests for any deep inequality. assert.notDeepEqual(actual, expected, message_opt);

Arguments:
  • actual (*) – Test subject to be evaluated as NOT equivalent to expected, including nested properties.

  • expected (*) – Test reference to evaluate against actual.

  • message (string) – Short explanation of the expected result.