Skip to content

Commit

Permalink
Core: Refactor internal core.js to declare QUnit as a single literal
Browse files Browse the repository at this point in the history
Remove any remaining dynamic or indirection in declaring the exports.

Ref #1551.
Ref #1724.
  • Loading branch information
Krinkle committed Jul 15, 2024
1 parent 04040f3 commit d395525
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 69 deletions.
38 changes: 38 additions & 0 deletions src/callbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import config from './core/config.js';
import Promise from './promise.js';

export function createRegisterCallbackFunction (key) {
// Initialize key collection of callback
if (typeof config.callbacks[key] === 'undefined') {
config.callbacks[key] = [];
}

return function registerCallback (callback) {
if (typeof callback !== 'function') {
throw new TypeError('Callback parameter must be a function');
}
config.callbacks[key].push(callback);
};
}

export function runLoggingCallbacks (key, arg) {
const callbacks = config.callbacks[key];

// Handling 'log' callbacks separately. Unlike the other callbacks,
// the log callback is not controlled by the processing queue,
// but rather used by asserts. Hence to promisfy the 'log' callback
// would mean promisfying each step of a test
if (key === 'log') {
callbacks.map(callback => callback(arg));
return;
}

// ensure that each callback is executed serially
let promiseChain = Promise.resolve();
callbacks.forEach(callback => {
promiseChain = promiseChain.then(() => {
return Promise.resolve(callback(arg));
});
});
return promiseChain;
}
39 changes: 22 additions & 17 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import reporters from './reporters.js';

import config from './core/config.js';
import hooks from './core/hooks.js';
import { extend, objectType, is, performance } from './core/utilities.js';
import { registerLoggingCallbacks, runLoggingCallbacks } from './core/logging.js';
import { objectType, is, performance } from './core/utilities.js';
import { createRegisterCallbackFunction, runLoggingCallbacks } from './callbacks.js';
import { sourceFromStacktrace } from './core/stacktrace.js';
import ProcessingQueue from './core/processing-queue.js';

Expand All @@ -20,8 +20,6 @@ import onUncaughtException from './core/on-uncaught-exception.js';
import diff from './diff.js';
import version from './version.js';

const QUnit = {};

// The "currentModule" object would ideally be defined using the createModule()
// function. Since it isn't, add the missing suiteReport property to it now that
// we have loaded all source code required to do so.
Expand All @@ -32,13 +30,14 @@ config.currentModule.suiteReport = runSuite;

config._pq = new ProcessingQueue(test);

// Figure out if we're running the tests from a server or not
QUnit.isLocal = (window && window.location && window.location.protocol === 'file:');
const QUnit = {

// Figure out if we're running the tests from a server or not
isLocal: (window && window.location && window.location.protocol === 'file:'),

// Expose the current QUnit version
QUnit.version = version;
// Expose the current QUnit version
version,

extend(QUnit, {
config,
urlParams,

Expand All @@ -48,11 +47,19 @@ extend(QUnit, {
reporters,
hooks,
is,
objectType,
on,
objectType,
onUncaughtException,
pushFailure,

begin: createRegisterCallbackFunction('begin'),
done: createRegisterCallbackFunction('done'),
log: createRegisterCallbackFunction('log'),
moduleDone: createRegisterCallbackFunction('moduleDone'),
moduleStart: createRegisterCallbackFunction('moduleStart'),
testDone: createRegisterCallbackFunction('testDone'),
testStart: createRegisterCallbackFunction('testStart'),

assert: Assert.prototype,
module,
test,
Expand Down Expand Up @@ -81,15 +88,15 @@ extend(QUnit, {
// still wait for DOM ready to ensure reliable integration of reporters.
window.addEventListener('load', function () {
setTimeout(function () {
begin();
doBegin();
});
});
} else if (setTimeout) {
setTimeout(function () {
begin();
doBegin();
});
} else {
begin();
doBegin();
}
},

Expand All @@ -101,16 +108,14 @@ extend(QUnit, {
const source = sourceFromStacktrace(offset);
return source;
}
});

registerLoggingCallbacks(QUnit);
};

function unblockAndAdvanceQueue () {
config.blocking = false;
config._pq.advance();
}

function begin () {
function doBegin () {
if (config.started) {
unblockAndAdvanceQueue();
return;
Expand Down
50 changes: 0 additions & 50 deletions src/core/logging.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/processing-queue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import config from './config.js';
import { extend, generateHash, performance } from './utilities.js';
import { runLoggingCallbacks } from './logging.js';
import { runLoggingCallbacks } from '../callbacks.js';

import Promise from '../promise.js';
import { runSuite } from '../module.js';
Expand Down
2 changes: 1 addition & 1 deletion src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
inArray,
performance
} from './core/utilities.js';
import { runLoggingCallbacks } from './core/logging.js';
import { runLoggingCallbacks } from './callbacks.js';
import { extractStacktrace, sourceFromStacktrace } from './core/stacktrace.js';
import dump from './dump.js';

Expand Down

0 comments on commit d395525

Please sign in to comment.