Skip to content

Instantly share code, notes, and snippets.

@Integral
Created June 9, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integral/53b918bd04e86cb741bdb956f1040a8e to your computer and use it in GitHub Desktop.
Save Integral/53b918bd04e86cb741bdb956f1040a8e to your computer and use it in GitHub Desktop.
'use strict';
var tape = require('tape');
var inBrowser = require('../util/inBrowser');
var platform = require('../util/platform');
var DefaultDOMElement = require('../ui/DefaultDOMElement');
var nextTick = process.nextTick;
var harness = tape.createHarness();
var results = harness._results;
harness.runAllTests = function() {
var i = 0;
function next() {
while (i < results.tests.length) {
var t = results.tests[i++];
t.once('end', function(){ nextTick(next); });
t.run();
}
}
nextTick(next);
};
harness.getTests = function() {
return results.tests || [];
};
harness.module = function(moduleName) {
var tapeish = function() {
var args = getTestArgs.apply(null, arguments);
var name = moduleName + ": " + args.name;
var t = harness(name, args.opts, args.cb);
t.moduleName = moduleName;
return t;
};
return _withExtensions(tapeish, true);
};
_withExtensions(harness);
/*
Helpers
*/
// copied from tape/lib/test.js
function getTestArgs() {
var name = '(anonymous)';
var opts = {};
var cb;
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
var t = typeof arg;
if (t === 'string') {
name = arg;
}
else if (t === 'object') {
opts = arg || opts;
}
else if (t === 'function') {
cb = arg;
}
}
return { name: name, opts: opts, cb: cb };
}
function _withExtensions(tapeish, module) {
function _withBeforeAndAfter(args) {
var _before = args.opts.before;
var _after = args.opts.after;
return tapeish(args.name, args.opts, function (t) {
if(_before) _before(t);
args.cb(t);
if(_after) _after(t);
});
}
tapeish.UI = function() {
var args = getTestArgs.apply(null, arguments);
if (!inBrowser) {
args.opts.skip = true;
} else {
var _before = args.opts.before;
var _after = args.opts.after;
args.opts.before = function(t) {
_setupUI(t);
if(_before) _before(t);
};
args.opts.after = function(t) {
if(_after) _after(t);
_teardownUI(t);
};
}
return _withBeforeAndAfter(args);
};
tapeish.FF = function() {
var args = getTestArgs.apply(null, arguments);
if (!inBrowser || !platform.isFF) {
args.opts.skip = true;
}
return _withBeforeAndAfter(args);
};
tapeish.WK = function() {
var args = getTestArgs.apply(null, arguments);
if (!inBrowser || !platform.isWebKit) {
args.opts.skip = true;
}
return _withBeforeAndAfter(args);
};
if(!module) {
tapeish.module = function(name) {
return function(description, fn) {
tape(name+': '+description, function (t) {
fn(t);
});
}
}
}
return tapeish;
}
function _setupUI(t) {
var fixtureElement = window.document.querySelector('#qunit-fixture');
if (!fixtureElement) {
fixtureElement = window.document.createElement('div');
fixtureElement.id = "qunit-fixture";
window.document.querySelector('body').appendChild(fixtureElement);
}
var sandboxEl = window.document.createElement('div');
sandboxEl.id = 'sandbox-'+t.test.id;
fixtureElement.appendChild(sandboxEl);
t.sandbox = DefaultDOMElement.wrapNativeElement(sandboxEl);
}
function _teardownUI(t) {
var sandbox = t.sandbox;
if (sandbox) {
sandbox.remove();
}
}
if (!inBrowser) {
harness = _withExtensions(tape, false);
}
module.exports = harness;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment