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

Assert: Clone actual steps array to avoid passing internal reference #1267

Merged
merged 2 commits into from
Mar 1, 2018
Merged
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
5 changes: 4 additions & 1 deletion src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class Assert {

// Verifies the steps in a test match a given array of string values
verifySteps( steps, message ) {
this.deepEqual( this.test.steps, steps, message );

// Since the steps array is just string values, we can clone with slice
const actualStepsClone = this.test.steps.slice();
this.deepEqual( actualStepsClone, steps, message );
this.test.steps.length = 0;
}

Expand Down
27 changes: 27 additions & 0 deletions test/main/assert/step.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,30 @@ QUnit.test( "errors if not called when `assert.step` is used", function( assert
assert.equal( message, "Expected assert.verifySteps() to be called before end of test after using assert.step(). Unverified steps: one" );
};
} );

// Testing to ensure steps array is not passed by reference: https://github.com/qunitjs/qunit/issues/1266
QUnit.module( "assert.verifySteps value reference", function() {

var loggedAssertions = {};

QUnit.log( function( details ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that (AFAIK) these functions cannot be removed, this QUnit.log will continue to fill loggedAssertions for all subsequent (and unrelated) tests, right?

Does that pose a concern (or maybe it’s “fine”)?

Copy link
Member Author

Choose a reason for hiding this comment

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

It only ever logs entries into loggedAssertions["verification-assertion"], so I don't see much of an issue with it (given that I don't believe any other assertion has that message in our code base)


if ( details.message === "verification-assertion" ) {
loggedAssertions[ details.message ] = details;
}

} );

QUnit.test( "passing test to see if steps array is passed by reference to logging function", function( assert ) {
assert.step( "step one" );
assert.step( "step two" );

assert.verifySteps( [ "step one", "step two" ], "verification-assertion" );
} );

QUnit.test( "steps array should not be reset in logging function", function( assert ) {
const result = loggedAssertions[ "verification-assertion" ].actual;
assert.deepEqual( result, [ "step one", "step two" ] );
} );

} );