Skip to content

Commit

Permalink
Test: Convert es2018/esm.mjs to main/modules-esm.mjs
Browse files Browse the repository at this point in the history
* This introduces the first running of /test/main in an ESM environment,
  with the addition of `mosjs.mjs`.
  This uncovered a minor issue in test/main/setTimeout.js which assumed
  a global `this`, which is undefined in a module context.

* This introduces the first covering of standalone Node.js usage
  in ESM via import().
  • Loading branch information
Krinkle committed Jul 8, 2024
1 parent 0a51f1b commit 8808b04
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 44 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
}
},
{
"files": ["test/mozjs.js"],
"files": ["test/mozjs.js", "test/mozjs.mjs"],
"parserOptions": {
"ecmaVersion": 6
},
Expand All @@ -141,7 +141,7 @@
}
},
{
"files": ["test/main/*-es2018.js"],
"files": ["test/main/*-es2018.js", "test/main/*-esm.mjs"],
"env": {
"es2018": true
},
Expand All @@ -153,7 +153,7 @@
}
},
{
"files": ["test/benchmark/**", "test/es2018/**"],
"files": ["test/benchmark/**"],
"env": {
"es2017": true
},
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand Down Expand Up @@ -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:
#
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions build/tasks/test-on-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

Expand All @@ -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');
Expand All @@ -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) {
Expand Down
16 changes: 0 additions & 16 deletions test/cli/cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
15 changes: 11 additions & 4 deletions test/cli/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});

Expand All @@ -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 => {
Expand Down
File renamed without changes.
7 changes: 0 additions & 7 deletions test/es2018/esm.mjs

This file was deleted.

1 change: 1 addition & 0 deletions test/index-es5.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<script src="main/HtmlReporter.js"></script>
<script src="main/modules.js"></script>
<!-- <script src="main/modules-es2018.js"></script> -->
<!-- <script src="main/modules-esm.mjs" type="module"></script> -->
<script src="main/legacy.js"></script>
<script src="main/onUncaughtException.js"></script>
<script src="main/promise.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<script src="main/HtmlReporter.js"></script>
<script src="main/modules.js"></script>
<script src="main/modules-es2018.js"></script>
<script src="main/modules-esm.mjs" type="module"></script>
<script src="main/legacy.js"></script>
<script src="main/onUncaughtException.js"></script>
<script src="main/promise.js"></script>
Expand Down
7 changes: 7 additions & 0 deletions test/main/modules-esm.mjs
Original file line number Diff line number Diff line change
@@ -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));
});
});
14 changes: 7 additions & 7 deletions test/main/setTimeout.js
Original file line number Diff line number Diff line change
@@ -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;
}
});

Expand All @@ -17,6 +19,4 @@
QUnit.test('test two', function (assert) {
assert.true(true);
});
}((function () {
return this;
}())));
}());
1 change: 1 addition & 0 deletions test/mozjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
34 changes: 34 additions & 0 deletions test/mozjs.mjs
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions test/webWorker-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8808b04

Please sign in to comment.