Skip to content

Commit

Permalink
Modifying files from jasmine-node to reduce to only the async-callback
Browse files Browse the repository at this point in the history
modifiers and the terminal reporters.
  • Loading branch information
juliemr committed Jul 10, 2013
0 parents commit 6070c33
Show file tree
Hide file tree
Showing 20 changed files with 4,516 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 2013 Julie Ralph

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

20 changes: 20 additions & 0 deletions README.md
@@ -0,0 +1,20 @@
minijasminenode
======

Based on Jasmine-Node, but minus the fancy stuff.
This node.js module makes the wonderful Pivotal Lab's jasmine
(http://github.com/pivotal/jasmine) spec framework available in
node.js.

jasmine
-------

Version 1.3.1 of Jasmine is currently included with node-jasmine.

Currently in-progress.

To run the tests
----------------
`./specs.sh`

This will run passing tests as well as show examples of how failures look. To run only passing tests, use `npm test` or `./bin/minijn spec/*_spec.js`
6 changes: 6 additions & 0 deletions bin/minijn
@@ -0,0 +1,6 @@
#!/usr/bin/env node

if( !process.env.NODE_ENV ) process.env.NODE_ENV = 'test';

var path = require('path');
require(path.join(__dirname,'../lib/cli.js'));
61 changes: 61 additions & 0 deletions lib/async-callback.js
@@ -0,0 +1,61 @@
(function() {
var withoutAsync = {};

["it", "beforeEach", "afterEach"].forEach(function(jasmineFunction) {
withoutAsync[jasmineFunction] = jasmine.Env.prototype[jasmineFunction];
return jasmine.Env.prototype[jasmineFunction] = function() {
var args = Array.prototype.slice.call(arguments, 0);
var timeout = null;
if (isLastArgumentATimeout(args)) {
timeout = args.pop();
// The changes to the jasmine test runner causes undef to be passed when
// calling all it()'s now. If the last argument isn't a timeout and the
// last argument IS undefined, let's just pop it off. Since out of bounds
// items are undefined anyways, *hopefully* removing an undef item won't
// hurt.
} else if (args[args.length-1] == undefined) {
args.pop();
}
if (isLastArgumentAnAsyncSpecFunction(args))
{
var specFunction = args.pop();
args.push(function() {
return asyncSpec(specFunction, this, timeout);
});
}
return withoutAsync[jasmineFunction].apply(this, args);
};
});

function isLastArgumentATimeout(args)
{
return args.length > 0 && (typeof args[args.length-1]) === "number";
}

function isLastArgumentAnAsyncSpecFunction(args)
{
return args.length > 0 && (typeof args[args.length-1]) === "function" && args[args.length-1].length > 0;
}

function asyncSpec(specFunction, spec, timeout) {
if (timeout == null) timeout = jasmine.DEFAULT_TIMEOUT_INTERVAL || 1000;
var done = false;
spec.runs(function() {
try {
return specFunction.call(spec, function(error) {
done = true;
if (error != null) return spec.fail(error);
});
} catch (e) {
done = true;
throw e;
}
});
return spec.waitsFor(function() {
if (done === true) {
return true;
}
}, "spec to complete", timeout);
};

}).call(this);
145 changes: 145 additions & 0 deletions lib/cli.js
@@ -0,0 +1,145 @@
var util = require('util'),
path = require('path'),
fs = require('fs');

var minijasminelib = require('./index');
/**
* A super simple wrapper around minijasminelib.executeSpecs()
*/

var forceExit = false;
var captureExceptions = false;

var onComplete = function(runner, log) {
util.print('\n');
if (runner.results().failedCount == 0) {
exitCode = 0;
} else {
exitCode = 1;
}
if (forceExit) {
process.exit(exitCode);
}
};

var options = {
tests: [],
onComplete: onComplete,
isVerbose: false,
showColors: true,
junitreport: {
report: false,
savePath: './reports/',
useDotNotation: true,
consolidate: true
},
includeStackTrace: true
};


var args = process.argv.slice(2);

while(args.length) {
var arg = args.shift();

switch(arg)
{
case '--color':
options.showColors = true;
break;
case '--noColor':
case '--nocolor':
options.showColors = false;
break;
case '--verbose':
options.isVerbose = true;
break;
case '--junitreport':
options.junitreport.report = true;
break;
case '--junitoutput':
options.junitreport.savePath = args.shift();
break;
case '--forceexit':
forceExit = true;
break;
case '--captureExceptions':
captureExceptions = true;
break;
case '--noStack':
options.includeStackTrace = false;
break;
case '--config':
var configKey = args.shift();
var configValue = args.shift();
process.env[configKey]=configValue;
break;
case '-h':
case '--help':
help();
default:
if (arg.match(/^--params=.*/)) {
break;
}
if (arg.match(/^-/)) {
help();
}
if (arg.match(/^\/.*/)) {
options.tests.push(arg);
} else {
options.tests.push(path.join(process.cwd(), arg));
}
break;
}
}

if (options.tests.length === 0) {
help();
} else {
// Check to see if all our files exist
for (var i = 0; i < options.tests.length; i++) {
if (!fs.existsSync(options.tests[i])) {
console.log("File: " + options.tests[i] + " is missing.");
return;
}
}
}

var exitCode = 0;

if (captureExceptions) {
process.on('uncaughtException', function(e) {
console.error(e.stack || e);
exitCode = 1;
process.exit(exitCode);
});
}

function onExit() {
process.removeListener("exit", onExit);
process.exit(exitCode);
}
process.on("exit", onExit);

minijasminelib.executeSpecs(options);


function help(){
util.print([
'USAGE: jasmine-node [--color|--noColor] [--verbose] test1 test2'
, ''
, 'Options:'
, ' --color - use color coding for output'
, ' --noColor - do not use color coding for output'
, ' --verbose - print extra information per each test run'
, ' --junitreport - export tests results as junitreport xml format'
, ' --junitoutput - defines the output folder for junitreport files'
, ' --forceexit - force exit once tests complete'
, ' --captureExceptions- listen to global exceptions, report them and exit (interferes with Domains)'
, ' --noStack - suppress the stack trace generated from a test failure'
, ' -h, --help - display this help and exit'
, ''
].join("\n"));

process.exit(-1);
}
103 changes: 103 additions & 0 deletions lib/index.js
@@ -0,0 +1,103 @@
var fs = require('fs');
var mkdirp = require('mkdirp');
var util = require('util');
var path = require('path');

// Put jasmine in the global context, this is somewhat like running in a
// browser where every file will have access to `jasmine`.
var requireJasmine = require('./jasmine-1.3.1.js');
for (var key in requireJasmine) {
global[key] = requireJasmine[key];
}

// Allow async tests by passing in a 'done' function.
require('./async-callback');

// For jasmine.JUnitXmlReporter.
require('jasmine-reporters');

// For the terminal reporters.
var nodeReporters = require('./reporter').jasmineNode;
jasmine.TerminalVerboseReporter = nodeReporters.TerminalVerboseReporter;
jasmine.TerminalReporter = nodeReporters.TerminalReporter;

function removeJasmineFrames(text) {
if (!text) {
return text;
}
var jasmineFilename = __dirname + '/jasmine-1.3.1.js';
var lines = [];
text.split(/\n/).forEach(function(line){
if (line.indexOf(jasmineFilename) == -1) {
lines.push(line);
}
});
return lines.join('\n');
}

exports.executeSpecs = function(options) {
/** An array of filenames, relative to current dir */
var tests = options['tests'];
/** A function to call on completion. function(runner, log) */
var done = options['onComplete'];
/** If true, display spec names */
var isVerbose = options['isVerbose'];
/** If true, print colors to the terminal */
var showColors = options['showColors'];
/**
* If junitreport.report == true, print a JUnit style XML report
* to junitreport.savePath.
*/
var junitreport = options['junitreport'];
/** If true, include stack traces in failures */
var includeStackTrace = options['includeStackTrace'];

// Overwriting it allows us to handle custom async specs
it = function(desc, func, timeout) {
return jasmine.getEnv().it(desc, func, timeout);
}
var colors = showColors || false,
jasmineEnv = jasmine.getEnv();

if (junitreport && junitreport.report) {
var existsSync = fs.existsSync || path.existsSync;
if (!existsSync(junitreport.savePath)) {
util.puts('creating junit xml report save path: ' + junitreport.savePath);
mkdirp.sync(junitreport.savePath, "0755");
}
jasmineEnv.addReporter(new jasmine.JUnitXmlReporter(
junitreport.savePath,
junitreport.consolidate,
junitreport.useDotNotation));
}

if (isVerbose) {
jasmineEnv.addReporter(new jasmine.TerminalVerboseReporter({
print: util.print,
color: showColors,
onComplete: done,
stackFilter: removeJasmineFrames}));
} else {
jasmineEnv.addReporter(new jasmine.TerminalReporter({
print: util.print,
color: showColors,
includeStackTrace: includeStackTrace,
onComplete: done,
stackFilter: removeJasmineFrames}));
}

var specFiles = tests; // TODO(julie) This should accept patterns.

for (var i = 0, len = specFiles.length; i < len; ++i) {
var filename = specFiles[i];
// Catch exceptions in loading the spec files.
try {
require(filename);
} catch (e) {
console.log("Exception loading: " + filename);
console.log(e);
}
}

jasmineEnv.execute();
};

0 comments on commit 6070c33

Please sign in to comment.