Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tap reporter (prototype yet - please come and discuss) #972

Closed
wants to merge 1 commit into from
Closed
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
77 changes: 77 additions & 0 deletions reporter/tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(function() {

function reporter( rep ) {
if ( rep === "tap" ) {
tapReporter();
}
};

reporter.print = function( val ) {
console.log( val );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this a bit more generic? Like:

console.log.apply(console, arguments);

I guess this depends on the circumstances under which print will be used.

};

QUnit.reporter = reporter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely doesn't belong in reporter/tap.js, and I still have issues with QUnit.reporter( string ).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely doesn't belong in reporter/tap.js

definitely, I agree.

I'll review this, crossing with the efforts on the js-reporter and provide some good rebased thing.


function tapReporter() {
var testCount = 0;
var failed = 0;

QUnit.begin( function() {
QUnit.reporter.print( "TAP version 13" );
Copy link
Member

@gibson042 gibson042 May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QUnit.reporter.print also feels a bit too global.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted something I can patch/replace/inspect. Do you have any other suggestion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be specific to the TAP reporter, or even better to a TAP reporter instance. Testing code code then boil down to something like QUnit.reporter( TAPReporterFactory({ print: captureOutput }) ) (modulo construction/configuration/registration bikeshedding).

} );

QUnit.done( function( details ) {
var output = [
"",
"1.." + testCount,
"# tests " + testCount,
"# pass " + ( testCount - failed )
];

QUnit.reporter.print( output.join( "\n" ) );
} );

QUnit.moduleStart( function( details ) {
QUnit.reporter.print( "# module: " + details.name );
} );

QUnit.testDone( function( details ) {
var assertion;
var failedMessage;
var output;
var i = 0;

testCount++;

output = "ok " + testCount + " - ";

if ( details.skipped ) {
output += "# SKIP " + details.name;
} else if ( details.failed == 0 ) {
output += details.name;
} else {
failed++;
output = "not " + output + details.name;

for ( i = 0; i < details.assertions.length; i++ ) {
assertion = details.assertions[ i ];
if ( assertion.result ) {
continue;
}
failedMessage = [
"",
" ---",
" message: '" + assertion.message + "'",
" severity: fail",
" ..."
];

output += failedMessage.join( "\n" );
}
}

QUnit.reporter.print( output );
} );
};

})();
14 changes: 14 additions & 0 deletions test/tap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Test Suite - Tap Reporter</title>
<link rel="stylesheet" href="../dist/qunit.css">
<script src="../dist/qunit.js"></script>
<script src="../reporter/tap.js"></script>
<script src="tap.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>
97 changes: 97 additions & 0 deletions test/tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* globals console:true */

var storedResults = [];
var originalPrint = QUnit.reporter.print;
QUnit.reporter.print = function( output ) {
storedResults.push( output );
};

QUnit.config.reorder = false;

QUnit.reporter( "tap" );

QUnit.module( "print function" );

QUnit[ console && console.log ? "test" : "skip" ]( "prints to console.log", function( assert ) {
var results = [];

// Duck punch console.log
this.originalLog = console.log;
console.log = function( arg ) {
results.push( arg );
};

originalPrint( "foo", "bar" );
originalPrint( "baz" );

assert.strictEqual( results.length, 2 );

assert.strictEqual( results[ 0 ], "foo", "calls console.log fn" );
assert.strictEqual( results[ 1 ], "baz" );

console.log = this.originalLog;
} );

QUnit.module( "tap reporter - samples" );

QUnit.test( "this test will pass", function( assert ) {
assert.ok( true );
assert.equal( 1, 1 );
} );

QUnit.test( "async tests that pass", function( assert ) {
assert.ok( true );
var done = assert.async();

setTimeout( function() {
assert.ok( true );
done();
}, 13 );
} );

QUnit.module( "tap reporter - samples #2" );

QUnit.test( "this test will pass", function( assert ) {
assert.ok( true );
assert.equal( 1, 1 );
} );

QUnit.test( "async tests that pass", function( assert ) {
assert.ok( true );
var done = assert.async();

setTimeout( function() {
assert.ok( true );
done();
}, 13 );
} );

var once = true;
var expected = [
"TAP version 13",
"# module: print function",
"ok 1 - prints to console.log",
"# module: tap reporter - samples",
"ok 2 - this test will pass",
"ok 3 - async tests that pass",
"# module: tap reporter - samples #2",
"ok 4 - this test will pass",
"ok 5 - async tests that pass",
"",
"1..5",
"# tests 5",
"# pass 5"
].join( "\n" );

QUnit.done( function() {
if ( once ) {
once = false;
QUnit.module( "tap reporter" );
QUnit.test( "final tests", function( assert ) {

// Remove the current module
storedResults.pop();
assert.equal( storedResults.join( "\n" ), expected );
} );
}
} );