Skip to content

feat: add Mocha support - #116

Merged
cbasitodx merged 2 commits into
mainfrom
sebas.conde/feat/add_mocha_support-v2
Aug 6, 2026
Merged

feat: add Mocha support#116
cbasitodx merged 2 commits into
mainfrom
sebas.conde/feat/add_mocha_support-v2

Conversation

@cbasitodx

Copy link
Copy Markdown
Contributor

What

Add Mocha 8+ support to ddtest for JavaScript suite-level planning and execution.

The implementation uses a small embedded Node adapter to load Mocha's effective configuration and collect files without loading test modules. During execution it replaces configured spec inputs with the files assigned to the current ddtest worker while preserving other Mocha options, including shared --file setup. It also adds framework detection, Datadog initialization handling, documentation, unit/integration coverage, and a Mocha 8/9/10/11 compatibility CI matrix.

Why

Appending selected files directly to Mocha is not sufficient because Mocha combines positional files with configured spec patterns, which can make every worker run the full suite. Mocha's discovery internals also changed across supported versions: Mocha 8 and 9 return a file array, Mocha 10+ returns a collection object, and Mocha 8 does not accept pre-parsed CLI options.

This adapter handles those differences explicitly while avoiding --dry-run, which is unavailable on Mocha 8 and loads project test/setup code during planning.

E2E testing

Automated validation completed locally:

  • go test ./internal/framework ./internal/platform
  • Real Mocha 11.7.6 adapter test covering configured discovery, shared --file setup, selected-file execution, and config-free default discovery
  • All repository Go tests except internal/testoptimization/api
  • make lint (format, vet, and golangci-lint; 0 issues)
  • go build -o /private/tmp/ddtest-mocha-bin main.go
  • GitHub Actions workflow YAML parsing and git diff --check

The existing internal/testoptimization/api tests could not run locally because this sandbox denies the loopback port used by httptest.NewServer. The PR's normal CI environment is not subject to that restriction.

Manual scenario:

  1. Configure a Mocha project with a .mocharc.json whose spec matches multiple files and whose file loads shared setup.
  2. Run ddtest plan --platform javascript --framework mocha and confirm only Mocha-selected test files are planned; shared setup is not partitioned.
  3. Run ddtest run --platform javascript --framework mocha with multiple workers.
  4. Confirm each worker runs only its assigned files, shared setup loads for every worker, and test events are instrumented through dd-trace/ci/init.

@cbasitodx
cbasitodx requested a review from a team as a code owner August 6, 2026 12:43
@cbasitodx
cbasitodx merged commit 6e5b82e into main Aug 6, 2026
20 checks passed
@cbasitodx
cbasitodx deleted the sebas.conde/feat/add_mocha_support-v2 branch August 6, 2026 12:45

@datadog-prod-us1-4 datadog-prod-us1-4 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Datadog Autotest: PASS

More details

The Mocha adapter preserved configured setup files and effective options while replacing configured specs with worker-selected files across Mocha 8, 9, 10, and 11, including custom command wrappers. No additional tests recommended: the adversarial scenarios covered the riskiest compatibility and partitioning paths, and no production regression was found.

Was this helpful? React 👍 or 👎

📊 Validated against 10 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit 92bacd8 · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 92bacd82ef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

nodeOptions = os.Getenv(nodeOptionsEnvVar)
}
adapterEnv[nodeOptionsEnvVar] = strings.TrimSpace(nodeOptions + " --require " + strconv.Quote(adapterPath))
adapterEnv[mochaRequestEnvVar] = string(request)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep batch data out of a single environment variable

When a worker receives enough test files for the serialized request to exceed the platform's per-environment-string limit—about 128 KiB on Linux—starting the Mocha command fails with E2BIG before Mocha runs. A sequential or single-worker CI-node run can reach this with thousands of ordinary test paths, so pass the request through a temporary file or another mechanism that does not place the entire batch in one environment variable.

Useful? React with 👍 / 👎.

// The selected command will load Mocha's CLI after this preload. Replace
// its option loader so configured specs cannot be merged back in, while
// retaining all other effective Mocha options on every supported version.
options._ = files

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Rebase selected files after command wrappers change cwd

When a supported command wrapper changes directory before execing Mocha, discovery has normalized its results relative to DDTest's original working directory, but these relative paths are assigned directly after the wrapper has established a different process.cwd(). Mocha then resolves paths such as packages/foo/test/a.js from inside packages/foo, producing an invalid duplicated path and failing or running no tests; convert the selected files to absolute paths before handoff or rebase them to the actual Mocha working directory.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants