build(client): Convert mocharc-common to ESM TypeScript in mocha-test-setup and test-version-utils#27385
Conversation
…-setup and test-version-utils Relocate mocharc-common.cjs into src/ as typed ESM TypeScript modules (mocharcCommon.ts) in both @fluid-internal/mocha-test-setup and @fluid-private/test-version-utils that now become part ot their respective `build:esnext` tasks. Key changes: - Update mocha-test-setup dependencies to have `tsc` depend on `build:esnext` - Replace CJS require/module.exports with ESM imports and named exports - Add FluidTestMochaConfig interface as a specific return type - Use fs.readFileSync + JSON.parse instead of require() for package.json - Use import.meta.url instead of __dirname - Add mocharc-common-cjswrapper.cjs for backward-compatible CJS require() - Remove tsconfig.cjs.json from test-version-utils (no longer needed) - Update package.json exports maps to point to new compiled output paths - Update downstream .mocharc.cjs consumers to use the new import paths - Fix all eslint issues (strict-boolean-expressions, dot-notation, no-param-reassign, no-nodejs-modules) - Switch .mocharc.cjs in webflow and test-version-utils packages to use getFluidTestMochaConfig from mocha-test-setup directly, as no version or layer compatibility tests are run in those packages Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Hi! Thank you for opening this PR. Want me to review it? Based on the diff (381 lines, 25 files), I've queued these reviewers:
How this works
|
There was a problem hiding this comment.
Pull request overview
This PR migrates shared Mocha configuration helpers (mocharc-common) from ad-hoc CommonJS files to typed ESM TypeScript modules in @fluid-internal/mocha-test-setup and @fluid-private/test-version-utils, updates package export maps/build wiring accordingly, and adjusts downstream .mocharc.cjs consumers to use the new entrypoints.
Changes:
- Convert
mocharc-commonlogic to ESM TypeScript (mocharcCommon.ts) and update consumers to import/require the new exports. - Update build wiring so the ESM build is produced as part of package build flows; remove the standalone CJS-only tsconfig in
test-version-utils. - Introduce a CJS wrapper entrypoint for
@fluid-internal/mocha-test-setup/mocharc-commonand update several.mocharc.cjsfiles to use the new sources.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/test/test-version-utils/tsconfig.cjs.json | Removes the CJS-only tsconfig previously used to build compat options for Mocha config consumption. |
| packages/test/test-version-utils/src/mocharcCommon.ts | Adds ESM/TS implementation for generating compat-aware Mocha config. |
| packages/test/test-version-utils/package.json | Updates exports/scripts to point ./mocharc-common at compiled ESM output and adjusts test scripts. |
| packages/test/test-version-utils/mocharc-common.cjs | Deletes the legacy CJS Mocha config helper. |
| packages/test/test-version-utils/.mocharc.cjs | Switches to consuming Mocha config from @fluid-internal/mocha-test-setup. |
| packages/test/test-end-to-end-tests/.mocharc.cjs | Updates compat test Mocha config consumption to the new named export API. |
| packages/test/mocha-test-setup/src/mocharcCommon.ts | Converts implementation to ESM/TS, adds a typed config interface, replaces __dirname/require() usages with ESM equivalents. |
| packages/test/mocha-test-setup/package.json | Updates exports and build task wiring for the new ESM-based implementation and wrapper. |
| packages/test/mocha-test-setup/mocharc-common-cjswrapper.cjs | Adds a CJS wrapper intended to support require("@fluid-internal/mocha-test-setup/mocharc-common"). |
| packages/service-clients/end-to-end-tests/odsp-client/src/test/.mocharc.cjs | Updates compat Mocha config consumption to new named export API. |
| packages/service-clients/end-to-end-tests/azure-client/src/test/.mocharc.cjs | Updates compat Mocha config consumption to new named export API. |
| examples/data-objects/webflow/.mocharc.cjs | Switches Mocha config source to @fluid-internal/mocha-test-setup/mocharc-common. |
| packages/common/client-utils/package.json | Adjusts fluidBuild.tasks dependency wiring for test/build tasks. |
| /** | ||
| * Mocha configuration object returned by {@link getFluidTestMochaConfig}. | ||
| */ | ||
| export interface FluidTestMochaConfig { |
There was a problem hiding this comment.
Given that https://nodejs.org/learn/typescript/run-natively exists now, it's probably practical to implement this file in typescript without a build step, and just import it directly from JS config files as a ts file.
I'm not convinced that would be better, but just pointing out it may be an option now (and wasn't recently).
There was a problem hiding this comment.
That is super interesting. The docs don't comment on how existence of a .js import in a natively run .ts file are handled. The suggestion is that this aren't handled. I haven't tested that.
There are further suggestion to only employ that when using TypeScript 5.8 or later, which we aren't at yet. (Maybe in a couple months.)
So, I have #27388 out to just use TS natively, but that may not stand.
There was a problem hiding this comment.
Doesn't work out - native TS is not supported for exports.
It looked like it worked, but it fails when package is used from installed package. (Can't be under node_modules.)
Locally it is also consumed from a node_modules path but that is the relative path and symbolic links resolve to a path that doesn't have node_modules.
| "use strict"; | ||
|
|
||
| const getFluidTestMochaConfig = require("./mocharc-common.cjs"); | ||
| const { getFluidTestMochaConfig } = require("@fluid-internal/mocha-test-setup/mocharc-common"); |
There was a problem hiding this comment.
I suspect this is fine, but we might want to make this file (and the others in this PR which you had to modify anyway) .mjs and just do everything we can as full ESM. Mocha has instructions for pure ESM config: https://mochajs.org/running/configuring/
There was a problem hiding this comment.
Turns out the docs are quite misleading. In v12, mocha added ESM config support. But the config section is bannered by since v6. The v12 PR didn't qualify things when docs were modified.
In the end, I figured out how to use ESM config with v11 anyway. Since the config files have a single export that is the config, a "modules.exports" export that is the config.
attw (Are The Types Wrongs) doesn't directly allow CJS to resolve to ESM. 1. Suppress cjs-resolves-to-esm rule for mocha-test-setup. 2. Update test-version-utils users to strictly use ESM as they mostly did already. And remove CJS from config export /mocharc-common. - allow mocharc.js to exist in client packages.
…mport.meta.dirname supported since Node v21
Exclude Node10 check since mocharc-common.cjs was renamed to prevent confusion that it is always the import path.
Relocate mocharc-common.cjs into src/ as typed ESM TypeScript modules (mocharcCommon.ts) in both @fluid-internal/mocha-test-setup and @fluid-private/test-version-utils that now become part ot their respective
build:esnexttasks.Key changes:
tscdepend onbuild:esnexttype: module)