(function( window ) {
var QUnit,
+ assert,
config,
onErrorFnPrev,
testId = 0,
}
};
+// `assert` initialized at top of scope
// Asssert helpers
-// All of these must call either QUnit.push() or manually do:
+// All of these must either call QUnit.push() or manually do:
// - runLoggingCallbacks( "log", .. );
// - config.current.assertions.push({ .. });
-QUnit.assert = {
+// We attach it to the QUnit object *after* we expose the public API,
+// otherwise `assert` will become a global variable in browsers (#341).
+assert = {
/**
* Asserts rough true-ish result.
* @name ok
/**
* @deprecate since 1.8.0
- * Kept assertion helpers in root for backwards compatibility
+ * Kept assertion helpers in root for backwards compatibility.
*/
-extend( QUnit, QUnit.assert );
+extend( QUnit, assert );
/**
* @deprecated since 1.9.0
- * Kept global "raises()" for backwards compatibility
+ * Kept root "raises()" for backwards compatibility.
+ * (Note that we don't introduce assert.raises).
*/
-QUnit.raises = QUnit.assert[ "throws" ];
+QUnit.raises = assert[ "throws" ];
/**
* @deprecated since 1.0.0, replaced with error pushes since 1.3.0
// Extend QUnit object,
// these after set here because they should not be exposed as global functions
extend( QUnit, {
+ assert: assert,
+
config: config,
// Initialize the configuration options
QUnit.module("assertion helpers");
-QUnit.test( "QUnit.assert compatibility", function( assert ) {
- QUnit.expect(4);
-
+QUnit.test( "QUnit.assert compatibility", 5, function( assert ) {
assert.ok( true, "Calling method on `assert` argument to test() callback" );
- // Should also work, although not documented
+ // Should also work, although discouraged and not documented
QUnit.assert.ok( true, "Calling method on QUnit.assert object" );
// Test compatibility aliases
QUnit.ok( true, "Calling aliased method in QUnit root object" );
ok( true, "Calling aliased function in global namespace" );
+
+ // Regression fix for #341
+ // The assert-context way of testing discouraged global variables,
+ // it doesn't make sense of it itself to be a global variable.
+ // Only allows for mistakes (e.g. forgetting to list 'assert' as parameter)
+ assert.notStrictEqual( window.assert, QUnit.assert, "Assert does not get exposed as a global variable" );
});
module("setup test", {