diff --git a/README.markdown b/README.markdown index 89b73fc..1d47931 100644 --- a/README.markdown +++ b/README.markdown @@ -129,20 +129,37 @@ Suites are executed in the order in which `describe()` calls are made, usually i You don't need a DOM to run your tests, but you do need a page on which to load & execute your JS. Include the `jasmine.js` file in a script tag as well as the JS file with your specs. You can also use this page for reporting. More on that in a moment. -// include example.html +Here's the example HTML file (in `jasmine/example`): + + + + + Jasmine Example + + + + + +

+ Running Jasmine Example Specs +

+
+ + + ### Reports -no reporting yet other than Runner.results, which is walkable +If a reporter exists on the Jasmine instance (named `jasmine`), it will be called when each spec, suite and the overall runner complete. If you're at the single-spec result level, you'll get a spec description, whether it passed or failed, and what the failure message was. At the suite & runner report level, you'll get the total specs run so far, the passed counts, failed counts, and a description (of the suite or runner). -#### JSON Reporter -Coming soon. - -#### HTML Reporter -Coming soon. - -#### In-line HTML Reporter -Coming soon. +There is a `Jasmine.Reporters` namespace for you to see how to handle reporting. See the file `json_reporter.js`, which takes the results objects and turns them into JSON strings, for two examples of how to make the results callbacks work for you. ### Custom Matchers @@ -157,6 +174,13 @@ A Matcher has a method name, takes an expected value as it's only parameter, has Feel free to define your own matcher as needed in your code. If you'd like to add Matchers to Jasmine, please write tests. +### Limitations + +You can only have one instance of Jasmine (which is a container for a runner) running at any given time. As you can see from `bootstrap.js`, this means you have to wait until a runner is done before defining suites & specs for another runner. + +This is a bit sloppy and will be fixed at some point - but it allows for a nicer syntax when defining your specs. For now we expect this to be fine as most of the time having multiple suites is sufficient for isolating application-level code. + + Contributing and Tests ---------------------- @@ -171,17 +195,13 @@ Your contributions are welcome. Please submit tests with your pull request. * [Davis W. Frank](dwfrank@pivotallabs.com), Pivotal Labs * [Rajan Agaskar](rajan@pivotallabs.com), Pivotal Labs +## Acknowledgments +A big shout out to the various JavaScript test framework authors, especially TJ for [JSpec](http://github.com/visionmedia/jspec/tree/master) - we played with it a bit before deciding that we really needed to roll our own. + ## TODO List In no particular order: -* protect the global-ness of some variables & functions -* Suite beforeAll and afterAll functions * add a description to runs() -* suite.beforeAll and suite.afterAll -* JSON reporter -* HTML reporter -* HTML reporter (callback driven) - diff --git a/example/example.html b/example/example.html index 40265c0..6fa607a 100644 --- a/example/example.html +++ b/example/example.html @@ -13,9 +13,10 @@
diff --git a/jasmine.iws b/jasmine.iws index 70ff270..71c3b98 100644 --- a/jasmine.iws +++ b/jasmine.iws @@ -89,10 +89,10 @@ - + - + @@ -101,16 +101,16 @@ - + - + - + @@ -119,7 +119,7 @@ - + @@ -430,22 +430,22 @@ - - + + - - - + + + - - + + - - - + + + @@ -507,6 +507,23 @@ + + + + + + + + + + + + + + + + + @@ -521,19 +538,16 @@ - + - + - - - - + @@ -543,21 +557,7 @@ - - - - - - - - - - - - - - - + diff --git a/lib/jasmine.js b/lib/jasmine.js index ec220f9..6c7294b 100755 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -152,17 +152,35 @@ var queuedFunction = function(func, timeout, spec) { * Jasmine ******************************************************************************/ + +var Jasmine = {} + +Jasmine.init = function () { + var that = { + currentSpec: null, + currentSuite: null, + currentRunner: null, + execute: function () { + that.currentRunner.execute(); + } + } + return that; +} + +var jasmine = Jasmine.init(); + /* - * Matchers methods; add your own with Matchers.method() + * Jasmine.Matchers methods; add your own with Jasmine.Matchers.method() - don't forget to write a test + * */ -Matchers = function (actual, results) { +Jasmine.Matchers = function (actual, results) { this.actual = actual; this.passing_message = 'Passed.' this.results = results || nestedResults(); } -Matchers.method('report', function (result, failing_message) { +Jasmine.Matchers.method('report', function (result, failing_message) { this.results.push({ passed: result, @@ -172,12 +190,12 @@ Matchers.method('report', function (result, failing_message) { return result; }); -Matchers.method('should_equal', function (expected) { +Jasmine.Matchers.method('should_equal', function (expected) { return this.report((this.actual === expected), 'Expected ' + expected + ' but got ' + this.actual + '.'); }); -Matchers.method('should_not_equal', function (expected) { +Jasmine.Matchers.method('should_not_equal', function (expected) { return this.report((this.actual !== expected), 'Expected ' + expected + ' to not equal ' + this.actual + ', but it does.'); }); @@ -196,7 +214,7 @@ var it = function (description, func) { results: nestedResults(), expects_that: function (actual) { - return new Matchers(actual, that.results); + return new Jasmine.Matchers(actual, that.results); }, waits: function (timeout) { @@ -209,8 +227,8 @@ var it = function (description, func) { }, finishCallback: function () { - if (Jasmine.reporter) { - Jasmine.reporter.reportSpecResults(that.results); + if (jasmine.reporter) { + jasmine.reporter.reportSpecResults(that.results); } }, @@ -243,8 +261,8 @@ var it = function (description, func) { that.runs = addToQueue; - currentSuite.specs.push(that); - currentSpec = that; + jasmine.currentSuite.specs.push(that); + jasmine.currentSpec = that; if (func) { func(); @@ -255,19 +273,19 @@ var it = function (description, func) { } var runs = function (func) { - currentSpec.runs(func); + jasmine.currentSpec.runs(func); } var waits = function (timeout) { - currentSpec.waits(timeout); + jasmine.currentSpec.waits(timeout); } var beforeEach = function (beforeEach) { - currentSuite.beforeEach = beforeEach; + jasmine.currentSuite.beforeEach = beforeEach; } var afterEach = function (afterEach) { - currentSuite.afterEach = afterEach; + jasmine.currentSuite.afterEach = afterEach; } var describe = function (description, spec_definitions) { @@ -276,16 +294,16 @@ var describe = function (description, spec_definitions) { that.description = description; that.specs = that.actions; - currentSuite = that; - Jasmine.suites.push(that); + jasmine.currentSuite = that; + jasmine.currentRunner.suites.push(that); spec_definitions(); that.results.description = description; that.finishCallback = function () { - if (Jasmine.reporter) { - Jasmine.reporter.reportSuiteResults(that.results); + if (jasmine.reporter) { + jasmine.reporter.reportSuiteResults(that.results); } } @@ -299,26 +317,24 @@ var Runner = function () { that.results.description = 'All Jasmine Suites'; that.finishCallback = function () { - if (that.reporter) { - that.reporter.reportRunnerResults(that.results); + if (jasmine.reporter) { + jasmine.reporter.reportRunnerResults(that.results); } } - Jasmine = that; + jasmine.currentRunner = that; return that; } -var Jasmine = Runner(); -var currentSuite; -var currentSpec; +jasmine.currentRunner = Runner(); /* JasmineReporters.reporter * Base object that will get called whenever a Spec, Suite, or Runner is done. It is up to * descendants of this object to do something with the results (see json_reporter.js) */ -var JasmineReporters = {}; +Jasmine.Reporters = {}; -JasmineReporters.reporter = function (callbacks) { +Jasmine.Reporters.reporter = function (callbacks) { var that = { callbacks: callbacks || {}, diff --git a/lib/json_reporter.js b/lib/json_reporter.js index dd42445..18a6e96 100644 --- a/lib/json_reporter.js +++ b/lib/json_reporter.js @@ -3,8 +3,8 @@ * Basic reporter that keeps a JSON string of the most recent Spec, Suite or Runner * result. Calling application can then do whatever it wants/needs with the string; */ -JasmineReporters.JSON = function () { - var that = JasmineReporters.reporter(); +Jasmine.Reporters.JSON = function () { + var that = Jasmine.Reporters.reporter(); that.specJSON = ''; that.suiteJSON = ''; that.runnerJSON = ''; @@ -27,7 +27,7 @@ JasmineReporters.JSON = function () { return that; } -var domWriter = function (elementId) { +Jasmine.Reporters.domWriter = function (elementId) { var that = { element: document.getElementById(elementId), @@ -43,10 +43,10 @@ var domWriter = function (elementId) { return that; } -JasmineReporters.JSONtoDOM = function (elementId) { - var that = JasmineReporters.JSON(); +Jasmine.Reporters.JSONtoDOM = function (elementId) { + var that = Jasmine.Reporters.JSON(); - that.domWriter = domWriter(elementId); + that.domWriter = Jasmine.Reporters.domWriter(elementId); var writeRunnerResults = function (results) { that.domWriter.write(Object.toJSON(results)); @@ -56,18 +56,3 @@ JasmineReporters.JSONtoDOM = function (elementId) { return that; } - - -//JasmineReporters.IncrementalJSON = function (elementId) { -// var that = JasmineReporters.reporter(elementId); -// -// that.reportSpecResults = function (results) { -// that.output = Object.toJSON(results); -// if (that.element) { -// that.element.innerHTML += that.output; -// } -// } -// -// return that; -//} - diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml deleted file mode 100755 index c1f155a..0000000 --- a/nbproject/private/private.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/nbproject/project.properties b/nbproject/project.properties deleted file mode 100755 index caab57d..0000000 --- a/nbproject/project.properties +++ /dev/null @@ -1,3 +0,0 @@ -main.file= -platform.active=Ruby -source.encoding=UTF-8 diff --git a/nbproject/project.xml b/nbproject/project.xml deleted file mode 100755 index 5b7dc0a..0000000 --- a/nbproject/project.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - org.netbeans.modules.ruby.rubyproject - - - jasmine - - - - - diff --git a/test/bootstrap.js b/test/bootstrap.js index baa9fab..60c5dee 100755 --- a/test/bootstrap.js +++ b/test/bootstrap.js @@ -42,19 +42,19 @@ var reporter = function () { }(); var testMatchersComparisons = function () { - var expected = new Matchers(true); + var expected = new Jasmine.Matchers(true); reporter.test(expected.should_equal(true), 'expects_that(true).should_equal(true) returned false'); - expected = new Matchers(false); + expected = new Jasmine.Matchers(false); reporter.test(!(expected.should_equal(true)), 'expects_that(true).should_equal(true) returned true'); - expected = new Matchers(true); + expected = new Jasmine.Matchers(true); reporter.test(expected.should_not_equal(false), 'expects_that(true).should_not_equal(false) retruned false'); - expected = new Matchers(true); + expected = new Jasmine.Matchers(true); reporter.test(!(expected.should_not_equal(true)), 'expects_that(true).should_not_equal(false) retruned true'); } @@ -62,7 +62,7 @@ var testMatchersComparisons = function () { var testMatchersReporting = function () { var results = []; - var expected = new Matchers(true, results); + var expected = new Jasmine.Matchers(true, results); expected.should_equal(true); expected.should_equal(false); @@ -76,14 +76,14 @@ var testMatchersReporting = function () { "Second spec did pass"); results = []; - expected = new Matchers(false, results); + expected = new Jasmine.Matchers(false, results); expected.should_equal(true); reporter.test((results[0].message == 'Expected true but got false.'), "Failed expectation didn't test the failure message"); results = []; - expected = new Matchers(true, results); + expected = new Jasmine.Matchers(true, results); expected.should_equal(true); reporter.test((results[0].message == 'Passed.'), @@ -502,7 +502,7 @@ var testRunner = function() { it('should be a test'); }); reporter.test((runner.suites.length === 1), - "Runner expected one suite"); + "Runner expected one suite, got " + runner.suites.length); runner = Runner(); describe('one suite description', function () { @@ -535,7 +535,7 @@ var testRunner = function() { setTimeout(function () { reporter.test((runner.suites.length === 2), - "Runner expected two suites"); + "Runner expected two suites, got " + runner.suites.length); reporter.test((runner.suites[0].specs[0].results.results[0].passed === true), "Runner should have run specs in first suite"); reporter.test((runner.suites[1].specs[0].results.results[0].passed === false), @@ -649,11 +649,9 @@ var testResults = function () { } var testReporterWithCallbacks = function () { - var foo = 0; - var bar = 0; - var baz = 0; - + jasmine = Jasmine.init(); var runner = Runner(); + describe('Suite for JSON Reporter with Callbacks', function () { it('should be a test', function() { runs(function () { @@ -675,31 +673,24 @@ var testReporterWithCallbacks = function () { }); + var foo = 0; + var bar = 0; + var baz = 0; - var specCallback = function (results) { - foo++; - } + var specCallback = function (results) { foo++; } + var suiteCallback = function (results) { bar++; } + var runnerCallback = function (results) { baz++; } - var suiteCallback = function (results) { - bar++; - } - - var runnerCallback = function (results) { - baz++; - } - - callbackFunctions = { + jasmine.reporter = Jasmine.Reporters.reporter({ specCallback: specCallback, suiteCallback: suiteCallback, runnerCallback: runnerCallback - } - - runner.reporter = JasmineReporters.reporter(callbackFunctions); + }); runner.execute(); setTimeout(function() { reporter.test((foo === 3), - 'foo was expected to be 1, was ' + foo); + 'foo was expected to be 3, was ' + foo); reporter.test((bar === 2), 'bar was expected to be 2, was ' + bar); reporter.test((baz === 1), @@ -709,7 +700,9 @@ var testReporterWithCallbacks = function () { } var testJSONReporter = function () { + jasmine = Jasmine.init(); var runner = Runner(); + describe('Suite for JSON Reporter, NO DOM', function () { it('should be a test', function() { runs(function () { @@ -718,7 +711,7 @@ var testJSONReporter = function () { }); }); - runner.reporter = JasmineReporters.JSON(); + jasmine.reporter = Jasmine.Reporters.JSON(); runner.execute(); @@ -727,17 +720,17 @@ var testJSONReporter = function () { var expectedSuiteJSON = '{"totalCount": 1, "passedCount": 1, "failedCount": 0, "results": [{"totalCount": 1, "passedCount": 1, "failedCount": 0, "results": [{"passed": true, "message": "Passed."}], "description": "should be a test"}], "description": "Suite for JSON Reporter, NO DOM"}'; var expectedRunnerJSON = '{"totalCount": 1, "passedCount": 1, "failedCount": 0, "results": [{"totalCount": 1, "passedCount": 1, "failedCount": 0, "results": [{"totalCount": 1, "passedCount": 1, "failedCount": 0, "results": [{"passed": true, "message": "Passed."}], "description": "should be a test"}], "description": "Suite for JSON Reporter, NO DOM"}], "description": "All Jasmine Suites"}'; - specJSON = runner.reporter.specJSON; + specJSON = jasmine.reporter.specJSON; reporter.test((specJSON === expectedSpecJSON), 'JSON Reporter does not have the expected Spec results report.
Expected:
' + expectedSpecJSON + '
Got:
' + specJSON); - suiteJSON = runner.reporter.suiteJSON; + suiteJSON = jasmine.reporter.suiteJSON; reporter.test((suiteJSON === expectedSuiteJSON), 'JSON Reporter does not have the expected Suite results report.
Expected:
' + expectedSuiteJSON + '
Got:
' + suiteJSON); - runnerJSON = runner.reporter.runnerJSON; + runnerJSON = jasmine.reporter.runnerJSON; reporter.test((runnerJSON === expectedRunnerJSON), 'JSON Reporter does not have the expected Runner results report.
Expected:
' + expectedRunnerJSON + '
Got:
' + runnerJSON); @@ -745,7 +738,9 @@ var testJSONReporter = function () { } var testJSONReporterWithDOM = function () { + jasmine = Jasmine.init(); var runner = Runner(); + describe('Suite for JSON Reporter/DOM', function () { it('should be a test', function() { runs(function () { @@ -754,7 +749,7 @@ var testJSONReporterWithDOM = function () { }); }); - runner.reporter = JasmineReporters.JSONtoDOM('json_reporter_results'); + jasmine.reporter = Jasmine.Reporters.JSONtoDOM('json_reporter_results'); runner.execute(); setTimeout(function() { @@ -787,15 +782,15 @@ var runTests = function () { }, 2000); setTimeout(function () { testJSONReporter(); - }, 2750); + }, 3500); setTimeout(function () { testJSONReporterWithDOM(); - }, 3000); + }, 5000); setTimeout(function() { $('spinner').hide(); reporter.summary(); - }, 4500); + }, 6000); }