Skip to content

Instantly share code, notes, and snippets.

@asabaylus
Created July 6, 2012 12:26
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 asabaylus/3059886 to your computer and use it in GitHub Desktop.
Save asabaylus/3059886 to your computer and use it in GitHub Desktop.
Jasmine Junit Reporter Scafolding
/*global module:false*/
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: '<json:jquery.sessiontimeout.js.jquery.json>',
meta: {
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
},
concat: {
dist: {
src: ['<banner:meta.banner>', '<file_strip_banner:src/<%= pkg.name %>.js>'],
dest: 'dist/<%= pkg.name %>.js'
}
},
min: {
dist: {
src: ['<banner:meta.banner>', '<config:concat.dist.dest>'],
dest: 'dist/<%= pkg.name %>.min.js'
}
},
jasmine: {
all: ['test/junit.html'],
junit: {
dest: 'test-results'
}
},
lint: {
files: ['grunt.js', 'src/**/*.js', 'test/**/*.js']
},
watch: {
files: '<config:lint.files>',
tasks: 'lint qunit'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true,
devel:true,
jquery: true
},
globals: {}
},
uglify: {}
});
// Default task.
// run Jasmin in versbose mode (-v) to help with debugging
grunt.registerTask('default', 'lint jasmine concat min');
// Jamsmine BDD task
grunt.loadNpmTasks('grunt-jasmine-task');
};
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="../libs/jasmine/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="../libs/jasmine/jasmine.css">
<script type="text/javascript" src="../libs/jasmine/jasmine.js"></script>
<script type="text/javascript" src="../libs/jasmine/jasmine-html.js"></script>
<!-- For JUnit output of test results include the following link
to Lary Myer's JUnit reporter -->
<script type="text/javascript" src="../node_modules/grunt-jasmine-task/tasks/jasmine/jasmine-junit-reporter.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="spec/your_spec_file_here.js"></script>
<!-- include source files here... -->
<script type="text/javascript" src="../libs/jquery/jquery.js"></script>
<script type="text/javascript" src="../src/your_scripts_here.js"></script>
</head>
<body>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
// Specify target test results folder as below, for now
var junitReporter = new jasmine.JUnitXmlReporter('test-results/');
jasmineEnv.addReporter(junitReporter);
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
</script>
</body>
</html>
@asabaylus
Copy link
Author

The Grunt Jasmine Reporter is not quite ready yet. A few notes...

  • You must specify the target directory for the Junit output in the footer script of you test HTML page see junit.html above
  • Also you must link to the jasmine-junit-reporter.js which is provided in the grunt jasmine task, you're link may be different from my link.

Todo: junit branch.

  • JUnit task throws an error and cannot be directly invoked: grunt jasmine:junit
  • jasmine-junit-reporter.js should be automatically inject into jasmine.html

See the reporters branch of my grunt-jasmine-task fork:
https://github.com/asabaylus/grunt-jasmine-task/tree/reporters

JUnit output

To create XML compatible with Bamboo, Hudson, Crusie Control and other CI systems:

jasmine: {
      all: ['test/jasmine.html'],
      junit: {
        dest: 'test-results' 
      }  
},

junit XML will be generated under test-reports/TEST-myspec.xml

@creynders
Copy link

Probably I'm a bit dense, but I still don't get why you need to modify the grunt-jasmine-task for this? If you add the JUnit reporter in specrunner.html, isn't that enough? Or is it because you want to be able to configure the output directory for JUnit in your gruntfile?

@asabaylus
Copy link
Author

Its a few things. I believe developers will want to specify the output directory. But also phantom-jasmine-runner.js in it present state dosn't play well with the Junit XML reporter.

For instance in the runner I "borrowed" from Larry's project, the JUnit is broken into files based on specs.

var ival = setInterval(function(){
            if (isFinished()) {
                // get the results that need to be written to disk
                var xml_results = getXmlResults(page, resultsKey),
                    output;
                for (var filename in xml_results) {
                    if (xml_results.hasOwnProperty(filename) && (output = xml_results[filename]) && typeof(output) === "string") {
                        fs.write(filename, output, "w");
                    }
                }

                ...

                sendMessage( ['done'] );
            }
        }, 100);

Since phantom.js is new to me I was happy to just add some of the functions from your runner JS to Larry Myers' runner

I ended up renaming tasks/jasmine/phantom-jasmine-runner.js → tasks/jasmine/phantom-console-runner.js
then added a tweaked version Larry's runner as tasks/jasmine/phantom-junit-runner.js
This got it working even if not in an ideal state.

Here's a link to compare my reporters branch with your master
https://github.com/asabaylus/grunt-jasmine-task/compare/master...reporters

I'd be happy for any help or advice. Thanks

@creynders
Copy link

Ok, I'll need to take a longer look on this then. Don't know when I'll have the time though. I created an issue for this: https://github.com/creynders/grunt-jasmine-task/issues/10

However, 99.9% certainly I won't be integrating this straight into the grunt-jasmine-task, since I want to keep its dependencies restricted to jasmine alone. It'll have to become a separate plugin, I think.

@cdcrews
Copy link

cdcrews commented Aug 1, 2013

I know I'm a year late, but I'm trying to get this to work and can't seem to. I really need the junit xml output. Running grunt jasmine gives me the following error:
"path.existsSync is now called fs.existsSync.
Running "jasmine:junit" (jasmine) task
An error occurred while processing a template (Cannot call method 'indexOf' of undefined).
Use --force to continue. "

Any idea why if I have my files formatted as above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment