Skip to content

Commit

Permalink
Assert: Fail test when using step inside a test without verifySteps
Browse files Browse the repository at this point in the history
  • Loading branch information
trentmwillis committed Jan 17, 2018
1 parent 89dcd90 commit 7385942
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ Test.prototype = {

finish: function() {
config.current = this;

if ( this.steps.length ) {
const stepsList = this.steps.join( ", " );
this.pushFailure( "Expected assert.verifySteps() to be called before end of test " +
`after using assert.step(). Unverified steps: ${stepsList}`, this.stack );
}

if ( config.requireExpects && this.expected === null ) {
this.pushFailure( "Expected number of assertions to be defined, but expect() was " +
"not called.", this.stack );
Expand Down
24 changes: 21 additions & 3 deletions test/main/assert/step.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
QUnit.module( "assert.step" );

QUnit.test( "pushes a failing assertion if no message is given", function( assert ) {
assert.expect( 2 );
assert.expect( 3 );

var originalPushResult = assert.pushResult;
assert.pushResult = function pushResultStub( resultInfo ) {
Expand All @@ -12,10 +12,12 @@ QUnit.test( "pushes a failing assertion if no message is given", function( asser
};

assert.step();

assert.verifySteps( [ undefined ] );
} );

QUnit.test( "pushes a failing assertion if empty message is given", function( assert ) {
assert.expect( 2 );
assert.expect( 3 );

var originalPushResult = assert.pushResult;
assert.pushResult = function pushResultStub( resultInfo ) {
Expand All @@ -26,13 +28,17 @@ QUnit.test( "pushes a failing assertion if empty message is given", function( as
};

assert.step( "" );

assert.verifySteps( [ "" ] );
} );

QUnit.test( "pushes a passing assertion if a message is given", function( assert ) {
assert.expect( 2 );
assert.expect( 3 );

assert.step( "One step" );
assert.step( "Two step" );

assert.verifySteps( [ "One step", "Two step" ] );
} );

QUnit.module( "assert.verifySteps" );
Expand Down Expand Up @@ -88,3 +94,15 @@ QUnit.test( "resets the step list after verification", function( assert ) {
assert.step( "two" );
assert.verifySteps( [ "two" ] );
} );

QUnit.test( "errors if not called when `assert.step` is used", function( assert ) {
assert.expect( 2 );
assert.step( "one" );

var originalPushFailure = QUnit.config.current.pushFailure;
QUnit.config.current.pushFailure = function pushFailureStub( message ) {
QUnit.config.current.pushFailure = originalPushFailure;

assert.equal( message, "Expected assert.verifySteps() to be called before end of test after using assert.step(). Unverified steps: one" );
};
} );

0 comments on commit 7385942

Please sign in to comment.