new TestCase( methodName )

Description

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 typeTool 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, NexusHoratio.base, enhances NexusHoratio.xunit.testing with a convenience function to run all TestCases present in testCases. Similarly, this class provides a static method to register such classes.

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();
Details

Classes


new Error()


new Fail()


new Skip()

Members


defaultEqual :NexusHoratio.xunit.TestCase~EqualFunc

Description

Points to the current equalX() function.


id :string

Details
string

typeTool :NexusHoratio.xunit.TypeTool

Methods


<static> register()

Description

Register this class with the default test runner infrastructure.

Details

<static> setUpClass()

Description

Called once before any instances are created.

Details

<static> addClassCleanup( func, ...rest )

Description

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.

Details

<static> doClassCleanups()

Description

Execute all functions registered with addClassCleanup.

Details

run( [ result ] ) → {NexusHoratio.xunit~TestResult}

Description

Execute the test method registered upon instantiation.

Parameters
Name Type Attributes Description
result NexusHoratio.xunit~TestResult <optional>

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

Returns

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

Details

setUp()

Description

Called once before each test method.

Details

addCleanup( func, ...rest )

Description

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.

Details

doCleanups()

Description

Execute all functions registered with addCleanup.

Details

skip( [ msg ] )

Description

Immediately skips a test method.

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

Reason for skipping.

Details

fail( [ msg ] )

Description

Immediately fail a test method.

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

Reason for the failure.

Details

assertEqual( first, second [, msg ] )

Description

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.

Details

assertNotEqual( first, second [, msg ] )

Description

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.

Details

assertTrue( arg [, msg ] )

Description

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.

Details

assertFalse( arg [, msg ] )

Description

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.

Details

assertRaises( exc, func [, msg ] )

Description

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.

Details

assertNoRaises( func [, msg ] )

Description

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.

Details

assertRaisesCause( exc, cause, func [, msg ] )

Description

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.

Details

assertRaisesRegExp( exc, regexp, func [, msg ] )

Description

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.

Details

assertRegExp( target, regexp [, msg ] )

Description

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.

Details

repr( item ) → {string}

Parameters
Name Type Description
item object

Anything.

Returns

String version of item.

Details

<p>Migrate to NexusHoratio.xunit.TestCase#typeTool.repr.</p>


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

Description

Find an equality function appropriate for the arguments.

Parameters
Name Type Description
first object

First argument.

second object

Second argument.

Returns

Function that should be used to test equality.

Details

addEqualFunc( type, func )

Parameters
Name Type Description
type string

As returned from getType.

func NexusHoratio.xunit.TestCase~EqualFunc

Function to call to compare that type.

Details

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

Parameters
Name Type Description
first object

First argument.

second object

Second argument.

Returns

Results of testing equality.


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

Description

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

Parameters
Name Type Description
first object

First argument.

second object

Second argument.

Returns

Results of testing equality.


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

Parameters
Name Type Description
first object

First argument.

second object

Second argument.

Returns

Results of testing equality.


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

Description

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

Results of testing equality.


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

Parameters
Name Type Description
first Array.<object>

First argument.

second Array.<object>

Second argument.

Returns

Results of testing equality.


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

Description

Order is ignored.

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

First argument.

second Map.<object, object>

Second argument.

Returns

Results of testing equality.


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

Description

Order is ignored.

Parameters
Name Type Description
first Set.<object>

First argument.

second Set.<object>

Second argument.

Returns

Results of testing equality.


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

Description

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

Parameters
Name Type Description
first object

First argument.

second object

Second argument.

Returns

Results of testing equality.

Type Definitions


EqualOutput

Properties
Name Type Description
equal boolean

Result of equality test.

detail string

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

Details
object

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

Parameters
Name Type Description
first object

First argument.

second object

Second argument.

Returns

Results of testing equality.

Details
function