diff --git a/.eslintrc.json b/.eslintrc.json index 1b588c63a..fdfc36087 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -120,7 +120,7 @@ } }, { - "files": ["test/mozjs.js"], + "files": ["test/mozjs.js", "test/mozjs.mjs"], "parserOptions": { "ecmaVersion": 6 }, @@ -141,7 +141,7 @@ } }, { - "files": ["test/main/*-es2018.js"], + "files": ["test/main/*-es2018.js", "test/main/*-esm.mjs"], "env": { "es2018": true }, @@ -153,7 +153,7 @@ } }, { - "files": ["test/benchmark/**", "test/es2018/**"], + "files": ["test/benchmark/**"], "env": { "es2017": true }, diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index fe9079c70..3c4642546 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -106,8 +106,10 @@ jobs: sudo apt-fast install -y libmozjs-68-dev - run: npm ci - run: npm run build - - name: Tests + - name: Test mozjs.js run: js68 test/mozjs.js + - name: Test mozjs.mjs + run: js68 -m test/mozjs.mjs # To reproduce SpiderMonkey 102 builds locally: # @@ -142,8 +144,10 @@ jobs: sudo apt-get install -y libmozjs-102-dev - run: npm ci - run: npm run build - - name: Tests + - name: Test mozjs.js run: js102 test/mozjs.js + - name: Test mozjs.mjs + run: js102 -m test/mozjs.mjs # To reproduce SpiderMonkey 115 builds locally: # @@ -178,5 +182,7 @@ jobs: sudo apt-get install -y libmozjs-115-dev - run: npm ci - run: npm run build - - name: Tests + - name: Test mozjs.js run: js115 test/mozjs.js + - name: Test mozjs.mjs + run: js115 -m test/mozjs.mjs diff --git a/Gruntfile.js b/Gruntfile.js index 88a333cc8..f256e7de1 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -128,6 +128,7 @@ module.exports = function (grunt) { 'test/main/HtmlReporter.js', 'test/main/modules.js', 'test/main/modules-es2018.js', + 'test/main/modules-esm.mjs', 'test/main/legacy.js', 'test/main/onUncaughtException.js', 'test/main/promise.js', diff --git a/build/tasks/test-on-node.js b/build/tasks/test-on-node.js index d3df6f1e4..a1610d0c1 100644 --- a/build/tasks/test-on-node.js +++ b/build/tasks/test-on-node.js @@ -6,7 +6,7 @@ module.exports = function (grunt) { const totals = { files: 0, failed: 0 }; for (const file of this.data) { - totals.failed += await new Promise(resolve => runQUnit(file, resolve)); + totals.failed += await runQUnit(file); totals.files++; } @@ -28,7 +28,7 @@ module.exports = function (grunt) { return require(path); } - function runQUnit (file, runEnd) { + async function runQUnit (file) { // Resolve current QUnit path and remove it from the require cache // to avoid stacking the QUnit logs. let QUnit = requireFresh('../../qunit/qunit.js'); @@ -37,9 +37,10 @@ module.exports = function (grunt) { global.QUnit = QUnit; QUnit.config.autostart = false; - requireFresh('../../' + file); - registerEvents(QUnit, file, runEnd); + await import('../../' + file); + const runEnd = new Promise(resolve => registerEvents(QUnit, file, resolve)); QUnit.start(); + await runEnd; } function registerEvents (QUnit, fileName, callback) { diff --git a/test/cli/cli-main.js b/test/cli/cli-main.js index 59dadc790..8af31d97a 100644 --- a/test/cli/cli-main.js +++ b/test/cli/cli-main.js @@ -54,22 +54,6 @@ ok 3 config # exit code: 1`); }); - // TODO: Move to /test/cli/fixtures/ - QUnit.test('run ESM test suite with import statement', async assert => { - const command = ['qunit', '../../es2018/esm.mjs']; - const execution = await execute(command); - - assert.equal(execution.code, 0); - assert.equal(execution.stderr, ''); - assert.equal(execution.stdout, `TAP version 13 -ok 1 ESM test suite > sum() -1..1 -# pass 1 -# skip 0 -# todo 0 -# fail 0`); - }); - // TODO: Move to /test/cli/fixtures/ QUnit.test('normal trace with native source map', async assert => { const command = ['qunit', 'sourcemap/source.js']; diff --git a/test/cli/structure.js b/test/cli/structure.js index e032fc3d3..349887b54 100644 --- a/test/cli/structure.js +++ b/test/cli/structure.js @@ -7,16 +7,16 @@ const glob = require('tiny-glob/sync'); // tests, such as checking for fixture files that aren't used or // missing from one of the test targets. QUnit.module('structure', () => { - QUnit.module('test/main/*.js', () => { + QUnit.module('test/main/', () => { const files = fs.readdirSync(path.join(__dirname, '..', 'main')) .map(file => `main/${file}`); QUnit.test('files', assert => { assert.true(files.length > 5, 'found files'); assert.deepEqual( - files.filter(file => file.endsWith('.js')), + files.filter(file => file.endsWith('.js') || file.endsWith('.mjs')), files, - 'js files' + 'JS files only' ); }); @@ -43,13 +43,20 @@ QUnit.module('structure', () => { }); }); - QUnit.test('test/mozjs', assert => { + QUnit.test('test/mozjs.js', assert => { const contents = fs.readFileSync(path.join(__dirname, '..', 'mozjs.js'), 'utf8'); files.forEach(file => { assert.true(contents.includes(file), file); }); }); + QUnit.test('test/mozjs.mjs', assert => { + const contents = fs.readFileSync(path.join(__dirname, '..', 'mozjs.mjs'), 'utf8'); + files.forEach(file => { + assert.true(contents.includes(file), file); + }); + }); + QUnit.test('test/webWorker-worker.js', assert => { const contents = fs.readFileSync(path.join(__dirname, '..', 'webWorker-worker.js'), 'utf8'); files.forEach(file => { diff --git a/test/es2018/sum.mjs b/test/dynamic-import/sum.mjs similarity index 100% rename from test/es2018/sum.mjs rename to test/dynamic-import/sum.mjs diff --git a/test/es2018/esm.mjs b/test/es2018/esm.mjs deleted file mode 100644 index ca17f7a3c..000000000 --- a/test/es2018/esm.mjs +++ /dev/null @@ -1,7 +0,0 @@ -import sum from './sum.mjs'; - -QUnit.module('ESM test suite', () => { - QUnit.test('sum()', assert => { - assert.equal(5, sum(2, 3)); - }); -}); diff --git a/test/index-es5.html b/test/index-es5.html index 91ad6befa..8d48d117c 100644 --- a/test/index-es5.html +++ b/test/index-es5.html @@ -22,6 +22,7 @@ + diff --git a/test/index.html b/test/index.html index 356321617..afc7bbc1f 100644 --- a/test/index.html +++ b/test/index.html @@ -22,6 +22,7 @@ + diff --git a/test/main/modules-esm.mjs b/test/main/modules-esm.mjs new file mode 100644 index 000000000..f747b0125 --- /dev/null +++ b/test/main/modules-esm.mjs @@ -0,0 +1,7 @@ +import sum from '../dynamic-import/sum.mjs'; + +QUnit.module('modules [esm]', () => { + QUnit.test('example', assert => { + assert.equal(5, sum(2, 3)); + }); +}); diff --git a/test/main/setTimeout.js b/test/main/setTimeout.js index 4b00f71dc..e3f9f4e87 100644 --- a/test/main/setTimeout.js +++ b/test/main/setTimeout.js @@ -1,12 +1,14 @@ -(function (globalThis) { +(function () { + // eslint-disable-next-line no-undef + var global = typeof globalThis !== 'undefined' ? globalThis : window; QUnit.module('Support for mocked setTimeout', { beforeEach: function () { - this.setTimeout = globalThis.setTimeout; - globalThis.setTimeout = function () {}; + this.setTimeout = global.setTimeout; + global.setTimeout = function () {}; }, afterEach: function () { - globalThis.setTimeout = this.setTimeout; + global.setTimeout = this.setTimeout; } }); @@ -17,6 +19,4 @@ QUnit.test('test two', function (assert) { assert.true(true); }); -}((function () { - return this; -}()))); +}()); diff --git a/test/mozjs.js b/test/mozjs.js index 0697ed53e..b0ac0ff96 100644 --- a/test/mozjs.js +++ b/test/mozjs.js @@ -24,6 +24,7 @@ loadRelativeToScript('../test/main/each.js'); loadRelativeToScript('../test/main/HtmlReporter.js'); // loadRelativeToScript( "../test/main/modules.js" ); // Requires setTimeout loadRelativeToScript('../test/main/modules-es2018.js'); +// import('../test/main/modules-esm.mjs').then(…); // Covered by mozjs.mjs loadRelativeToScript('../test/main/legacy.js'); loadRelativeToScript('../test/main/onUncaughtException.js'); loadRelativeToScript('../test/main/promise.js'); diff --git a/test/mozjs.mjs b/test/mozjs.mjs new file mode 100644 index 000000000..de9517911 --- /dev/null +++ b/test/mozjs.mjs @@ -0,0 +1,34 @@ +import '../qunit/qunit.js'; + +// Sync with test/index.html +import '../test/main/assert.js'; +import '../test/main/assert-es6.js'; +import '../test/main/assert-step.js'; +// import "../test/main/assert-timeout.js"; // Requires setTimeout +// import "../test/main/async.js"; // Requires setTimeout +import '../test/main/deepEqual.js'; +import '../test/main/diff.js'; +import '../test/main/dump.js'; +import '../test/main/each.js'; +import '../test/main/HtmlReporter.js'; +// import "../test/main/modules.js"; // Requires setTimeout +import '../test/main/modules-es2018.js'; +import '../test/main/modules-esm.mjs'; +import '../test/main/legacy.js'; +import '../test/main/onUncaughtException.js'; +import '../test/main/promise.js'; +import '../test/main/setTimeout.js'; +import '../test/main/stacktrace.js'; +import '../test/main/test.js'; +import '../test/main/utilities.js'; + +QUnit.reporters.tap.init(QUnit); +QUnit.on('runEnd', (suiteEnd) => { + if (suiteEnd.status === 'failed') { + // There is no built-in function for sending a non-zero exit code, + // so throw an uncaught error to make this happen. + throw new Error('Test run has failures.'); + } +}); + +QUnit.start(); diff --git a/test/webWorker-worker.js b/test/webWorker-worker.js index b9464a05e..f6b92abee 100644 --- a/test/webWorker-worker.js +++ b/test/webWorker-worker.js @@ -18,6 +18,7 @@ importScripts( 'main/HtmlReporter.js', 'main/modules.js', 'main/modules-es2018.js', + // TODO: 'main/modules-esm.mjs', 'main/legacy.js', 'main/onUncaughtException.js', 'main/promise.js',