Skip to content

Commit

Permalink
Core: Add assertion event
Browse files Browse the repository at this point in the history
Implements the assertion event according to js-reporters standard
  • Loading branch information
Trent Willis authored and leobalter committed Feb 7, 2017
1 parent 59e9f5a commit 721f380
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gruntfile.js
Expand Up @@ -116,6 +116,7 @@ grunt.initConfig( {
"test/reorderError1.html",
"test/reorderError2.html",
"test/callbacks.html",
"test/events.html",
"test/logs.html",
"test/setTimeout.html",
"test/amd.html",
Expand Down Expand Up @@ -145,6 +146,7 @@ grunt.initConfig( {
"test/main/modules",
"test/main/deepEqual",
"test/main/stack",
"test/events",
"test/only",
"test/setTimeout",
"test/main/dump",
Expand Down
22 changes: 20 additions & 2 deletions src/test.js
Expand Up @@ -2,6 +2,7 @@ import global from "global";

import { internalState, process, begin } from "./core";
import { setTimeout, clearTimeout } from "./globals";
import { emit } from "./events";
import Assert from "./assert";

import config from "./core/config";
Expand Down Expand Up @@ -356,7 +357,7 @@ Test.prototype = {
}
}

runLoggingCallbacks( "log", details );
this.logAssertion( details );

this.assertions.push( {
result: !!resultInfo.result,
Expand Down Expand Up @@ -384,14 +385,31 @@ Test.prototype = {
details.source = source;
}

runLoggingCallbacks( "log", details );
this.logAssertion( details );

this.assertions.push( {
result: false,
message: message
} );
},

/**
* Log assertion details using both the old QUnit.log interface and
* QUnit.on( "assertion" ) interface.
*
* @private
*/
logAssertion( details ) {
runLoggingCallbacks( "log", details );
emit( "assertion", {
passed: details.result,
actual: details.actual,
expected: details.expected,
message: details.message,
stack: details.source
} );
},

resolvePromise: function( promise, phase ) {
var then, resume, message,
test = this;
Expand Down
14 changes: 14 additions & 0 deletions test/events.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Events Test Suite</title>
<link rel="stylesheet" href="../dist/qunit.css">
<script src="../dist/qunit.js"></script>
<script src="events.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</body>
</html>
56 changes: 56 additions & 0 deletions test/events.js
@@ -0,0 +1,56 @@
/**
* This test file verifies the execution order and contents of events emitted
* by QUnit. They are expected to adhere to the js-reporters standard.
*/

var invokedHooks = [],
invokedHookDetails = {},
done = false;

function callback( name ) {
invokedHookDetails[ name ] = [];

return function( details ) {
if ( done ) {
return;
}

invokedHooks.push( name );
invokedHookDetails[ name ].push( details );
};
}

QUnit.on( "assertion", callback( "assertion1" ) );
QUnit.on( "assertion", callback( "assertion2" ) );

// After all the tests run, we verify the events were fired in the correct order
// with the correct data
QUnit.done( function() {
if ( done ) {
return;
}

done = true;

QUnit.module( "Events" );
QUnit.test( "verify callback order and data", function( assert ) {
assert.deepEqual( invokedHooks, [
"assertion1",
"assertion2"
], "event callbacks are called in correct order" );

assert.deepEqual( invokedHookDetails.assertion1[ 0 ], {
passed: true,
actual: true,
expected: true,
message: "passing assertion",
stack: undefined
}, "assertion callback data is correct" );
} );
} );

QUnit.module( "Events", function() {
QUnit.test( "test1", function( assert ) {
assert.ok( true, "passing assertion" );
} );
} );

0 comments on commit 721f380

Please sign in to comment.