xunit.TestCase(methodName)

An xUnit style test framework.

Many expected methods exist, such as setUp, setUpClass, addCleanup, addClassCleanup, etc. No tearDown methods, however; use addCleanup.

Assertion methods should always take a plain text string, typically named msg, as the last parameter. This string should be added to the assertion specific error message in case of a failure.

JavaScript does not have portable access to things like line numbers and stack traces (and in the case of userscripts, those may be inaccurate anyway). So it can be difficult to track down a particular test failure. The failure messages do include the name of the test class and test method, but, if the method happens to have several assertions in it, it may not be obvious which one failed. These extra descriptive messages can help with differentiation. This system will emit a debug message if any test method calls more than one assert method without a descriptive message.

While the assertEqual method will handle many cases by looking up special functions comparing by type. There may be times when what it can handle needs to be enhanced. There are currently two ways to make such enhancements.

First, the method addEqualFunc will allow the test method to register an additional function for comparing two identical instances.

Second, the property defaultEqual points to whatever equalX() function should be used if one cannot be found, or if instances differ by type. This fallback defaults to equalEqEqEq which uses the strict equality (===) operator. This can be explicitly set in the test method. The method equalValueOf will use the instance's valueOf() method to get comparable values, and may be useful in such cases.

In many languages, order does not matter for some built-in container types (e.g., Map, Set). The JavaScript standard explicitly specifies that order DOES matter for these types. However, for this test library, the default equalX() methods explicitly IGNORE order.

Implementations for built-in types will be added as needed.

All assertX() and equalX() methods should use repr to turn values into strings for these error messages.

TestCases should run only one test method per instance. The name of the method is registered during instantiation and invoked by calling the run method. Generally, a system, like TestRunner is used to register a number of TestCases, discover the test methods, and invoke all of them in turn.

The sibling library, lib/base, enhances testing with a convenience function to run all TestCases present in module:xunit.testing.testCases. Similarly, this class provides a static method to register such classes.

new TestCase(methodName)

Parameters:
Name Type Description
methodName string

The method to run on this instantiation.

Examples
class FooTestCase extends TestCase {
  testMethod() {
    // Assemble
    // Act

    // Assert
    this.assertEqual(actual, expected, 'extra message');
  }
}

const test = new FooTestCase('testMethod');
const result = test.run();
class BarTestCase extends TestCase {
  testMethod() {
    // Assemble
    // Act

    // Assert
    this.assertEqual(actual, expected, 'extra message');
  }

  static { this.register(); }
}

testing.run();

Classes

Error

Fail

Skip

Members

id :string

typeTool :module:xunit.TypeTool

Methods

addCleanup(func, …rest)

Register a function with arguments to run after a test.

Parameters:
Name Type Attributes Description
func function

Function to call.

rest object <repeatable>

Arbitrary arguments to func.

addEqualFunc(type, func)

Parameters:
Name Type Description
type string

As returned from getType.

func module:xunit.TestCase~EqualFunc

Function to call to compare that type.

assertEqual(first, second, msgopt)

Asserts that two arguments are equal.

Parameters:
Name Type Attributes Default Description
first object

First argument.

second object

Second argument.

msg string <optional>
''

Text to complement the failure message.

assertFalse(arg, msgopt)

Asserts that the argument is a boolean false.

Parameters:
Name Type Attributes Default Description
arg object

Argument to test.

msg string <optional>
''

Text to complement the failure message.

assertNoRaises(func, msgopt)

Asserts that no exception is raised.

Useful for supplying descriptive text when verifying an error does not occur.

Parameters:
Name Type Attributes Default Description
func function

Function to call.

msg string <optional>
''

Text to complement the failure message.

assertNotEqual(first, second, msgopt)

Asserts that two arguments are NOT equal.

Parameters:
Name Type Attributes Default Description
first object

First argument.

second object

Second argument.

msg string <optional>
''

Text to complement the failure message.

assertRaises(exc, func, msgopt)

Asserts the expected exception is raised.

Parameters:
Name Type Attributes Default Description
exc function

Expected Error class.

func function

Function to call.

msg string <optional>
''

Text to complement the failure message.

assertRaisesCause(exc, cause, func, msgopt)

Asserts the expected exception is raised with matching cause.

Parameters:
Name Type Attributes Default Description
exc function

Expected Error class.

cause object

Cause to match.

func function

