Why SJTest? Very easy-to-use browser-based and/or command line testing.

  • SJTest is simple. You could have a working test suite in minutes. See the HelloWorld example below.
  • SJTest works in the browser -- where your code runs.
  • SJTest can run from the command line for automated testing and continuous integration.
  • SJTest can be deployed as part of production code (it just switches off).
  • SJTest is free & open-source, using the commercially friendly MIT license.

Get Started: Hello World

Tests are defined and run within a normal html page, just like any other javascript. Here is a simple example:

<html>
<head>
	<!-- You don't need jQuery or Bootstrap, but if they're present, we use them for nicer output. -->
	<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
	<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

	<!-- SJTest.js (please download your own copy) -->
	<script src='https://rawgithub.com/winterstein/SJTest/master/SJTest.min.js'></script>
</head>
<body>
	<script>
		
		SJTest.run({name:'HelloWorld',

			easyTest: function() {
				assert(1+1 == 2);
			},

			failingTest: function() {
				assert(false, "Well that was never going to work");						
			}

		});

	</script>
</body>
</html>

Try it now: HelloWorld.html?SJTest=on


Documentation (or JSDoc)

Running SJTest

In a browser: view with SJTest=on

IMPORTANT: by default, SJTest does nothing in the browser. You must switch it on, either by adding SJTest=on to the url, or in the page with
<script>SJTest.on = true;</script>

This is so that SJTest can be deployed as part of production code.

Then just view your html page in any modern browser

From the command line with PhantomJS

First install phantomjs.
Then call
phantomjs SJTest.js MyTestFile.html
.

Cheatsheet: Methods for Writing & Running Tests

See the JSDoc or examples for details.

Run a set of tests SJTest.run()

A test is just a function. If it returns without error, the test passes. If it throws an error, the test fails.

Run a set of tests from a separate script file SJTest.runScript()

Run an injected test script (this is super-handy for debugging in real-world contexts) SJTest.runScriptFromUrl()

Example: Let's run some of SJTest's own tests here & now: In the page, we have: <script>SJTest.runScriptFromUrl()</script>
Then the url can inject the test scripts: ?SJTest=test/SJTestTest.js

Run a single test SJTest.runTest()

Run an async test SJTest.runTest() using waitFor()

Cheat-sheet: Useful Functions

See the JSDoc or examples for details.

See These handy functions are both in the SJTest namespace, and exported to global (unless you set SJTest.expose=false).

assert()
Check something!
match()
Flexible matching, e.g. exact matches or jsdoc types such as match(myVar, '?Number|MyClass');
assertMatch()
A nice wrapper for assert + match
isa()
Class matching
waitFor()
Waiting on async behaviour (really useful)
str()
Flexible toString

SJTest settings

SJTest.on
{boolean, false by default} Switch SJTest on/off.
SJTest.expose
{boolean, true by default} Export the utility functions to global.
SJTest.minTime
{milliseconds, 100 by default} In command-line usage, wait this long for the page to load & activate tests.
SJTest.wait
{boolean, false by default} Force command-line SJTest to wait (an alternative to minTime).

Contact

London

Email:
Phone: +44 (0)203 4788 651
Twitter: @sodash
1 Adam Street
London, WC2N 6LE map

Latin America

Email:
Phone: +506 22907872
Rohrmoser, 10109
San José, Costa Rica map

Edinburgh

Email:
Twitter: @winterwellassoc
127 Rose Street Lane South
Edinburgh, EH2 3DT map