From cd0fc36b988de2d1734ef36e1ab3d84e501b035b Mon Sep 17 00:00:00 2001 From: James Wright Date: Wed, 8 Sep 2021 11:16:38 -0700 Subject: [PATCH] Run Test262 tests against production artifacts. --- .github/workflows/test.yml | 12 +++++++++++ .gitignore | 1 + .npmignore | 2 ++ lib/init.cjs | 26 ----------------------- lib/init.ts | 42 ++++++++++++++++++++++++++++++++++++++ package.json | 6 ++++-- rollup.config.js | 41 ++++++++++++++++++++++++++++++++++--- test/test262.sh | 18 ++++++++++++++++ tsconfig.json | 2 +- 9 files changed, 118 insertions(+), 32 deletions(-) delete mode 100644 lib/init.cjs create mode 100644 lib/init.ts create mode 100755 test/test262.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 41146c4a..f20f84a1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,3 +25,15 @@ jobs: node-version: 14.x - run: npm ci --no-optional - run: npm test + test-test262: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: use node.js v16.x + uses: actions/setup-node@v1 + with: + node-version: 16.x + - run: npm ci + - run: npm run test262 + env: + HEAD_SHA: ${{ github.event.pull_request.head.sha }} diff --git a/.gitignore b/.gitignore index d7c7c711..8656030f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ tsc-out/ .vscode/* !.vscode/launch.json *.tgz +test/tc39 diff --git a/.npmignore b/.npmignore index 77d4e578..37624212 100644 --- a/.npmignore +++ b/.npmignore @@ -4,3 +4,5 @@ types tsconfig.json *.sh .vscode +dist/script.js +dist/playground.cjs diff --git a/lib/init.cjs b/lib/init.cjs deleted file mode 100644 index efc5d487..00000000 --- a/lib/init.cjs +++ /dev/null @@ -1,26 +0,0 @@ -if (module.parent && module.parent.id === 'internal/preload') { - // Running unbundled as 'npm run playground' - globalThis.__debug__ = true; -} - -import('../dist/index.cjs') - .then(({ Temporal, Intl, toTemporalInstant }) => { - globalThis.Temporal = { ...Temporal }; - Object.defineProperty(globalThis.Temporal, Symbol.toStringTag, { - value: 'Temporal', - writable: false, - enumerable: false, - configurable: true - }); - Object.assign(globalThis.Intl, Intl); - Object.defineProperty(Date.prototype, 'toTemporalInstant', { - value: toTemporalInstant, - writable: true, - enumerable: false, - configurable: true - }); - }) - .catch((err) => { - console.error(err); - process.exit(1); - }); diff --git a/lib/init.ts b/lib/init.ts new file mode 100644 index 00000000..70a24f94 --- /dev/null +++ b/lib/init.ts @@ -0,0 +1,42 @@ +// This is an alternate entry point that polyfills Temporal onto the global +// object. This is used only for the browser playground and the test262 tests. +// See the note in index.mjs. + +import * as Temporal from './temporal'; +import * as Intl from './intl'; +import { toTemporalInstant } from './legacydate'; + +Object.defineProperty(globalThis, 'Temporal', { + value: {}, + writable: true, + enumerable: false, + configurable: true +}); +copy(globalThis.Temporal, Temporal); +Object.defineProperty(globalThis.Temporal, Symbol.toStringTag, { + value: 'Temporal', + writable: false, + enumerable: false, + configurable: true +}); +copy(globalThis.Temporal.Now, Temporal.Now); +copy(globalThis.Intl, Intl); +Object.defineProperty(globalThis.Date.prototype, 'toTemporalInstant', { + value: toTemporalInstant, + writable: true, + enumerable: false, + configurable: true +}); + +function copy(target, source) { + for (const prop of Object.getOwnPropertyNames(source)) { + Object.defineProperty(target, prop, { + value: source[prop], + writable: true, + enumerable: false, + configurable: true + }); + } +} + +export { Temporal, Intl, toTemporalInstant }; diff --git a/package.json b/package.json index 73ea3f97..ddc52c5c 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,11 @@ "types": "./index.d.ts", "scripts": { "test": "tsc && node --no-warnings --experimental-modules --experimental-specifier-resolution=node --icu-data-dir node_modules/full-icu --loader ./test/resolve.source.mjs ./test/all.mjs", - "build": "rollup -c rollup.config.js", + "test262": "./test/test262.sh", + "build": "rm -rf dist/* && rollup -c rollup.config.js", + "prepare": "npm run build", "prepublishOnly": "npm run build", - "playground": "npm run build && node --experimental-modules --no-warnings --icu-data-dir node_modules/full-icu -r ./lib/init.cjs", + "playground": "TEMPORAL_PLAYGROUND=1 npm run build && node --experimental-modules --no-warnings --icu-data-dir node_modules/full-icu -r ./dist/playground.cjs", "lint": "eslint . --ext ts,js,mjs,.d.ts --max-warnings 0 --cache \"$@\"", "postlint": "npm run tscheck", "pretty": "eslint . --ext ts,js,mjs,.d.ts --fix", diff --git a/rollup.config.js b/rollup.config.js index 049788d4..ebd947a0 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -7,14 +7,16 @@ import { terser } from 'rollup-plugin-terser'; import { env } from 'process'; import pkg from './package.json'; -const isProduction = env.NODE_ENV === 'production'; +const isPlaygroundBuild = !!env.TEMPORAL_PLAYGROUND; +const isTest262 = !!env.TEST262; +const isProduction = env.NODE_ENV === 'production' && !isTest262; const libName = 'temporal'; const plugins = [ typescript({ typescript: require('typescript') }), - replace({ exclude: 'node_modules/**', 'globalThis.__debug__': !isProduction, preventAssignment: true }), + replace({ exclude: 'node_modules/**', 'globalThis.__debug__': !isTest262 && !isProduction, preventAssignment: true }), resolve({ preferBuiltins: false }), commonjs(), babel({ @@ -51,7 +53,7 @@ function outputEntry(file, format) { }; } -export default [ +let builds = [ { input, external, @@ -74,3 +76,36 @@ export default [ plugins } ]; + +if (isTest262) { + builds = [ + { + input: 'lib/init.ts', + output: { + name: libName, + file: 'dist/script.js', + format: 'iife', + sourcemap: true + }, + plugins + } + ]; +} + +if (isPlaygroundBuild) { + builds = [ + { + input: 'lib/init.ts', + output: { + name: libName, + file: 'dist/playground.cjs', + format: 'cjs', + exports: 'named', + sourcemap: true + }, + plugins + } + ]; +} + +export default builds; diff --git a/test/test262.sh b/test/test262.sh new file mode 100755 index 00000000..3b0151dd --- /dev/null +++ b/test/test262.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -e +cd "$(dirname "$0")" +if [ ! -d "tc39" ]; then + git clone --depth=1 https://github.com/tc39/proposal-temporal.git tc39 +else + cd ./tc39 + git fetch origin + git merge --ff-only origin/main +fi + +cd ../ && TEST262=1 npm run build +cd tc39/polyfill && npm install +if [ ! -z "$TESTS" ]; then + PRELUDE=../../../dist/script.js npm run test262 "$TESTS" +else + PRELUDE=../../../dist/script.js npm run test262 +fi diff --git a/tsconfig.json b/tsconfig.json index e27b6e6e..9e3f7e94 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -37,7 +37,7 @@ // "strictNullChecks": true, // "strictPropertyInitialization": true, "stripInternal": true, - "target": "es5", + "target": "es2015", "outDir": "tsc-out/", "types": [] },