Execute jstd tests in your test runner of choice (for example, Karma). This project attempts to replicate the jstd window environment and its API (functions such as TestCase
and AsyncTestCase
) so that a project can continue to run existing jstd tests while migrating to other test runners and libraries.
Include the jstd.shim.js
file in the window before including/injecting jstd test files. The file will define a global called JSTD_SHIM
which can be used after jstd test files have loaded to execute the tests with JSTD_SHIM.execute()
. It will also define global functions that exist in the jstd environment (TestCase, AsyncTestCase, asserts, etc.).
By default JSTD_SHIM outputs general test result information to the console. The following code implements the default reporter object inside jstd.shim.js:
var reporter = Object.create({
// test pass
success: function (resultObj) {},
// test fail
error: function (error, resultObj, name) {
var message;
name = name || error.name;
console.log(name, error);
if (error.stack) {
console.error(error.stack);
}
},
// occurs after a success OR error
result: function (resultObj) {
console.log((resultObj.success ? 'SUCCESS' : 'FAIL') + ' ' + resultObj.description);
},
// occurs on test complete
complete: function (stats) {
console.log(" **** JSTD SHIM RESULTS: " + (stats.fail > 0 || stats.error > 0 ? 'FAIL' : 'SUCCESS') + " **** ");
console.log(" Ran: " + stats.total + " in " + (stats.totalTime / 1000).toFixed(3) + " secs");
console.log(" Passed: " + stats.pass);
console.log(" Failed: " + stats.fail);
console.log(" Error: " + stats.error);
console.log(" Ignored: " + stats.ignore);
}
});
In order to adapt the code to other uses the JSTD_SHIM global provides a method to override the default functions of the reporter.
JSTD_SHIM.modifyReporter(function (defaultReporter) {
// return modified reporter
return defaultReporter.success = function (resultObj) {
console.log("A test passed! Description: " resultObj.description);
}
});
In karma.conf.js
, include the jstd.shim.js
and jstdshim-jasmine-karma.js
files after jasmine has been loaded in the files array:
files = [
JASMINE,
JASMINE_ADAPTER,
'jstd.shim.js',
'adapters/jstdshim-jasmine-karma.js',
'some-jstd-tests/**/*.js'
]
Test changes to jstd.shim.js after installing Karma (e.g. npm install
) from the command line with karma start
.
Begin the test runner from a console:
karma start
When the browser window with the test runner appears, click the "Debug" button.
Open the Browser debug tools for the test window (F12) - this allows you to see the test
output in the debug console.
To run a single test, add the following to the end of the test url:
?tests=
For example:
http://localhost:9876/debug.html?tests=TestTextDocumentViewer
- 0.1.0 - Initial release
- 0.1.1 - Noop reporter, package.json updates, doc refinement
- 0.1.2 - jstdshim-jasmine-karma adapter fix
- 0.1.3 - fixed async bug where successful tests were getting completed twice
- 0.1.4 - allow single unit test to be run instead of full suite