From b07d38cc2d85c6127391686482abf762758dce34 Mon Sep 17 00:00:00 2001 From: Steven Lambert <2433219+straker@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:17:32 -0700 Subject: [PATCH] fix(webdriverjs): fix default commonJs export (#927) * fix(webdriverjs): fix default commonJs export * fix react building on test * fix now? * revert example update * support 4.7.3 --- .github/workflows/tests.yml | 17 +++++++------ packages/playwright/package.json | 2 ++ packages/playwright/test/commonjsTest.js | 11 ++++++++ packages/playwright/test/esmTest.mjs | 7 ++++-- packages/puppeteer/package.json | 2 ++ packages/puppeteer/test/commonjsTest.js | 11 ++++++++ packages/puppeteer/test/esmTest.mjs | 13 +++++----- packages/react/package.json | 4 ++- packages/react/setupGlobals.mjs | 4 --- packages/react/test/commonjsTest.js | 8 ++++++ packages/react/{ => test}/esmTest.mjs | 6 +++-- packages/react/test/setupGlobals.mjs | 2 ++ packages/reporter-earl/jest.config.js | 2 +- packages/reporter-earl/package.json | 4 ++- packages/reporter-earl/tests/commonjsTest.js | 6 +++++ .../reporter-earl/{ => tests}/esmTest.mjs | 3 ++- packages/webdriverio/package.json | 2 ++ packages/webdriverio/test/commonjsTest.js | 11 ++++++++ packages/webdriverio/test/esmTest.mjs | 6 +++-- packages/webdriverjs/package.json | 2 ++ packages/webdriverjs/src/index.ts | 7 ++++++ packages/webdriverjs/test/commonjsTest.js | 25 +++++++++++++++++++ packages/webdriverjs/test/esmTest.mjs | 7 ++++-- 23 files changed, 133 insertions(+), 29 deletions(-) create mode 100644 packages/playwright/test/commonjsTest.js create mode 100644 packages/puppeteer/test/commonjsTest.js delete mode 100644 packages/react/setupGlobals.mjs create mode 100644 packages/react/test/commonjsTest.js rename packages/react/{ => test}/esmTest.mjs (72%) create mode 100644 packages/react/test/setupGlobals.mjs create mode 100644 packages/reporter-earl/tests/commonjsTest.js rename packages/reporter-earl/{ => tests}/esmTest.mjs (60%) create mode 100644 packages/webdriverio/test/commonjsTest.js create mode 100644 packages/webdriverjs/test/commonjsTest.js diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a897ab4a..5505215e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -41,7 +41,7 @@ jobs: working-directory: packages/puppeteer - run: npm run build --workspace=packages/puppeteer - run: npm run coverage --workspace=packages/puppeteer - - run: npm run test:esm --workspace=packages/puppeteer + - run: npm run test:export --workspace=packages/puppeteer cli: strategy: @@ -83,7 +83,7 @@ jobs: working-directory: packages/webdriverjs - run: npm run build --workspace=packages/webdriverjs - run: npm run coverage --workspace=packages/webdriverjs - - run: npm run test:esm --workspace=packages/webdriverjs + - run: npm run test:export --workspace=packages/webdriverjs webdriverio: strategy: @@ -105,7 +105,7 @@ jobs: working-directory: packages/webdriverio - run: npm run build --workspace=packages/webdriverio - run: npm run coverage --workspace=packages/webdriverio - - run: npm run test:esm --workspace=packages/webdriverio + - run: npm run test:export --workspace=packages/webdriverio reporter_earl: strategy: @@ -123,7 +123,7 @@ jobs: - run: npm ci - run: npm run build --workspace=packages/reporter-earl - run: npm run test --workspace=packages/reporter-earl - - run: npm run test:esm --workspace=packages/reporter-earl + - run: npm run test:export --workspace=packages/reporter-earl react: strategy: @@ -139,9 +139,12 @@ jobs: node-version: ${{ matrix.node }} cache: 'npm' - run: npm ci - - run: npm run build --workspace=packages/react - run: npm run test --workspace=packages/react - - run: npm run test:esm --workspace=packages/react + # the tests builds the project using tsc and relies on `cache.ts` to be + # built and be it's own file. however we don't want that for the export + # test so we need to rebuild using tsup + - run: npm run build --workspace=packages/react + - run: npm run test:export --workspace=packages/react playwright: strategy: @@ -160,7 +163,7 @@ jobs: - run: npx playwright install --with-deps - run: npm run build --workspace=packages/playwright - run: npm run coverage --workspace=packages/playwright - - run: npm run test:esm --workspace=packages/playwright + - run: npm run test:export --workspace=packages/playwright wdio_globals_test: strategy: diff --git a/packages/playwright/package.json b/packages/playwright/package.json index 92439c88..b9f4d914 100644 --- a/packages/playwright/package.json +++ b/packages/playwright/package.json @@ -43,7 +43,9 @@ "prebuild": "rimraf dist", "build": "tsup src/index.ts --dts --format esm,cjs", "test": "mocha --timeout 60000 -r ts-node/register 'test/**.spec.ts'", + "test:export": "npm run test:esm && npm run test:commonjs", "test:esm": "node test/esmTest.mjs", + "test:commonjs": "node test/commonjsTest.js", "coverage": "nyc npm run test", "prepare": "npx playwright install && npm run build" }, diff --git a/packages/playwright/test/commonjsTest.js b/packages/playwright/test/commonjsTest.js new file mode 100644 index 00000000..399ed177 --- /dev/null +++ b/packages/playwright/test/commonjsTest.js @@ -0,0 +1,11 @@ +// ensure backwards compatibility of commonJs format +const defaultExport = require('../dist/index.js').default; +const { AxeBuilder } = require('../dist/index.js'); +const assert = require('assert'); + +assert(typeof defaultExport === 'function', 'default export is not a function'); +assert(typeof AxeBuilder === 'function', 'named export is not a function'); +assert( + defaultExport === AxeBuilder, + 'default and named export are not the same' +); diff --git a/packages/playwright/test/esmTest.mjs b/packages/playwright/test/esmTest.mjs index 0a2ffa4d..eb852e5a 100644 --- a/packages/playwright/test/esmTest.mjs +++ b/packages/playwright/test/esmTest.mjs @@ -1,5 +1,8 @@ +// ensure compatibility of ESM format import defaultExport from '../dist/index.mjs'; +import { AxeBuilder } from '../dist/index.mjs'; import assert from 'assert'; -const exportIsFunction = typeof defaultExport === 'function'; -assert(exportIsFunction, 'export is not a function'); +assert(typeof defaultExport === 'function', 'default export is not a function'); +assert(typeof AxeBuilder === 'function', 'named export is not a function') +assert(defaultExport === AxeBuilder, 'default and named export are not the same'); \ No newline at end of file diff --git a/packages/puppeteer/package.json b/packages/puppeteer/package.json index 24834a3a..9331a8c4 100644 --- a/packages/puppeteer/package.json +++ b/packages/puppeteer/package.json @@ -25,7 +25,9 @@ "scripts": { "build": "tsup src/index.ts --dts --format esm,cjs", "test": "mocha --timeout 60000 -r ts-node/register 'test/**.spec.ts'", + "test:export": "npm run test:esm && npm run test:commonjs", "test:esm": "node test/esmTest.mjs", + "test:commonjs": "node test/commonjsTest.js", "coverage": "nyc npm run test", "prepublishOnly": "npm run build" }, diff --git a/packages/puppeteer/test/commonjsTest.js b/packages/puppeteer/test/commonjsTest.js new file mode 100644 index 00000000..c051ef53 --- /dev/null +++ b/packages/puppeteer/test/commonjsTest.js @@ -0,0 +1,11 @@ +// ensure backwards compatibility of commonJs format +const defaultExport = require('../dist/index.js').default; +const { AxePuppeteer } = require('../dist/index.js'); +const assert = require('assert'); + +assert(typeof defaultExport === 'function', 'default export is not a function'); +assert(typeof AxePuppeteer === 'function', 'named export is not a function'); +assert( + defaultExport === AxePuppeteer, + 'default and named export are not the same' +); diff --git a/packages/puppeteer/test/esmTest.mjs b/packages/puppeteer/test/esmTest.mjs index f1d61ba2..5861801b 100644 --- a/packages/puppeteer/test/esmTest.mjs +++ b/packages/puppeteer/test/esmTest.mjs @@ -1,14 +1,15 @@ -import defaultExport, { AxePuppeteer } from '../dist/index.mjs'; +// ensure compatibility of ESM format +import defaultExport from '../dist/index.mjs'; +import { AxePuppeteer } from '../dist/index.mjs'; import assert from 'assert'; import puppeteer from 'puppeteer'; import { fileURLToPath, pathToFileURL } from 'url'; import { join } from 'path'; import { fixturesPath } from 'axe-test-fixtures'; -const exportIsFunction = typeof defaultExport === 'function'; -const exportIsSame = defaultExport === AxePuppeteer; -assert(exportIsFunction, 'export is not a function'); -assert(exportIsSame, 'default and named export is not the same'); +assert(typeof defaultExport === 'function', 'default export is not a function'); +assert(typeof AxePuppeteer === 'function', 'named export is not a function') +assert(defaultExport === AxePuppeteer, 'default and named export are not the same'); const options = {}; @@ -30,4 +31,4 @@ async function integrationTest() { await page.close(); await browser.close(); } -integrationTest(); +integrationTest(); \ No newline at end of file diff --git a/packages/react/package.json b/packages/react/package.json index a1bf841d..4dc39c53 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -28,7 +28,9 @@ "build": "tsup index.ts --dts --format esm,cjs", "prepare": "npm run build", "test": "tsc && npm run test:types && jest", - "test:esm": "node esmTest.mjs", + "test:export": "npm run test:esm && npm run test:commonjs", + "test:esm": "node test/esmTest.mjs", + "test:commonjs": "node test/commonjsTest.js", "test:types": "cd test && tsc" }, "keywords": [ diff --git a/packages/react/setupGlobals.mjs b/packages/react/setupGlobals.mjs deleted file mode 100644 index 62080241..00000000 --- a/packages/react/setupGlobals.mjs +++ /dev/null @@ -1,4 +0,0 @@ -global.window = {}; -global.document = {}; - -export default {}; diff --git a/packages/react/test/commonjsTest.js b/packages/react/test/commonjsTest.js new file mode 100644 index 00000000..b4732c0a --- /dev/null +++ b/packages/react/test/commonjsTest.js @@ -0,0 +1,8 @@ +// ensure backwards compatibility of commonJs format +global.window = {}; +global.document = {}; + +const defaultExport = require('../dist/index.js'); +const assert = require('assert'); + +assert(typeof defaultExport === 'function', 'export is not a function'); diff --git a/packages/react/esmTest.mjs b/packages/react/test/esmTest.mjs similarity index 72% rename from packages/react/esmTest.mjs rename to packages/react/test/esmTest.mjs index a3c3e891..f1ef59f7 100644 --- a/packages/react/esmTest.mjs +++ b/packages/react/test/esmTest.mjs @@ -1,8 +1,10 @@ +// ensure compatibility of ESM format + // in order to properly set global placeholders for `window` and `document` we have to // import a file that does that. // Setting them in this file will not work. -import _ from './setupGlobals.mjs'; -import defaultExport from './dist/index.mjs'; +import './setupGlobals.mjs'; +import defaultExport from '../dist/index.mjs'; import assert from 'assert'; const exportIsFunction = typeof defaultExport === 'function'; diff --git a/packages/react/test/setupGlobals.mjs b/packages/react/test/setupGlobals.mjs new file mode 100644 index 00000000..ce0265f4 --- /dev/null +++ b/packages/react/test/setupGlobals.mjs @@ -0,0 +1,2 @@ +global.window = {}; +global.document = {}; \ No newline at end of file diff --git a/packages/reporter-earl/jest.config.js b/packages/reporter-earl/jest.config.js index 93f303c3..b6950a33 100755 --- a/packages/reporter-earl/jest.config.js +++ b/packages/reporter-earl/jest.config.js @@ -4,7 +4,7 @@ module.exports = { transform: { '\\.(ts|tsx)$': 'ts-jest' }, - testRegex: '/tests/.*\\.(ts|tsx|js)$', + testPathDirs: 'tests', testPathIgnorePatterns: ['/node_modules/', '/dist/', '/tests/utils.ts'], silent: false, coverageThreshold: { diff --git a/packages/reporter-earl/package.json b/packages/reporter-earl/package.json index 775d87d0..df8368da 100644 --- a/packages/reporter-earl/package.json +++ b/packages/reporter-earl/package.json @@ -15,7 +15,9 @@ "scripts": { "start": "NODE_OPTIONS=--experimental-vm-modules jest --watch --env=jsdom", "test": "npm run build && npm run test:unit", - "test:esm": "node esmTest.mjs", + "test:export": "npm run test:esm && npm run test:commonjs", + "test:esm": "node tests/esmTest.mjs", + "test:commonjs": "node tests/commonjsTest.js", "test:unit": "NODE_OPTIONS=--experimental-vm-modules jest --collectCoverage", "build": "tsup src/axeReporterEarl.ts --dts --format esm,cjs", "prepublishOnly": "npm run build" diff --git a/packages/reporter-earl/tests/commonjsTest.js b/packages/reporter-earl/tests/commonjsTest.js new file mode 100644 index 00000000..6bf9a838 --- /dev/null +++ b/packages/reporter-earl/tests/commonjsTest.js @@ -0,0 +1,6 @@ +// ensure backwards compatibility of commonJs format +const defaultExport = require('../dist/axeReporterEarl.js').default; +const assert = require('assert'); + +const exportIsFunction = typeof defaultExport === 'function'; +assert(exportIsFunction, 'export is not a function'); diff --git a/packages/reporter-earl/esmTest.mjs b/packages/reporter-earl/tests/esmTest.mjs similarity index 60% rename from packages/reporter-earl/esmTest.mjs rename to packages/reporter-earl/tests/esmTest.mjs index 9addf7b8..b73557fc 100644 --- a/packages/reporter-earl/esmTest.mjs +++ b/packages/reporter-earl/tests/esmTest.mjs @@ -1,4 +1,5 @@ -import defaultExport from './dist/axeReporterEarl.mjs'; +// ensure compatibility of ESM format +import defaultExport from '../dist/axeReporterEarl.mjs'; import assert from 'assert'; const exportIsFunction = typeof defaultExport === 'function'; diff --git a/packages/webdriverio/package.json b/packages/webdriverio/package.json index 8c21a684..3eed32b4 100644 --- a/packages/webdriverio/package.json +++ b/packages/webdriverio/package.json @@ -29,7 +29,9 @@ "prebuild": "rimraf dist", "build": "tsup src/index.ts --dts --format esm,cjs", "test": "mocha --timeout 60000 -r ts-node/register 'test/**.spec.ts'", + "test:export": "npm run test:esm && npm run test:commonjs", "test:esm": "node test/esmTest.mjs", + "test:commonjs": "node test/commonjsTest.js", "coverage": "nyc npm run test", "prepare": "npm run build" }, diff --git a/packages/webdriverio/test/commonjsTest.js b/packages/webdriverio/test/commonjsTest.js new file mode 100644 index 00000000..399ed177 --- /dev/null +++ b/packages/webdriverio/test/commonjsTest.js @@ -0,0 +1,11 @@ +// ensure backwards compatibility of commonJs format +const defaultExport = require('../dist/index.js').default; +const { AxeBuilder } = require('../dist/index.js'); +const assert = require('assert'); + +assert(typeof defaultExport === 'function', 'default export is not a function'); +assert(typeof AxeBuilder === 'function', 'named export is not a function'); +assert( + defaultExport === AxeBuilder, + 'default and named export are not the same' +); diff --git a/packages/webdriverio/test/esmTest.mjs b/packages/webdriverio/test/esmTest.mjs index d0544c51..851855f2 100644 --- a/packages/webdriverio/test/esmTest.mjs +++ b/packages/webdriverio/test/esmTest.mjs @@ -1,12 +1,14 @@ import defaultExport from '../dist/index.mjs'; +import { AxeBuilder } from '../dist/index.mjs'; import assert from 'assert'; import * as webdriverio from 'webdriverio'; import { fileURLToPath, pathToFileURL } from 'url'; import { join } from 'path'; import { fixturesPath } from 'axe-test-fixtures'; -const exportIsFunction = typeof defaultExport === 'function'; -assert(exportIsFunction, 'export is not a function'); +assert(typeof defaultExport === 'function', 'default export is not a function'); +assert(typeof AxeBuilder === 'function', 'named export is not a function') +assert(defaultExport === AxeBuilder, 'default and named export are not the same'); async function integrationTest() { const path = join(fixturesPath, 'index.html'); diff --git a/packages/webdriverjs/package.json b/packages/webdriverjs/package.json index 0fbdaf5b..6be01e67 100644 --- a/packages/webdriverjs/package.json +++ b/packages/webdriverjs/package.json @@ -49,7 +49,9 @@ "prebuild": "rimraf dist", "build": "tsup src/index.ts --dts --format esm,cjs", "test": "mocha --timeout 60000 -r ts-node/register 'test/**.spec.ts'", + "test:export": "npm run test:esm && npm run test:commonjs", "test:esm": "node test/esmTest.mjs", + "test:commonjs": "node test/commonjsTest.js", "coverage": "nyc npm run test", "prepare": "npm run build" }, diff --git a/packages/webdriverjs/src/index.ts b/packages/webdriverjs/src/index.ts index 8d035816..c1aab37b 100644 --- a/packages/webdriverjs/src/index.ts +++ b/packages/webdriverjs/src/index.ts @@ -291,4 +291,11 @@ export default class AxeBuilder { } } +// ensure backwards compatibility with commonJs default export +if (typeof module === 'object') { + module.exports = AxeBuilder; + module.exports.default = AxeBuilder; + module.exports.AxeBuilder = AxeBuilder; +} + export { AxeBuilder }; diff --git a/packages/webdriverjs/test/commonjsTest.js b/packages/webdriverjs/test/commonjsTest.js new file mode 100644 index 00000000..b80f4fd6 --- /dev/null +++ b/packages/webdriverjs/test/commonjsTest.js @@ -0,0 +1,25 @@ +// ensure backwards compatibility of commonJs format +const implicitDefaultExport = require('../dist/index.js'); // support <4.7.3 +const explicitDefaultExport = require('../dist/index.js').default; // support 4.7.3+ +const { AxeBuilder } = require('../dist/index.js'); +const assert = require('assert'); + +assert(typeof AxeBuilder === 'function', 'named export is not a function'); + +assert( + typeof implicitDefaultExport === 'function', + 'implicit default export is not a function' +); +assert( + implicitDefaultExport === AxeBuilder, + 'implicit default and named export are not the same' +); + +assert( + typeof explicitDefaultExport === 'function', + 'explicit default export is not a function' +); +assert( + explicitDefaultExport === AxeBuilder, + 'explicit default and named export are not the same' +); diff --git a/packages/webdriverjs/test/esmTest.mjs b/packages/webdriverjs/test/esmTest.mjs index 0a2ffa4d..eb852e5a 100644 --- a/packages/webdriverjs/test/esmTest.mjs +++ b/packages/webdriverjs/test/esmTest.mjs @@ -1,5 +1,8 @@ +// ensure compatibility of ESM format import defaultExport from '../dist/index.mjs'; +import { AxeBuilder } from '../dist/index.mjs'; import assert from 'assert'; -const exportIsFunction = typeof defaultExport === 'function'; -assert(exportIsFunction, 'export is not a function'); +assert(typeof defaultExport === 'function', 'default export is not a function'); +assert(typeof AxeBuilder === 'function', 'named export is not a function') +assert(defaultExport === AxeBuilder, 'default and named export are not the same'); \ No newline at end of file