Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for before

  • Loading branch information...
commit ed1094554505deb5c40ccec9f3ec946f043247dd 1 parent 650ab27
@joshuaclayton authored
Showing with 87 additions and 3 deletions.
  1. +20 −0 README.markdown
  2. +15 −3 specit.js
  3. +52 −0 specit.tests.js
View
20 README.markdown
@@ -9,6 +9,12 @@ I don't have the patience to write my own JS testing framework so I figured I'd
## Example
describe("SpecIt", function() {
+ var john;
+
+ before(function() {
+ john = {name: "John Doe", age: 26};
+ });
+
it("should match on inclusion", function() {
[1, 2].should(include, 1, 2);
({one: 1, two: 2}).should(include, "one");
@@ -21,6 +27,16 @@ I don't have the patience to write my own JS testing framework so I figured I'd
true.should(beA, Boolean);
({}).should(beAn, Object);
});
+
+ it("allows overriding variables", function() {
+ john = {};
+ john.shouldNot(include, "name");
+ });
+
+ it("should use before blocks", function() {
+ john.name.should(eql, "John Doe");
+ john.age.should(beLessThan, 30);
+ });
});
## Supported Matchers
@@ -38,6 +54,10 @@ I don't have the patience to write my own JS testing framework so I figured I'd
* beGreaterThanOrEqualTo (checks a number is greater than or equal to another)
* beOnThePage (checks that an element is present on a page)
+## Other supported features
+
+* Before callbacks
+
## What's it do?
I wrote some matchers and used QUnit's module and test methods, that's all. The test file is a great form of documentation and demonstrates everything SpecIt can do currently.
View
18 specit.js
@@ -30,9 +30,20 @@
var SpecIt = {
currentExpectation: 'should',
- describe: function(description, body) { module(description); body(); },
- it: function(description, body) { test(description, body); },
- asyncIt: function(description, body) { asyncTest(description, body); },
+ describe: function(description, body) {
+ this.currentTests = [];
+ this.currentBefore = function() {};
+ body();
+ module(description, {setup: this.currentBefore});
+ $.each(this.currentTests, function(i, currentTest) { currentTest(); });
+ },
+ it: function(description, body) {
+ currentTests.push(function() { test(description, body); });
+ },
+ asyncIt: function(description, body) {
+ currentTests.push(function() { asyncTest(description, body); });
+ },
+ before: function(callback) { this.currentBefore = callback; },
expectations: function(current) {
var expect = function(expectation, args) {
var args = $.makeArray(args);
@@ -194,4 +205,5 @@
window.describe = SpecIt.describe;
window.it = SpecIt.it;
window.asyncIt = SpecIt.asyncIt;
+ window.before = SpecIt.before;
})();
View
52 specit.tests.js
@@ -158,3 +158,55 @@ describe("SpecIt", function() {
$(".workspace").empty();
});
});
+
+var john, beforeCallbackTest, afterCallbackTest;
+
+describe("SpecIt with a before callback", function() {
+ var jane = {name: "Jane"};
+
+ before(function() {
+ beforeCallbackTest = true;
+ john = {name: "John Doe"};
+ });
+
+ it("should support before", function() {
+ ok(beforeCallbackTest);
+ equal(afterCallbackTest, undefined);
+ });
+
+ it("should run before every test", function() {
+ john.name = "Wrong name";
+ jane.age = 26;
+ });
+
+ it("should run before every test", function() {
+ equals(john.name, "John Doe");
+ });
+
+ it("should not know attributes from another before callback", function() {
+ equals(john.age, undefined);
+ });
+
+ it("should not modify attributes on a local object if untouched in before", function() {
+ equals(jane.age, 26);
+ });
+});
+
+// the john object will carry over, but the jane object will not
+describe("SpecIt with a different before callback", function() {
+ before(function() { john.age = 35; });
+
+ it("should not run other describes' before callbacks", function() {
+ john.name = "whatever";
+ equals(john.age, 35);
+ });
+
+ it("should not run other describes' before callbacks", function() {
+ equals(john.name, "whatever");
+ equals(john.age, 35);
+ });
+
+ it("should not know of other objects in a different describe", function() {
+ equals(typeof jane, "undefined");
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.