Class: SJTest

SJTest

new SJTest()

Simple Javascript Testing (for browser-based code). Extend an existing object if present, to allow the user to put in settings. The default values below use ||s to let any user settings take precedence.
Source:

Members

(static) LOGTAG

Used with all console.log output, for easy filtering.
Source:

(static) version

What version of SJTest is this?
Source:

(static) wait

If true, isDone() will return false. Use-case: To avoid PhantomJS stopping early before after-page-load tests are setup, set true while loading & setting up tests, then you must set to false.
Source:
See:
  • SJTest.minTime

Methods

(static) assert(betrue, msg)

An assert function. Error handling can be overridden by replacing SJTest.assertFailed()
Parameters:
Name Type Description
betrue If true (or any truthy value), do nothing. If falsy, console.error and throw an Error. HACK: As a special convenience, the empty jQuery result (ie a jquery select which find nothing) is considered to be false! For testing jQuery selections: Use e.g. $('#foo').length
msg Message on error. This can be an object (which will be logged to console as-is, and converted to a string for the error).
Source:
Returns:
betrue on success. This allows assert() to be used as a transparent wrapper. E.g. you might write var x = assert(mything.propertyWhichMustExist);

(static) assertFailed()

Handle assert() failures. Users can replace this with a custom handler.
Source:

(static) assertMatch()

Convenience for assert(match(value, matcher), value+" !~ "+matcher); Because it's a common use case. Arguments are alternating [airs of value, matcher (as many pairs as you like). E.g. assertMatch(myNumericValue, Number); or assertMatch(myNumericValue, Number, specificStringValue, "foo|bar");
Source:

(static) display()

Called automatically. Adds a table of test results to the page. The table floats above the normal page, and can be closed.
Source:

(static) expectTests(n, timeoutnullable)

How many tests should we see? If less than this, then the page's tests are not yet done. Use-case: for async / delayed tests, to make sure they aren't skipped. This is itself a test (so you can see it pass/fail) -- but it is _not_ counted as one of the n. Note: Currently, this can only be called once per page.
Parameters:
Name Type Attributes Description
n Number How many tests does this page have? (excludes the expectTests one which this call will make)
timeout Number <nullable>
Milliseconds. Defaults to 10,000 (10 seconds)
Source:

(static) isa(obj, klass) → {Boolean}

Like instanceof, but more robust.
Parameters:
Name Type Description
obj Can be null/undefined (returns false)
klass e.g. Number
Source:
Returns:
true if obj is an example of klass.
Type
Boolean

(static) isDone() → {Boolean}

Source:
Returns:
true if all tests are run, and minTime has expired
Type
Boolean

(static) match(value, matcher)

Flexible matching test
Parameters:
Name Type Description
value
matcher Can be another value. Or a Class. Or a JSDoc-style class spec such as "?Number" or "Number|Function". Or a regex (for matching against strings). Or true/false (which match based on ifs semantics, e.g. '' matches false). Or an object (which does partial matching, allowing value to have extra properties).
Source:
Returns:
true if value matches, false otherwise

(static) removeValue()

array utility: remove by value. @return array
Source:

(static) run(testSet)

Parameters:
Name Type Description
testSet object A set of test functions to run, e.g. { name: "MySimpleTests" MyTest1: function() { assert( 1+1 == 2); } MyTest2: function() { assert(match("aa", /[abc]+/)); } }
Source:

(static) runScript(url, afternullable)

Parameters:
Name Type Attributes Description
url String Can be absolute or relative to the page.
after function <nullable>
Optional callback to run after loading.
Source:

(static) runScriptFromUrl()

Use with SJTest=PathToMyScript in the page url. Call this to run a script (if there is one) specified by SJTest=path in the url parameters.

Note: This is restricted to relative urls for security. This may still have security implications. If you call this, you allow a potentially malicious link to run arbitrary scripts from the same domain in the page. So do not use if an attacker could place a script onto the same domain, or abuse one of yours.

Source:

(static) runTest(testNamenon-null, testFnnullable, waitFornullable, timeoutnullable)

Parameters:
Name Type Attributes Description
testName String
testFn function <nullable>
If null, look for a test by this name.
waitFor function <nullable>
A check for test-success which will be run periodically. Returns true when done (or a String, which will be reported as the test-details). Throw an error if the test fails.
timeout Number <nullable>
max milliseconds to allow. Default to 10000 (10 seconds)
Source:

(static) waitFor(condition, callbacknullable, timeoutnullable, onTimeoutnullable)

Waitfor, adapted from http://blog.jeffscudder.com/2012/07/waitfor-javascript.html This does *not* block, but calls the callback when ready and any then/done/fail deferred functions.

Note: Why no blocking? Blocking is problematic given that normal javascript is single-threaded with switching. Usually you're waiting on an ajax request. Blocking would deny the ajax handler a chance to run. So you would block forever.

Parameters:
Name Type Attributes Description
condition function return true when ready. Errors are ignored.
callback function <nullable>
Called once condition is true.
timeout Number <nullable>
Max time in milliseconds. If unset: wait indefinitely.
onTimeout function <nullable>
Called if timeout occurs.
Source:
Returns:
A jQuery deferred object (IF jQuery is present), so you can do waitFor(X).then(Y). null if no jQuery.