Skip to content

Commit

Permalink
[WIP] Build: Adopt ESM export between core.js and qunit.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Krinkle committed Jul 24, 2024
1 parent af64edc commit 38f7171
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
69 changes: 39 additions & 30 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,55 @@ config.currentModule.suiteReport = runSuite;

config._pq = new ProcessingQueue(test);

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

// Figure out if we're running the tests from a server or not
isLocal: (window && window.location && window.location.protocol === 'file:'),
const begin = createRegisterCallbackFunction('begin');
const done = createRegisterCallbackFunction('done');
const log = createRegisterCallbackFunction('log');
const moduleDone = createRegisterCallbackFunction('moduleDone');
const moduleStart = createRegisterCallbackFunction('moduleStart');
const testDone = createRegisterCallbackFunction('testDone');
const testStart = createRegisterCallbackFunction('testStart');

// Expose the current QUnit version
version,
const assert = Assert.prototype;

const only = test.only;
const skip = test.skip;
const todo = test.todo;

export {
hooks,
module,
start,
test,

only,
skip,
todo,

begin,
done,
log,
moduleDone,
moduleStart,
on,
testDone,
testStart,

config,
stack,
urlParams,

assert,
diff,
dump,
equiv,
reporters,
hooks,
is,
on,
isLocal,
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,
start,
test,

// alias other test flavors for easy access
todo: test.todo,
skip: test.skip,
only: test.only
reporters,
stack,
urlParams,
version
};

export default QUnit;
21 changes: 20 additions & 1 deletion src/core/qunit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import QUnit from './core.js';
import * as QUnitCore from './core.js';
import { initBrowser } from './browser/browser-runner.js';
import { window, document } from './globals.js';
import { setQUnitObject } from './start.js';
import exportQUnit from './export.js';

// The imported object from core.js, per ESM/Rollup, is frozen.
// We support adding and replacing QUnit methods, so wrap the exported
// object in Object.create() to create writable version. The caveat is
// that a monkey-patched method like QUnit.test() will be reflected in
// `import QUnit from qunit.js; QUnit.test()`
// the change is not visible to
// `import { test } from qunit.js`
//
// CJS usage is unaffected since there we assign the writeable QUnit
// object to module.exports. The read-only QUnitCore will only
// be exposed to ESM via `export * from './core.js';` below.
const QUnit = Object.create(QUnitCore);

setQUnitObject(QUnit);

exportQUnit(QUnit);

if (window && document) {
initBrowser(QUnit, window, document);
}

// TODO: Decide how to format the CJS and ESM distribution
// in Rollup config.
// export * from './core.js';
// export default QUnit;
1 change: 1 addition & 0 deletions src/core/version.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Expose the current QUnit version
// Replaced by /rollup.config.js using /build/dist-replace.js
export default '@VERSION';

0 comments on commit 38f7171

Please sign in to comment.