Skip to content

Commit

Permalink
Core: Clean up internals to flatten logic and push down variables
Browse files Browse the repository at this point in the history
Flattening logic and pushing local vars down in scope.

Incremental cleanup towards deprecating `QUnit.load`.

Closes #1526.
Ref #1084.
  • Loading branch information
smcclure15 authored Dec 26, 2020
1 parent 25f8669 commit da3aff3
Showing 1 changed file with 68 additions and 69 deletions.
137 changes: 68 additions & 69 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,64 @@ QUnit.isLocal = ( window && window.location && window.location.protocol === "fil
QUnit.version = "@VERSION";

extend( QUnit, {
config,

dump,
equiv,
is,
objectType,
on,
onError,
onUnhandledRejection,
pushFailure,

assert: Assert.prototype,
module,

test: test,
test,

// alias other test flavors for easy access
todo: test.todo,
skip: test.skip,
only: test.only,

start: function( count ) {
var globalStartAlreadyCalled = globalStartCalled;

if ( !config.current ) {
globalStartCalled = true;

if ( runStarted ) {
throw new Error( "Called start() while test already started running" );
} else if ( globalStartAlreadyCalled || count > 1 ) {
throw new Error( "Called start() outside of a test context too many times" );
} else if ( config.autostart ) {
throw new Error( "Called start() outside of a test context when " +
"QUnit.config.autostart was true" );
} else if ( !config.pageLoaded ) {

// The page isn't completely loaded yet, so we set autostart and then
// load if we're in Node or wait for the browser's load event.
config.autostart = true;

// Starts from Node even if .load was not previously called. We still return
// early otherwise we'll wind up "beginning" twice.
if ( !document ) {
QUnit.load();
}

return;
}
} else {
if ( config.current ) {
throw new Error( "QUnit.start cannot be called inside a test context." );
}

scheduleBegin();
},
var globalStartAlreadyCalled = globalStartCalled;
globalStartCalled = true;

if ( runStarted ) {
throw new Error( "Called start() while test already started running" );
}
if ( globalStartAlreadyCalled || count > 1 ) {
throw new Error( "Called start() outside of a test context too many times" );
}
if ( config.autostart ) {
throw new Error( "Called start() outside of a test context when " +
"QUnit.config.autostart was true" );
}

config: config,
if ( !config.pageLoaded ) {

is: is,
// The page isn't completely loaded yet, so we set autostart and then
// load if we're in Node or wait for the browser's load event.
config.autostart = true;

objectType: objectType,
// Starts from Node even if .load was not previously called. We still return
// early otherwise we'll wind up "beginning" twice.
if ( !document ) {
QUnit.load();
}

return;
}

scheduleBegin();

},

extend: function( ...args ) {
Logger.warn( "QUnit.extend is deprecated and will be removed in QUnit 3.0." +
Expand Down Expand Up @@ -121,18 +129,9 @@ extend( QUnit, {
stack: function( offset ) {
offset = ( offset || 0 ) + 2;
return sourceFromStacktrace( offset );
},

onError,

onUnhandledRejection
}
} );

QUnit.pushFailure = pushFailure;
QUnit.assert = Assert.prototype;
QUnit.equiv = equiv;
QUnit.dump = dump;

registerLoggingCallbacks( QUnit );

function scheduleBegin() {
Expand All @@ -155,37 +154,37 @@ function unblockAndAdvanceQueue() {
}

export function begin() {
var i, l,
modulesLog = [];

// If the test run hasn't officially begun yet
if ( !config.started ) {

// Record the time of the test run's beginning
config.started = now();
if ( config.started ) {
unblockAndAdvanceQueue();
return;
}

// Delete the loose unnamed module if unused.
if ( config.modules[ 0 ].name === "" && config.modules[ 0 ].tests.length === 0 ) {
config.modules.shift();
}
// The test run hasn't officially begun yet
// Record the time of the test run's beginning
config.started = now();

// Avoid unnecessary information by not logging modules' test environments
for ( i = 0, l = config.modules.length; i < l; i++ ) {
modulesLog.push( {
name: config.modules[ i ].name,
tests: config.modules[ i ].tests
} );
}
// Delete the loose unnamed module if unused.
if ( config.modules[ 0 ].name === "" && config.modules[ 0 ].tests.length === 0 ) {
config.modules.shift();
}

// The test run is officially beginning now
emit( "runStart", globalSuite.start( true ) );
runLoggingCallbacks( "begin", {
totalTests: Test.count,
modules: modulesLog
} ).then( unblockAndAdvanceQueue );
} else {
unblockAndAdvanceQueue();
// Avoid unnecessary information by not logging modules' test environments
const l = config.modules.length;
const modulesLog = [];
for ( let i = 0; i < l; i++ ) {
modulesLog.push( {
name: config.modules[ i ].name,
tests: config.modules[ i ].tests
} );
}

// The test run is officially beginning now
emit( "runStart", globalSuite.start( true ) );
runLoggingCallbacks( "begin", {
totalTests: Test.count,
modules: modulesLog
} ).then( unblockAndAdvanceQueue );
}

exportQUnit( QUnit );
Expand Down

0 comments on commit da3aff3

Please sign in to comment.