From 12d6b330d0ec8818e3485476db2ee613af83a212 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 21 Jan 2021 11:01:10 -0800 Subject: [PATCH 1/6] [Tests] run `nyc` on all tests; use `tape` runner; add `implementation` tests --- .eslintignore | 1 + .nycrc | 13 +++++++++++++ package.json | 13 +++++-------- test/implementation.js | 30 ++++++++++++++++++++++++++++++ test/shimmed.js | 3 +-- 5 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 .eslintignore create mode 100644 .nycrc create mode 100644 test/implementation.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..404abb2 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +coverage/ diff --git a/.nycrc b/.nycrc new file mode 100644 index 0000000..1826526 --- /dev/null +++ b/.nycrc @@ -0,0 +1,13 @@ +{ + "all": true, + "check-coverage": false, + "reporter": ["text-summary", "text", "html", "json"], + "lines": 86, + "statements": 85.93, + "functions": 82.43, + "branches": 76.06, + "exclude": [ + "coverage", + "test" + ] +} diff --git a/package.json b/package.json index 7679ec7..8dcc1ca 100644 --- a/package.json +++ b/package.json @@ -15,15 +15,11 @@ "lint": "eslint .", "postlint": "evalmd README.md", "pretest": "npm run lint && es-shim-api --bound", - "test": "npm run --silent tests-only", + "test": "npm run tests-only && npm run test:promise-shimmed", "posttest": "npx aud --production", - "tests-only": "npm run --silent test:shimmed && npm run --silent test:module && npm run --silent tests:es5", - "tests:es5": "npm run --silent test:promise-shimmed", - "test:shimmed": "node test/shimmed", - "test:module": "node test", - "test:promise-shimmed": "node test/promise-shimmed", - "test:native": "node test/native", - "coverage": "covert test/*.js" + "tests-only": "nyc tape test/{implementation,index,shimmed}.js", + "test:promise-shimmed": "nyc node test/promise-shimmed", + "test:native": "node test/native" }, "repository": { "type": "git", @@ -72,6 +68,7 @@ "eslint": "^7.2.0", "evalmd": "^0.0.19", "functions-have-names": "^1.2.1", + "nyc": "^10.3.2", "safe-publish-latest": "^1.1.4", "tape": "^5.0.1" }, diff --git a/test/implementation.js b/test/implementation.js new file mode 100644 index 0000000..d1f5fdc --- /dev/null +++ b/test/implementation.js @@ -0,0 +1,30 @@ +'use strict'; + +var test = require('tape'); +var callBind = require('call-bind'); + +var any = require('../implementation'); +var runTests = require('./tests'); + +var bound = callBind(any); + +// eslint-disable-next-line no-shadow +var rebindable = function any(iterable) { + // eslint-disable-next-line no-invalid-this + return bound(typeof this === 'undefined' ? Promise : this, iterable); +}; + +test('as a function', function (t) { + t.test('bad Promise/this value', function (st) { + // eslint-disable-next-line no-useless-call + st['throws'](function () { any.call(undefined, []); }, TypeError, 'undefined is not an object'); + + // eslint-disable-next-line no-useless-call + st['throws'](function () { any.call(null, []); }, TypeError, 'null is not an object'); + st.end(); + }); + + runTests(rebindable, t); + + t.end(); +}); diff --git a/test/shimmed.js b/test/shimmed.js index f733a40..0b174ca 100644 --- a/test/shimmed.js +++ b/test/shimmed.js @@ -1,7 +1,6 @@ 'use strict'; -var any = require('../'); -any.shim(); +require('../auto'); var test = require('tape'); From 78d952b471459029830d66e28aed8b6c2aa4c8a6 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 15 Dec 2021 16:56:20 -0800 Subject: [PATCH 2/6] [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `@es-shims/api`, `aud`, `es6-shim`, `functions-have-names`, `safe-publish-latest`, `tape` --- .eslintignore | 1 - .eslintrc | 7 ------- package.json | 26 ++++++++++++++------------ 3 files changed, 14 insertions(+), 20 deletions(-) delete mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 404abb2..0000000 --- a/.eslintignore +++ /dev/null @@ -1 +0,0 @@ -coverage/ diff --git a/.eslintrc b/.eslintrc index 4192b7c..304752b 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,15 +3,8 @@ "extends": "@ljharb", - "globals": { - "Promise": false - }, - "rules": { - "dot-notation": [2, { "allowKeywords": false }], "new-cap": [2, { "capIsNewExceptions": ["GetIntrinsic", "PromiseResolve", "Type"] }], - "no-magic-numbers": 0, - "sort-keys": 0, }, "overrides": [ diff --git a/package.json b/package.json index 8dcc1ca..07e2142 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,12 @@ "description": "ES Proposal spec-compliant shim for Promise.any", "main": "index.js", "scripts": { - "prepublish": "safe-publish-latest", - "lint": "eslint .", - "postlint": "evalmd README.md", - "pretest": "npm run lint && es-shim-api --bound", + "prepublishOnly": "safe-publish-latest", + "prepublish": "not-in-publish || npm run prepublishOnly", + "prelint": "evalmd README.md", + "lint": "eslint --ext=js,mjs .", + "postlint": "es-shim-api --bound", + "pretest": "npm run lint", "test": "npm run tests-only && npm run test:promise-shimmed", "posttest": "npx aud --production", "tests-only": "nyc tape test/{implementation,index,shimmed}.js", @@ -61,16 +63,16 @@ "iterate-value": "^1.0.2" }, "devDependencies": { - "@es-shims/api": "^2.1.2", - "@ljharb/eslint-config": "^17.1.0", - "aud": "^1.1.2", - "es6-shim": "^0.35.5", - "eslint": "^7.2.0", + "@es-shims/api": "^2.2.3", + "@ljharb/eslint-config": "^20.1.0", + "aud": "^1.1.5", + "es6-shim": "^0.35.6", + "eslint": "^8.5.0", "evalmd": "^0.0.19", - "functions-have-names": "^1.2.1", + "functions-have-names": "^1.2.2", "nyc": "^10.3.2", - "safe-publish-latest": "^1.1.4", - "tape": "^5.0.1" + "safe-publish-latest": "^2.0.0", + "tape": "^5.4.0" }, "engines": { "node": ">= 0.4" From 885cd5905d90ca14a6b9a0597ac8a4590824ef28 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 15 Dec 2021 16:57:07 -0800 Subject: [PATCH 3/6] [Deps] update `array.prototype.map`, `es-abstract`, `es-aggregate-error` --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 07e2142..0207ef2 100644 --- a/package.json +++ b/package.json @@ -54,11 +54,11 @@ }, "homepage": "https://github.com/es-shims/Promise.any#readme", "dependencies": { - "array.prototype.map": "^1.0.2", + "array.prototype.map": "^1.0.4", "call-bind": "^1.0.2", "define-properties": "^1.1.3", - "es-abstract": "^1.18.0", - "es-aggregate-error": "^1.0.3", + "es-abstract": "^1.19.1", + "es-aggregate-error": "^1.0.7", "get-intrinsic": "^1.1.1", "iterate-value": "^1.0.2" }, From c63969e02b2d67a1e911f7bf1e42f20d9d0c2b1d Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 21 Jan 2021 11:01:53 -0800 Subject: [PATCH 4/6] [Tests] migrate tests to Github Actions; reuse common workflows - See https://github.com/inspect-js/is-arguments/pull/38 --- .github/workflows/node-aught.yml | 18 +++++++++++++++++ .github/workflows/node-native.yml | 18 +++++++++++++++++ .github/workflows/node-pretest.yml | 7 +++++++ .github/workflows/node-promise-shimmed.yml | 18 +++++++++++++++++ .github/workflows/node-tens.yml | 18 +++++++++++++++++ .github/workflows/rebase.yml | 8 ++++---- .github/workflows/require-allow-edits.yml | 2 +- .travis.yml | 23 ---------------------- 8 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/node-aught.yml create mode 100644 .github/workflows/node-native.yml create mode 100644 .github/workflows/node-pretest.yml create mode 100644 .github/workflows/node-promise-shimmed.yml create mode 100644 .github/workflows/node-tens.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/node-aught.yml b/.github/workflows/node-aught.yml new file mode 100644 index 0000000..4b48655 --- /dev/null +++ b/.github/workflows/node-aught.yml @@ -0,0 +1,18 @@ +name: 'Tests: node.js < 10' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '> 0.10 < 10' + type: minors + command: npm run tests-only + + node: + name: 'node < 10' + needs: [tests] + runs-on: ubuntu-latest + steps: + - run: 'echo tests completed' diff --git a/.github/workflows/node-native.yml b/.github/workflows/node-native.yml new file mode 100644 index 0000000..7862306 --- /dev/null +++ b/.github/workflows/node-native.yml @@ -0,0 +1,18 @@ +name: 'Tests: node.js: native' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '>= 15' + type: minors + command: npm run test:native + + node: + name: 'node, native tests' + needs: [tests] + runs-on: ubuntu-latest + steps: + - run: 'echo tests completed' diff --git a/.github/workflows/node-pretest.yml b/.github/workflows/node-pretest.yml new file mode 100644 index 0000000..765edf7 --- /dev/null +++ b/.github/workflows/node-pretest.yml @@ -0,0 +1,7 @@ +name: 'Tests: pretest/posttest' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/pretest.yml@main diff --git a/.github/workflows/node-promise-shimmed.yml b/.github/workflows/node-promise-shimmed.yml new file mode 100644 index 0000000..36cc773 --- /dev/null +++ b/.github/workflows/node-promise-shimmed.yml @@ -0,0 +1,18 @@ +name: 'Tests: node.js: shimmed Promise' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '0.8 - 0.12' + type: minors + command: npm run test:promise-shimmed + + node: + name: 'node, promise-shimmed tests' + needs: [tests] + runs-on: ubuntu-latest + steps: + - run: 'echo tests completed' diff --git a/.github/workflows/node-tens.yml b/.github/workflows/node-tens.yml new file mode 100644 index 0000000..b49ceb1 --- /dev/null +++ b/.github/workflows/node-tens.yml @@ -0,0 +1,18 @@ +name: 'Tests: node.js >= 10' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '>= 10' + type: minors + command: npm run tests-only + + node: + name: 'node >= 10' + needs: [tests] + runs-on: ubuntu-latest + steps: + - run: 'echo tests completed' diff --git a/.github/workflows/rebase.yml b/.github/workflows/rebase.yml index 027aed0..5b6d04b 100644 --- a/.github/workflows/rebase.yml +++ b/.github/workflows/rebase.yml @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: ljharb/rebase@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - uses: actions/checkout@v2 + - uses: ljharb/rebase@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/require-allow-edits.yml b/.github/workflows/require-allow-edits.yml index 549d7b4..7b842f8 100644 --- a/.github/workflows/require-allow-edits.yml +++ b/.github/workflows/require-allow-edits.yml @@ -9,4 +9,4 @@ jobs: runs-on: ubuntu-latest steps: - - uses: ljharb/require-allow-edits@main + - uses: ljharb/require-allow-edits@main diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 949264e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -version: ~> 1.0 -language: node_js -os: - - linux -import: - - ljharb/travis-ci:node/minors/gte_4.yml - - ljharb/travis-ci:node/minors/iojs.yml - - ljharb/travis-ci:node/pretest.yml - - ljharb/travis-ci:node/posttest.yml -node_js: - - "0.12" - - "0.11" -script: - - 'if [ -n "${ES5-}" ]; then npm run tests:es5 ; fi' - - 'if [ -n "${NATIVE-}" ]; then npm run test:native ; fi' -matrix: - include: - - node_js: "0.10" - env: ES5=true - - node_js: "node" - env: NATIVE=true ALLOW_FAILURE=true - allow_failures: - - env: NATIVE=true ALLOW_FAILURE=true From a103a3ef713c3b15245a11d8fda832246118012e Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 27 Dec 2021 09:24:31 -0800 Subject: [PATCH 5/6] [Fix] a poisoned `.then` should not be wrapped in an AggregateError --- implementation.js | 24 ++++++++++++------------ test/tests.js | 5 ++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/implementation.js b/implementation.js index e929d4c..9f07fdc 100644 --- a/implementation.js +++ b/implementation.js @@ -27,17 +27,17 @@ module.exports = function any(iterable) { var thrower = function (value) { return reject(C, value); }; - return all(C, map(iterate(iterable), function (item) { - var itemPromise = PromiseResolve(C, item); - try { + try { + return all(C, map(iterate(iterable), function (item) { + var itemPromise = PromiseResolve(C, item); return itemPromise.then(thrower, identity); - } catch (e) { - return e; - } - })).then( - function (errors) { - throw new AggregateError(errors, 'Every promise rejected'); - }, - identity - ); + })).then( + function (errors) { + throw new AggregateError(errors, 'Every promise rejected'); + }, + identity + ); + } catch (e) { + return reject(C, e); + } }; diff --git a/test/tests.js b/test/tests.js index a459c59..bd0be2c 100644 --- a/test/tests.js +++ b/test/tests.js @@ -85,15 +85,14 @@ module.exports = function (any, t) { }); t.test('poisoned .then', function (st) { - st.plan(2); + st.plan(1); var poison = new EvalError(); var promise = new Promise(function () {}); promise.then = function () { throw poison; }; any([promise]).then(function () { st.fail('should not reach here'); }, function (error) { - st.equal(error instanceof AggregateError, true, 'error is an AggregateError'); - st.deepEqual(error.errors, [poison], 'rejection showed up as expected'); + st.equal(error, poison, 'error is whatever the poisoned then throws'); }); }); From e1ea7587f44bf62a441ce4ddf5e792df18ba9bb0 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 27 Dec 2021 09:49:42 -0800 Subject: [PATCH 6/6] [Fix] remove an incorrect observable subclass `.then` call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There’s still one left --- implementation.js | 18 +++++++++--------- package.json | 3 +++ test/tests.js | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/implementation.js b/implementation.js index 9f07fdc..83a774f 100644 --- a/implementation.js +++ b/implementation.js @@ -14,10 +14,7 @@ var map = require('array.prototype.map'); var all = callBind(GetIntrinsic('%Promise.all%')); var reject = callBind(GetIntrinsic('%Promise.reject%')); - -var identity = function (x) { - return x; -}; +var $then = callBind(GetIntrinsic('%Promise.prototype.then%')); module.exports = function any(iterable) { var C = this; @@ -28,14 +25,17 @@ module.exports = function any(iterable) { return reject(C, value); }; try { - return all(C, map(iterate(iterable), function (item) { - var itemPromise = PromiseResolve(C, item); - return itemPromise.then(thrower, identity); - })).then( + return $then( + all(C, map(iterate(iterable), function (item) { + var itemPromise = PromiseResolve(C, item); + return itemPromise.then(thrower, function identity(x) { + return x; + }); + })), function (errors) { throw new AggregateError(errors, 'Every promise rejected'); }, - identity + function (x) { return x; } ); } catch (e) { return reject(C, e); diff --git a/package.json b/package.json index 0207ef2..e9f68f1 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,9 @@ "safe-publish-latest": "^2.0.0", "tape": "^5.4.0" }, + "testling": { + "files": "test/native.js" + }, "engines": { "node": ">= 0.4" } diff --git a/test/tests.js b/test/tests.js index bd0be2c..3bb4b83 100644 --- a/test/tests.js +++ b/test/tests.js @@ -88,7 +88,7 @@ module.exports = function (any, t) { st.plan(1); var poison = new EvalError(); var promise = new Promise(function () {}); - promise.then = function () { throw poison; }; + promise.then = function poisionedThen() { throw poison; }; any([promise]).then(function () { st.fail('should not reach here'); }, function (error) { @@ -123,7 +123,18 @@ module.exports = function (any, t) { any.call(Subclass, [original]); assertArray(s2t, original.thenArgs, 1); - assertArray(s2t, Subclass.thenArgs, 3); + assertArray(s2t, original.thenArgs[0], 2); + + s2t.test('proper subclass then invocation count', { todo: true }, function (s3t) { + // native implementations report 1, this implementation reports 2 + assertArray(s3t, Subclass.thenArgs, 1); + + s3t.end(); + }); + s2t.ok(Array.isArray(Subclass.thenArgs), 'value is an array'); + s2t.match(String(Subclass.thenArgs.length), /^[12]$/, 'length is 1 or 2'); + + assertArray(s2t, Subclass.thenArgs[0], 2); s2t.end(); });