From bea9febaf288334a3c4af3dc8c1c5340901fa980 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sun, 6 Jun 2021 17:46:39 +0100 Subject: [PATCH] Tests: Enable test/main/promise.js in mozjs CI job It just had one inline call to setTimeout in one of the test cases, which is easily changed to use our local `defer` function instead. After that, it passed. While at it, simplify and synchronise the two mock-promise functions. Not yet worth abstracting I think, but thinking about it. Ref https://github.com/qunitjs/qunit/issues/1511. --- .github/workflows/CI.yaml | 17 +++++++++++++++++ test/main/assert.js | 19 ++++++------------- test/main/promise.js | 17 ++++++++++------- test/mozjs.js | 2 +- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index a7983ab79..c7ca91305 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -65,6 +65,23 @@ jobs: - name: Tests run: ${{ matrix.script }} + # To reproduce SpiderMonkey builds locally: + # + # 1. Run the build the same way you normally do. + # ``` + # nobody@nodejs-isolated:/qunit$ npm run build + # ``` + # 2. Start a temporary Docker container using the official Ubuntu image from DockerHub, + # and mount the current working directory in it: + # ``` + # you@host:/qunit$ MNT=$(basename "$PWD"); docker run --rm --interactive --tty --mount type=bind,source="$PWD",target="/$MNT",readonly --entrypoint /bin/sh ubuntu:focal -c "cd /$MNT;bash" + # ``` + # 3. Run the following from the Docker container's bash prompt: + # ``` + # root@ubuntu-tmp/qunit$ apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y libmozjs-68-dev + # root@ubuntu-tmp/qunit$ js68 test/mozjs.js + # ``` + # sm-test: name: SpiderMonkey runs-on: ubuntu-20.04 diff --git a/test/main/assert.js b/test/main/assert.js index 2f702fcfb..935826769 100644 --- a/test/main/assert.js +++ b/test/main/assert.js @@ -1,16 +1,11 @@ function buildMockPromise( settledValue, shouldFulfill ) { - // Support IE 9: Promise not supported, test should not load polyfil globally to ensure - // QUnit works without it, so we create our own thenable with setTimeout. - // Support SpiderMonkey: setTimeout is not supported, re-use the IE 9 mock, but - // using SM's native Promise support for the deferred callback handling. - var defer = typeof setTimeout !== "undefined" ? - setTimeout : - function( fn ) { - Promise.resolve().then( fn ); - }; + // Support IE 9: Promise not supported, test MUST NOT load polyfil globally. + // Support SpiderMonkey: setTimeout is not supported, but native Promise is. + var defer = typeof setTimeout !== "undefined" ? setTimeout : function( fn ) { + Promise.resolve().then( fn ); + }; - // Return a mock self-fulfilling Promise ("thenable") var thenable = { then: function( fulfilledCallback, rejectedCallback ) { defer( function() { @@ -19,12 +14,10 @@ function buildMockPromise( settledValue, shouldFulfill ) { rejectedCallback.call( thenable, settledValue ); } ); - // returning another thennable for easy confirmation - // of return value + // returning another thenable for easy confirmation of return value return buildMockPromise( "final promise", true ); } }; - return thenable; } diff --git a/test/main/promise.js b/test/main/promise.js index 33c2c4ce3..d20dc47df 100644 --- a/test/main/promise.js +++ b/test/main/promise.js @@ -1,15 +1,18 @@ +// Support IE 9: Promise not supported, test MUST NOT load polyfil globally. +// Support SpiderMonkey: setTimeout is not supported, but native Promise is. +var defer = typeof setTimeout !== "undefined" ? setTimeout : function( fn ) { + Promise.resolve().then( fn ); +}; + // NOTE: Adds 1 assertion function createMockPromise( assert, reject, value ) { if ( arguments.length < 3 ) { value = {}; } - - // Return a mock self-fulfilling Promise ("thenable") var thenable = { then: function( fulfilledCallback, rejectedCallback ) { - assert.strictEqual( this, thenable, "`then` was invoked with the Promise as the " + - "context" ); - setTimeout( function() { + assert.strictEqual( this, thenable, "`then` invoked with our Promise as thisValue" ); + defer( function() { return reject ? rejectedCallback.call( thenable, value ) : fulfilledCallback.call( thenable, value ); @@ -150,10 +153,10 @@ QUnit.module( "Support for Promise", function() { assert.expect( 2 ); var done = assert.async(); - setTimeout( function() { + defer( function() { assert.true( true ); done(); - }, 100 ); + } ); // Adds 1 assertion return createMockPromise( assert ); diff --git a/test/mozjs.js b/test/mozjs.js index e97ec47e3..0313a297f 100644 --- a/test/mozjs.js +++ b/test/mozjs.js @@ -43,7 +43,7 @@ loadRelativeToScript( "../test/main/assert.js" ); loadRelativeToScript( "../test/main/assert/step.js" ); // Requires setTimeout, loadRelativeToScript( "../test/main/assert/timeout.js" ); // Requires setTimeout, loadRelativeToScript( "../test/main/async.js" ); -// Requires setTimeout, loadRelativeToScript( "../test/main/promise.js" ); +loadRelativeToScript( "../test/main/promise.js" ); loadRelativeToScript( "../test/main/dump.js" ); // Requires setTimeout, loadRelativeToScript( "../test/main/modules.js" ); loadRelativeToScript( "../test/main/deepEqual.js" );