Skip to content

build(client): Convert mocharc-common to ESM TypeScript in mocha-test-setup and test-version-utils#27385

Merged
Abe27342 merged 13 commits into
microsoft:mainfrom
jason-ha:test/esm-only-test-version-utils
May 28, 2026
Merged

build(client): Convert mocharc-common to ESM TypeScript in mocha-test-setup and test-version-utils#27385
Abe27342 merged 13 commits into
microsoft:mainfrom
jason-ha:test/esm-only-test-version-utils

Conversation

@jason-ha
Copy link
Copy Markdown
Contributor

@jason-ha jason-ha commented May 26, 2026

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.dirname instead of __dirname
  • Add mocharc-common-cjswrapper.cjs in mocha-test-setup for backward-compatible CJS require()
  • Remove tsconfig.cjs.json_s (no longer needed) and unused tsconfig.esnext.json
  • Update package.json exports maps to point to new compiled output 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
  • Update remaining test-version-utils/mocharc-common consumers (end-to-end-tests) to ESM mocha configs (.mocharc.js since packages are type: module)

…-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>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 26, 2026

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:

  • Correctness — logic errors, race conditions, lifecycle issues
  • Security — vulnerabilities, secret exposure, injection
  • API Compatibility — breaking changes, release tags, type design
  • Performance — algorithmic regressions, memory leaks
  • Testing — coverage gaps, hollow tests

How this works

  • Adjust the reviewer set by ticking/unticking boxes above. Reviewer toggles alone don't trigger anything.

  • Tick Start review below to dispatch the review fleet.

  • After review finishes, tick Start review again to request another run — it auto-resets after each dispatch.

  • This comment updates as new commits land; your reviewer selections are preserved.

  • Start review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-common logic 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-common and update several .mocharc.cjs files 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.

Comment thread packages/test/mocha-test-setup/mocharc-common-cjswrapper.cjs
Comment thread packages/test/test-version-utils/package.json
Comment thread packages/test/test-end-to-end-tests/.mocharc.cjs Outdated
Comment thread packages/test/test-version-utils/.mocharc.cjs Outdated
Comment thread packages/service-clients/end-to-end-tests/azure-client/src/test/.mocharc.cjs Outdated
Comment thread packages/service-clients/end-to-end-tests/odsp-client/src/test/.mocharc.cjs Outdated
Comment thread examples/data-objects/webflow/.mocharc.cjs
Comment thread packages/test/mocha-test-setup/package.json
Comment thread packages/test/mocha-test-setup/src/mocharcCommon.ts Outdated
/**
* Mocha configuration object returned by {@link getFluidTestMochaConfig}.
*/
export interface FluidTestMochaConfig {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jason-ha jason-ha requested a review from CraigMacomber May 26, 2026 18:45
"use strict";

const getFluidTestMochaConfig = require("./mocharc-common.cjs");
const { getFluidTestMochaConfig } = require("@fluid-internal/mocha-test-setup/mocharc-common");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

jason-ha added 6 commits May 26, 2026 14:19
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.
Exclude Node10 check since mocharc-common.cjs was renamed to prevent confusion that it is always the import path.
@jason-ha jason-ha marked this pull request as draft May 27, 2026 20:01
@jason-ha jason-ha removed the request for review from daesunp May 27, 2026 20:01
@jason-ha jason-ha marked this pull request as ready for review May 28, 2026 08:39
@jason-ha jason-ha enabled auto-merge (squash) May 28, 2026 20:49
@jason-ha jason-ha added the release-blocking Must be addressed before we cut and publish the next release label May 28, 2026
@Abe27342 Abe27342 disabled auto-merge May 28, 2026 22:09
@Abe27342 Abe27342 merged commit 869678e into microsoft:main May 28, 2026
45 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-blocking Must be addressed before we cut and publish the next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants