test: prefer assert.match for string matching#8540
Conversation
Add a new ESLint rule `eslint-prefer-assert-match` that runs on test files and flags `assert.ok(...)` calls whose first argument is a plain string-matching expression: `startsWith`, `endsWith`, `String.prototype.match`, or `RegExp.prototype.test` (optionally prefixed with `!`). `assert.match()` / `assert.doesNotMatch()` produces much more informative failure messages than `assert.ok(false)`. The rule auto-fixes safe cases (regex literals, and string literals whose metacharacters can be escaped into a regex literal) and reports the rest without a fix. `.includes()` is intentionally not handled because it is ambiguous between String and Array. Apply the auto-fixes across the test suite. Two call sites in `packages/datadog-webpack/test/plugin.spec.js` and `integration-tests/code-origin.spec.js` are intentionally kept on `assert.ok` (with an inline `eslint-disable` comment) because their search string is a runtime value (`source`, `cwd`) and safe escaping requires `RegExp.escape`, which is only available on Node.js 24+. They can be converted once the minimum supported Node.js version is 24.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Overall package sizeSelf size: 5.88 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.1 | 82.56 kB | 817.39 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: f3feb11 | Docs | Datadog PR Page | Give us feedback! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8415f93601
ℹ️ 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".
BenchmarksBenchmark execution time: 2026-05-19 12:30:51 Comparing candidate commit f3feb11 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 1501 metrics, 92 unstable metrics. |
Add a new ESLint rule `eslint-prefer-assert-match` that runs on test files and flags `assert.ok(...)` calls whose first argument is a plain string-matching expression: `startsWith`, `endsWith`, `String.prototype.match`, or `RegExp.prototype.test` (optionally prefixed with `!`). `assert.match()` / `assert.doesNotMatch()` produces much more informative failure messages than `assert.ok(false)`. The rule auto-fixes safe cases (regex literals, and string literals whose metacharacters can be escaped into a regex literal) and reports the rest without a fix. `.includes()` is intentionally not handled because it is ambiguous between String and Array. Apply the auto-fixes across the test suite. Two call sites in `packages/datadog-webpack/test/plugin.spec.js` and `integration-tests/code-origin.spec.js` are intentionally kept on `assert.ok` (with an inline `eslint-disable` comment) because their search string is a runtime value (`source`, `cwd`) and safe escaping requires `RegExp.escape`, which is only available on Node.js 24+. They can be converted once the minimum supported Node.js version is 24.
Add a new ESLint rule `eslint-prefer-assert-match` that runs on test files and flags `assert.ok(...)` calls whose first argument is a plain string-matching expression: `startsWith`, `endsWith`, `String.prototype.match`, or `RegExp.prototype.test` (optionally prefixed with `!`). `assert.match()` / `assert.doesNotMatch()` produces much more informative failure messages than `assert.ok(false)`. The rule auto-fixes safe cases (regex literals, and string literals whose metacharacters can be escaped into a regex literal) and reports the rest without a fix. `.includes()` is intentionally not handled because it is ambiguous between String and Array. Apply the auto-fixes across the test suite. Two call sites in `packages/datadog-webpack/test/plugin.spec.js` and `integration-tests/code-origin.spec.js` are intentionally kept on `assert.ok` (with an inline `eslint-disable` comment) because their search string is a runtime value (`source`, `cwd`) and safe escaping requires `RegExp.escape`, which is only available on Node.js 24+. They can be converted once the minimum supported Node.js version is 24.

What does this PR do?
Adds a new custom ESLint rule,
eslint-prefer-assert-match, that runs on test files and flagsassert.ok(...)calls whose first argument is a plain string-matching expression —String.prototype.startsWith,String.prototype.endsWith,String.prototype.match, orRegExp.prototype.test(with optional!negation). The rule auto-fixes the cases it can do safely (regex literals, and string literals whose regex metacharacters can be escaped into a regex literal) and reports the rest without a fix. The auto-fix has also been applied across the existing test suite.Motivation
assert.match(string, regexp)produces much more informative failure messages thanassert.ok(string.startsWith('foo'))— the latter fails with justExpected value to be truthy: false, whileassert.matchincludes both the actual string and the regex it was tested against. Until now we relied on convention; this rule mechanically enforces it on new test code and converts the existing call sites in one pass.Additional Notes
.includes()is deliberately not handled. Across this codebase it is used about equally on strings (comment.includes("ddsh='...'")) and arrays (tags.includes('foo'),_traceBuffer.includes(...)), and without runtime type information we'd produce a lot of false positives. Easy to extend later if we want it.assert.ok(with an inline// eslint-disable-next-line eslint-rules/eslint-prefer-assert-matchcomment that explains why):packages/datadog-webpack/test/plugin.spec.jsandintegration-tests/code-origin.spec.js. In both cases the search string is a runtime value (source,cwd) that can legitimately contain regex metacharacters, and safe escaping requiresRegExp.escape, which is only available on Node.js 24+. The comments point at the rewrite we'll be able to do once our minimum supported Node version is 24.