Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
max_line_length = 80
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
node_modules

npm-debug.log
7 changes: 7 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"undef": true,
"unused": true,
"eqeqeq": true,
"node": true,
"jasmine": true
}
142 changes: 101 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,113 @@
function install(globalObject) {
if (!globalObject.jasmine) {
throw new Error(
'It looks like you\'re trying to install jasmine-pit before installing ' +
'jasmine! Make sure there is a `jasmine` property on the global object ' +
'(window/global/etc) before calling install().'
);
/* global define */

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function () {
return (root.jasminePit = factory());
});
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.jasminePit = factory();
}
}(this, function () {
function assert(desc, test) {
if (!test) {
throw new Error(desc);
}
}

var jasmine = globalObject.jasmine;
function assertPromise(promise) {
assert('pit() tests should return a promise',
promise && typeof promise.then === 'function');
}

globalObject.pit = function pit(specName, promiseBuilder) {
return jasmine.getEnv().it(specName, runPitTest.bind(null, promiseBuilder));
};
function wrapPromiseLegacyJasmine(env, promiseFn) {
return function () {
var error = null;
var isFinished = false;

globalObject.pit.only = function pitOnly(specName, promiseBuilder) {
return jasmine.getEnv().it.only(specName, runPitTest.bind(null, promiseBuilder));
};
var currentSpec = env.currentSpec;

globalObject.xpit = function xpit(specName, promiseBuilder) {
return jasmine.getEnv().xit(specName, runPitTest.bind(null, promiseBuilder));
};
currentSpec.runs(function () {
try {
var promise = promiseFn.call(currentSpec);

function runPitTest(promiseBuilder) {
var jasmineEnv = jasmine.getEnv();
var spec = jasmineEnv.currentSpec;
var isFinished = false;
var error = null;
assertPromise(promise);

spec.runs(function() {
try {
var promise = promiseBuilder.call(spec);
if (!promise || !promise.then) {
throw new Error('pit() tests should return a promise');
promise.then(function () {
isFinished = true;
})['catch'](function (err) {
error = err;
isFinished = true;
});
} catch (e) {
error = e;
isFinished = true;
}
});

promise.then(function() {
isFinished = true;
})['catch'](function(err) {
error = err; isFinished = true;
});
} catch (e) {
error = e;
isFinished = true;
currentSpec.waitsFor(function () {
return isFinished;
});

currentSpec.runs(function () {
if (error) throw error;
});
};
}

function wrapPromiseJasmine(promiseFn) {
return function (done) {
var promise = promiseFn();
var fail = done.fail ? done.fail : function (err) { throw err; };

try {
assertPromise(promise);
} catch (err) {
fail(err);
}
});

spec.waitsFor(function() { return isFinished; });
spec.runs(function() { if (error) throw error; });
};
}
promise.then(function (res) {
done(res);
})['catch'](function (err) {
fail(err);
});
};
}

function install(globalObject) {
var jasmine = globalObject.jasmine;

exports.install = install;
assert(
'It looks like you\'re trying to install jasmine-pit before installing ' +
'jasmine! Make sure there is a `jasmine` property on the global object ' +
'(window/global/etc) before calling install().', jasmine);

var env = jasmine.getEnv();
var isLegacyJasmine = jasmine.version_ && jasmine.version_.major === 1;
var wrapFn = isLegacyJasmine ? wrapPromiseLegacyJasmine.bind(null, env) : wrapPromiseJasmine;

globalObject.pit = function pit(specName, promiseFn) {
return env.it(specName, wrapFn(promiseFn));
};

globalObject.xpit = function xpit(specName, promiseFn) {
return env.xit(specName, wrapFn(promiseFn));
};

if (isLegacyJasmine) {
globalObject.pit.only = function pitOnly(specName, promiseFn) {
return env.it.only(specName, wrapFn(promiseFn));
};
} else {
globalObject.fpit = function fpit(desc, promiseFn) {
return env.fit(desc, wrapFn(promiseFn));
};
}
}

return {
install: install
};
}));
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,18 @@
"type": "git",
"url": "[email protected]:jeffmo/jasmine-pit.git"
},
"license": "MIT"
"scripts": {
"test-on-jasmine-1.x": "minijasminenode ./spec/jasmine-pit-spec.js ./spec/jasmine-1.x-specific-spec.js",
"test-on-jasmine-2.x": "minijasminenode2 ./spec/jasmine-pit-spec.js ./spec/jasmine-2.x-specific-spec.js",
"test": "npm run test-on-jasmine-1.x && npm run test-on-jasmine-2.x"
},
"license": "MIT",
"devDependencies": {
"minijasminenode": "^1.1.1",
"minijasminenode2": "^1.0.0",
"rsvp": "^3.0.18"
},
"dependencies": {
"jasmine-only": "^0.1.1"
}
}
9 changes: 9 additions & 0 deletions spec/jasmine-1.x-specific-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var jasminePit = require('../index');

jasminePit.install(global);

describe('jasminePit should work on Jasmine 1.x: ', function () {
it('should have `pit.only` on globalObject', function () {
expect(global.pit.only).toBeDefined();
});
});
9 changes: 9 additions & 0 deletions spec/jasmine-2.x-specific-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var jasminePit = require('../index');

jasminePit.install(global);

describe('jasminePit should work on Jasmine 2.x: ', function () {
it('should have `fpit` on globalObject', function () {
expect(global.fpit).toBeDefined();
});
});
27 changes: 27 additions & 0 deletions spec/jasmine-pit-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* global pit, xpit */

var RSVP = require('rsvp');
var jasminePit = require('../index');

jasminePit.install(global);

describe('jasminePit: ', function () {
it('should have `pit`, `xpit` on globalObject', function () {
expect(global.pit).toBeDefined();
expect(global.xpit).toBeDefined();
});

pit('should work with Promise', function () {
return RSVP.resolve(42).then(function(val) {
expect(val).toBe(42);
});
});

pit('should fail if Promise fails', function () {
return RSVP.reject(null);
});

xpit('should skip tests with `xpit`', function () {
throw Error; // should not happen
});
});