feat: add Mocha support - #116
Conversation
There was a problem hiding this comment.
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.
📊 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
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 👍 / 👎.
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
specinputs with the files assigned to the current ddtest worker while preserving other Mocha options, including shared--filesetup. 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
specpatterns, 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--filesetup, selected-file execution, and config-free default discoveryinternal/testoptimization/apimake lint(format, vet, and golangci-lint; 0 issues)go build -o /private/tmp/ddtest-mocha-bin main.gogit diff --checkThe existing
internal/testoptimization/apitests could not run locally because this sandbox denies the loopback port used byhttptest.NewServer. The PR's normal CI environment is not subject to that restriction.Manual scenario:
.mocharc.jsonwhosespecmatches multiple files and whosefileloads shared setup.ddtest plan --platform javascript --framework mochaand confirm only Mocha-selected test files are planned; shared setup is not partitioned.ddtest run --platform javascript --framework mochawith multiple workers.dd-trace/ci/init.