Function to call.

msg string <optional>
''

Text to complement the failure message.

assertRaisesRegExp(exc, regexp, func, msgopt)

Asserts the expected exception is raised and the message matches the regular expression.

Parameters:
Name Type Attributes Default Description
exc function

Expected Error class.

regexp RegExp

Regular expression to match.

func function

Function to call.

msg string <optional>
''

Text to complement the failure message.

assertRegExp(target, regexp, msgopt)

Asserts the target matches the regular expression.

Parameters:
Name Type Attributes Default Description
target string

Target string to check.

regexp RegExp

Regular expression to match.

msg string <optional>
''

Text to complement the failure message.

assertTrue(arg, msgopt)

Asserts that the argument is a boolean true.

Parameters:
Name Type Attributes Default Description
arg object

Argument to test.

msg string <optional>
''

Text to complement the failure message.

doCleanups()

Execute all functions registered with addCleanup.

equalArray(first, second) → {module:xunit.TestCase~EqualOutput}

Parameters:
Name Type Description
first Array.<object>

First argument.

second Array.<object>

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

equalEqEqEq(first, second) → {module:xunit.TestCase~EqualOutput}

Parameters:
Name Type Description
first object

First argument.

second object

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

equalMap(first, second) → {module:xunit.TestCase~EqualOutput}

Order is ignored.

Parameters:
Name Type Description
first Map.<object, object>

First argument.

second Map.<object, object>

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

equalObject(first, second) → {module:xunit.TestCase~EqualOutput}

This currently only tests Object.entries().

Order is ignored.

Other tests, like frozen and sealed states may be implemented later.

Parameters:
Name Type Description
first object

First argument.

second object

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

equalRegExp(first, second) → {module:xunit.TestCase~EqualOutput}

This converts to strings for comparisons as that encodes all flags.

Parameters:
Name Type Description
first object

First argument.

second object

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

equalSet(first, second) → {module:xunit.TestCase~EqualOutput}

Order is ignored.

Parameters:
Name Type Description
first Set.<object>

First argument.

second Set.<object>

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

equalString(first, second) → {module:xunit.TestCase~EqualOutput}

Parameters:
Name Type Description
first object

First argument.

second object

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

equalValueOf(first, second) → {module:xunit.TestCase~EqualOutput}

For those cases when '===' is too strict.

Parameters:
Name Type Description
first object

First argument.

second object

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

Implements:

fail(msgopt)

Immediately fail a test method.

Parameters:
Name Type Attributes Default Description
msg string <optional>
''

Reason for the failure.

Throws:

getEqualFunc(first, second) → {module:xunit.TestCase~EqualFunc}

Find an equality function appropriate for the arguments.

Parameters:
Name Type Description
first object

First argument.

second object

Second argument.

Returns:
module:xunit.TestCase~EqualFunc -

Function that should be used to test equality.

repr(item) → {string}

Parameters:
Name Type Description
item object

Anything.

Returns:
string -

String version of item.

Deprecated:
  • Migrate to this.typeTool.repr().

run(resultopt) → {module:xunit~TestResult}

Execute the test method registered upon instantiation.

Parameters:
Name Type Attributes Description
result module:xunit~TestResult <optional>

Instance for accumulating results. Typically, a test runner will pass in one of these to gather results across multiple tests.

Returns:
module:xunit~TestResult -

Accumulated results (one is created if not passed in).

setUp()

Called once before each test method.

skip(msgopt)

Immediately skips a test method.

Parameters:
Name Type Attributes Default Description
msg string <optional>
''

Reason for skipping.

Throws:

static addClassCleanup(func, …rest)

Register a function with arguments to run after all tests in the class have ran.

Parameters:
Name Type Attributes Description
func function

Function to call.

rest object <repeatable>

Arbitrary arguments to func.

static doClassCleanups()

Execute all functions registered with addClassCleanup.

static register()

Register this class with the default test runner infrastructure.

static setUpClass()

Called once before any instances are created.

Type Definitions

EqualFunc(first, second) → {module:xunit.TestCase~EqualOutput}

Parameters:
Name Type Description
first object

First argument.

second object

Second argument.

Returns:
module:xunit.TestCase~EqualOutput -

Results of testing equality.

EqualOutput

Properties:
Name Type Description
equal boolean

Result of equality test.

detail string

Details appropriate to the test (e.g., where items differed).