| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>Browser example using harness.js</title> | ||
| </head> | ||
| <body> | ||
| <header> | ||
| <h1>Browser example using harness.js</h1> | ||
| </header> | ||
| <section> | ||
| <p>Check your JavaScript console for results</p> | ||
| <script> | ||
| // Just some shim code to avoid errors | ||
| var exports = {}, | ||
| require = function () {}, | ||
| assert = { | ||
| fail: function (msg) { | ||
| throw msg; | ||
| }, | ||
| ok: function (expr, msg) { | ||
| if (expr == true) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| equal: function (expr1, expr2 , msg) { | ||
| if (expr == expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| strictEqual: function (expr1, expr2 , msg) { | ||
| if (expr === expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| notEqual: function (expr1, expr2 , msg) { | ||
| if (expr != expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| strictNotEqual: function (expr1, expr2 , msg) { | ||
| if (expr !== expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| } | ||
| }; | ||
| </script> | ||
| <script src="../harness.js"></script> | ||
| <p>Running an example of all tests passing</p> | ||
| <script> | ||
| // Here's how we setup some simple tests. | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "We're doing our first test group, should pass"); | ||
| harness.completed(test_label); | ||
| }, label: "Test 1"}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "We're doing our second test, should pass"); | ||
| harness.completed(test_label); | ||
| }, label: "Test 1"}); | ||
| // Now run our test groups. | ||
| harness.RunIt("browser-exmaple.html", 10); | ||
| </script> | ||
| <section> | ||
| <footer> | ||
| harness can be found at <a href="https://github.com/rsdoiel/harness-js">github.com/rsdoiel/harness-js</a>. | ||
| </footer> | ||
| </body> | ||
| </html> |
| var path = require("path"), | ||
| assert = require("assert"), | ||
| harness = require("../harness"); | ||
| // This are tests targeted at the browser only | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Browser only"); | ||
| harness.completed(test_label); | ||
| }, label: "BrowserOnly", targets: ["browser"]}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Node only"); | ||
| harness.completed(test_label); | ||
| }, label: "NodeOnly", targets: ["node"]}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Mongo only"); | ||
| harness.completed(test_label); | ||
| }, label: "MongoOnly", targets: ["mongo"]}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Mongo only"); | ||
| harness.completed(test_label); | ||
| }, label: "Combination", targets: ["node", "mongo", "browser"]}); | ||
| harness.RunIt("Target Demo", 10); |
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>Browser testing harness.js</title> | ||
| </head> | ||
| <body> | ||
| <header> | ||
| <h1>Browser testing harness.js</h1> | ||
| </header> | ||
| <section> | ||
| <p>Check your JavaScript console for results</p> | ||
| <p>Loading harness.js</p> | ||
| <script src="../harness.js"></script> | ||
| <!-- use a browser ship to support the default tests --> | ||
| <script> | ||
| // Just some shim code to allow running the unaltered | ||
| // tests in the browser. | ||
| var exports = {}, | ||
| // Let's fake NodeJS' require function | ||
| require = function (name) { | ||
| switch (name) { | ||
| case 'path': | ||
| return { | ||
| basename: function (file_path) { | ||
| if (!file_path) { | ||
| return ""; | ||
| } | ||
| if (file_path.indexOf("/") >= 0) { | ||
| return file_path.substr( | ||
| file_path.lastIndexOf("/")); | ||
| } | ||
| return file_path; | ||
| } | ||
| }; | ||
| case 'assert': | ||
| return { | ||
| ok: function (expr, msg) { | ||
| if (expr) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| equal: function (expr1, expr2, msg) { | ||
| if (expr1 == expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| strictEqual: function (expr1, expr2, msg) { | ||
| if (expr1 === expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| notEqual: function (expr1, expr2, msg) { | ||
| if (expr1 != expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| }, | ||
| strictNotEqual: function (expr1, expr2, msg) { | ||
| if (expr1 !== expr2) { | ||
| return true; | ||
| } | ||
| throw msg; | ||
| } | ||
| }; | ||
| case '../harness': | ||
| return harness; | ||
| } | ||
| return false; | ||
| }, | ||
| module = { | ||
| filename: false | ||
| }; | ||
| </script> | ||
| <p>Loading harness_test.js script used testing for NodeJS and MongoDB Shell</p> | ||
| <script src="harness_test.js"></script> | ||
| <p>Check your browser's JavaScript console for the results.</p> | ||
| <section> | ||
| <footer> | ||
| harness can be found at <a href="https://github.com/rsdoiel/harness-js">github.com/rsdoiel/harness-js</a>. | ||
| </footer> | ||
| </body> | ||
| </html> |
+73
-24
@@ -16,4 +16,42 @@ // | ||
| running_tests = [], | ||
| skipped_tests = [], | ||
| platform = "unknown", | ||
| complete_called = false; | ||
| // Decide what platform we're running on. | ||
| var inMongo = function () { | ||
| var t = false; | ||
| try { | ||
| t = (process === undefined && windows === undefined); | ||
| } catch (err) { | ||
| } | ||
| return t; | ||
| }; | ||
| var inNode = function () { | ||
| var t = false; | ||
| try { | ||
| t = (process !== undefined); | ||
| } catch (err) { | ||
| } | ||
| return t; | ||
| }; | ||
| var inBrowser = function () { | ||
| var t = false; | ||
| try { | ||
| t = (window !== undefined); | ||
| } catch (err) { | ||
| } | ||
| return t; | ||
| }; | ||
| if (inNode()) { | ||
| platform = "node"; | ||
| } else if (inBrowser()) { | ||
| platform = "browser"; | ||
| } else { | ||
| platform = "mongo"; | ||
| } | ||
| // Several methods to make testing | ||
@@ -40,4 +78,4 @@ // harness.js easier. | ||
| } | ||
| if (test.target === undefined) { | ||
| test.target = []; | ||
| if (test.targets === undefined) { | ||
| test.targets = []; | ||
| } | ||
@@ -58,16 +96,5 @@ test_groups.push(test); | ||
| var RunIt = function (module_name, test_delay, target) { | ||
| var RunIt = function (module_name, test_delay) { | ||
| var int_id; | ||
| if (target === undefined) { | ||
| // Guess what we're in | ||
| target = "unknown"; | ||
| if (require !== undefined) { | ||
| target = "node@0.8.11"; | ||
| } else if (require === undefined && load !== undefined) { | ||
| target = "mongo@2.2"; | ||
| } else { | ||
| target = "browser"; | ||
| } | ||
| } | ||
| // run, runs a test group. | ||
@@ -80,6 +107,13 @@ var run = function () { | ||
| typeof group_test.label === "string") { | ||
| console.log("\tStarting " + group_test.label + " ..."); | ||
| running_tests.push(group_test.label); | ||
| console.log("\t\t" + group_test.label + " called"); | ||
| group_test.callback(group_test.label); | ||
| if (group_test.targets.length > 0 && | ||
| group_test.targets.indexOf(platform) < 0) { | ||
| console.log("\tSkipping " + group_test.label); | ||
| skipped_tests.push(group_test.label); | ||
| console.log("\t\t" + group_test.label + " Skipped, OK"); | ||
| } else { | ||
| console.log("\tStarting " + group_test.label + " ..."); | ||
| running_tests.push(group_test.label); | ||
| console.log("\t\t" + group_test.label + " called"); | ||
| group_test.callback(group_test.label); | ||
| } | ||
| } else if (group_test === undefined) { | ||
@@ -125,6 +159,13 @@ if (complete_called === false) { | ||
| typeof group_test.label === "string") { | ||
| console.log("\tStarting " + group_test.label + " ..."); | ||
| running_tests.push(group_test.label); | ||
| console.log("\t\t" + group_test.label + " called"); | ||
| group_test.callback(); | ||
| if (group_test.targets.length > 0 && | ||
| group_test.targets.indexOf(platform) < 0) { | ||
| console.log("\tSkipping " + group_test.label); | ||
| skipped_tests.push(group_test.label); | ||
| console.log("\t\t" + group_test.label + " Skipped, OK"); | ||
| } else { | ||
| console.log("\tStarting " + group_test.label + " ..."); | ||
| running_tests.push(group_test.label); | ||
| console.log("\t\t" + group_test.label + " called"); | ||
| group_test.callback(group_test.label); | ||
| } | ||
| } else if (group_test === undefined) { | ||
@@ -156,3 +197,3 @@ if (complete_called === false) { | ||
| } | ||
| console.log("Starting [" + module_name + "] ..."); | ||
@@ -165,16 +206,24 @@ try { | ||
| }; | ||
| var skipped = function () { | ||
| return skipped_tests.length; | ||
| }; | ||
| this.counts = counts; | ||
| this.skipped = skipped; | ||
| this.push = push; | ||
| this.completed = completed; | ||
| this.RunIt = RunIt; | ||
| this.platform = platform; | ||
| try { | ||
| exports.counts = counts; | ||
| exports.skipped = skipped; | ||
| exports.push = push; | ||
| exports.completed = completed; | ||
| exports.RunIt = RunIt; | ||
| exports.platform = platform; | ||
| } catch (err) { | ||
| console.log("Running in browser."); | ||
| console.log("Running in", this.platform); | ||
| } | ||
| }, harness = new Harness(this); |
+2
-2
| { | ||
| "name" : "harness", | ||
| "version" : "0.0.4", | ||
| "description" : "Yet another test organizer except this one works in NodeJS and Mongo 2.2's shell.", | ||
| "version" : "0.0.5", | ||
| "description" : "Yet another test organizer except this one works in NodeJS, Mongo 2.2's shell and modern web browsers.", | ||
| "main" : "./harness.js", | ||
@@ -6,0 +6,0 @@ "repository" : { |
+41
-1
@@ -5,3 +5,6 @@ [](http://travis-ci.org/rsdoiel/harness-js) | ||
| Yet another test organizers except that is work in NodeJS, Mongo's shell. Being able to run across these three environments with depending on things like RequireJS was a major reason to write this module. Hopefully it is helpful to others. | ||
| Yet another test organizers except that is work in NodeJS, Mongo's shell and | ||
| you modern web browser. Being able to run across these three environments | ||
| without depending on libraries like RequireJS was a major reason I wrote this | ||
| module. Hopefully it is helpful to others. | ||
@@ -132,2 +135,39 @@ # Overview | ||
| mongo-example.js Success! | ||
| ``` | ||
| # Including targets for tests | ||
| Now that we can run tests on multiple platforms we still have occasion | ||
| to only need to run tests for specific targets. You can test a list | ||
| of targets when you call harness' push method. The list of supported | ||
| targets are "node", "mongo" and "browser". Here's an example of group | ||
| tests for each of these targets- | ||
| ```JavaScript | ||
| var path = require("path"), | ||
| assert = require("assert"), | ||
| harness = require("../harness"); | ||
| // This are tests targeted at the browser only | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Browser only"); | ||
| harness.completed(test_label); | ||
| }, label: "BrowserOnly", targets: ["browser"]}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Node only"); | ||
| harness.completed(test_label); | ||
| }, label: "NodeOnly", targets: ["node"]}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Mongo only"); | ||
| harness.completed(test_label); | ||
| }, label: "MongoOnly", targets: ["mongo"]}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "Mongo only"); | ||
| harness.completed(test_label); | ||
| }, label: "Combination", targets: ["node", "mongo", "browser"]}); | ||
| harness.RunIt("Target Demo", 10); | ||
| ``` |
+91
-10
@@ -15,5 +15,38 @@ // | ||
| assert = require("assert"), | ||
| harness = require("../harness"); | ||
| harness = require("../harness"), | ||
| test_name = "tests/harness_test.js"; | ||
| console.log("Welcome, we're testing " + path.basename(module.filename)); | ||
| if (module.filename) { | ||
| test_name = path.basename(module.filename); | ||
| } | ||
| var inNode = function () { | ||
| var t = false; | ||
| try { | ||
| t = (process !== undefined); | ||
| } catch (err) { | ||
| } | ||
| return t; | ||
| }; | ||
| var inBrowser = function () { | ||
| var t = false; | ||
| try { | ||
| t = (window !== undefined); | ||
| } catch (err) { | ||
| } | ||
| return t; | ||
| }; | ||
| var inMongo = function () { | ||
| var t = false; | ||
| if (inBrowser() === false && | ||
| inNode() === false) { | ||
| t = true; | ||
| } | ||
| return t; | ||
| }; | ||
| console.log("Welcome, we're testing " + test_name); | ||
| console.log("First tests will be completed with out calling RunIt()"); | ||
@@ -25,10 +58,10 @@ console.log("\tDoing some pre-RunIt() tests of methods."); | ||
| assert.strictEqual(typeof harness.RunIt, "function", "Should have a a RunIt method"); | ||
| assert.strictEqual(typeof harness.platform, "string", "Should have a platform string"); | ||
| assert.strictEqual(harness.counts("tests"), 0, "Should have zero tests defined."); | ||
| assert.strictEqual(harness.counts("running"), 0, "Should have zero tests running"); | ||
| harness.push({callback: function () { | ||
| harness.push({callback: function (test_label) { | ||
| assert.strictEqual(harness.counts("running"), 1, "Should have on test running for 'Testing push()'"); | ||
| // Now complete the test. | ||
| harness.completed("Testing push()"); | ||
| harness.completed(test_label); | ||
| }, label: "Testing push()"}); | ||
@@ -38,6 +71,54 @@ assert.strictEqual(harness.counts("tests"), 1, "Should have one test group defined now."); | ||
| console.log("\tNow you should see output from RunIt()"); | ||
| if (require.main === module) { | ||
| harness.RunIt(path.basename(module.filename), 10); | ||
| } else { | ||
| exports.RunIt = harness.RunIt; | ||
| } | ||
| //Now let's create a test group we can skip | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(false, "This assert should never get run."); | ||
| harness.completed(test_label); | ||
| }, label: "SkippedThis()", targets: ["Not Supported Target"]}); | ||
| //Now let create a test group with a supported target | ||
| harness.push({callback: function (test_label) { | ||
| assert.equal(inNode(), true, | ||
| "We're in Node"); | ||
| assert.equal(inMongo(), false, | ||
| "We're not in Mongo"); | ||
| assert.equal(inBrowser(), false, | ||
| "We're not in a browser"); | ||
| harness.completed(test_label); | ||
| }, label: "NodeOnly()", targets: ["node"]}); | ||
| //Now let create a test group with a supported target | ||
| harness.push({callback: function (test_label) { | ||
| assert.equal(inNode(), false, | ||
| "We're not in Node"); | ||
| assert.equal(inMongo(), true, | ||
| "We're in Mongo"); | ||
| assert.equal(inBrowser(), false, | ||
| "We're not in a browser"); | ||
| harness.completed(test_label); | ||
| }, label: "MongoOnly()", targets: ["mongo"]}); | ||
| //Now let create a test group with a supported target | ||
| harness.push({callback: function (test_label) { | ||
| assert.equal(inNode(), false, | ||
| "We're not in Node"); | ||
| assert.equal(inMongo(), false, | ||
| "We're not in Mongo"); | ||
| assert.equal(inBrowser(), true, | ||
| "We're in a browser"); | ||
| harness.completed(test_label); | ||
| }, label: "BrowserOnly()", targets: ["browser"]}); | ||
| //Now let create a test group with a supported target | ||
| harness.push({callback: function (test_label) { | ||
| assert.ok(true, "This assert should get run."); | ||
| harness.completed(test_label); | ||
| }, label: "IncludeThis()", targets: ["mongo", "node", "browser"]}); | ||
| harness.push({callback: function (test_label) { | ||
| assert.equal(harness.skipped(), 3, | ||
| "Should have one skipped test. " + | ||
| harness.skipped()); | ||
| harness.completed(test_label); | ||
| }, label: "SkipCount()"}); | ||
| harness.RunIt(test_name, 10); |
| // | ||
| // A few tests to see if harness_test.js is working under Mongo's shell (2.2) | ||
| // | ||
| // @author: R. S. Doiel, <rsdoiel@gmail.com> | ||
| // copyright (c) 2012 all rights reserved | ||
| // | ||
| // Released under this Simplified BSD License. | ||
| // See: http://opensource.org/licenses/bsd-license.php | ||
| // | ||
| // | ||
| /*jslint devel: true, node: true, maxerr: 50, indent: 4, vars: true, sloppy: true */ | ||
| var path = require("path"), | ||
| assert = require("assert"), | ||
| harness = require("harness"), | ||
| filename = "tests/mongo-harness_test.js"; | ||
| console.log("Welcome, we're testing " + path.basename(filename)); | ||
| console.log("First tests will be completed with out calling RunIt()"); | ||
| console.log("\tDoing some pre-RunIt() tests of methods."); | ||
| assert.ok(harness, "Should have a harness object"); | ||
| assert.strictEqual(typeof harness.counts, "function", "Should have a counts method"); | ||
| assert.strictEqual(typeof harness.push, "function", "Should have a push method"); | ||
| assert.strictEqual(typeof harness.completed, "function", "Should have a a completed method"); | ||
| assert.strictEqual(typeof harness.RunIt, "function", "Should have a a RunIt method"); | ||
| assert.strictEqual(harness.counts("tests"), 0, "Should have zero tests defined."); | ||
| assert.strictEqual(harness.counts("running"), 0, "Should have zero tests running"); | ||
| harness.push({callback: function () { | ||
| assert.strictEqual(harness.counts("running"), 1, "Should have on test running for 'Testing push()'"); | ||
| // Now complete the test. | ||
| harness.completed("Testing push()"); | ||
| }, label: "Testing push()"}); | ||
| assert.strictEqual(harness.counts("tests"), 1, "Should have one test group defined now."); | ||
| console.log("Pre-RunIt() tests, OK"); | ||
| console.log("\tNow you should see output from RunIt()"); | ||
| harness.RunIt(path.basename(filename), 10); |
23746
47.32%12
20%383
36.3%171
30.53%