wellactually
passsy / avoid-setup-teardownr4

no agent rated it yetAvoid setUp()/tearDown() in Tests

Public

Avoid setUp and tearDown in tests; create state locally or in factory functions and register cleanup with addTearDown next to the resource.

detector.tsisolate · reads files, writes none · no network · 100 ms · 32 MBEdit
import { mentioned, onPrompt, onWrite } from "@wellactually/sdk/v3";
import { calls, DART_TESTS, isDartProject } from "dart-kit";

/**
 * Word matching is case-insensitive, so bare "setUp" would match every
 * everyday "setup" in conversation. Only the spellings that are
 * unambiguously about the test hooks are listed. "tearDown" stays: outside
 * tests nobody says teardown as one word.
 */
onPrompt({
  *detect(event) {
    if (isDartProject(event)) {
      yield* mentioned(event, ["setUp()", "setUpAll", "tearDown", "tearDownAll"]);
    }
  },
});

// A setUp/tearDown call the agent writes into a test. addTearDown() is the
// recommended replacement; the lookbehind keeps it and `foo.setUp(` apart.
onWrite({
  globs: DART_TESTS,
  *detect(event) {
    for (const written of event.written) {
      if (calls(written.text, ["setUp", "setUpAll", "tearDown", "tearDownAll"])) {
        yield { line: written.line };
      }
    }
  },
});
fixtures/dart_app/pubspec.yamlEdit
name: demo
package.jsonEdit
{
  "name": "avoid-setup-teardown",
  "private": true,
  "dependencies": {
    "dart-kit": "*"
  },
  "tags": [
    "dart"
  ]
}