Skip to content

test(frontend): route specs through HttpClientTestingModule and lint-enforce it#5185

Merged
Yicong-Huang merged 2 commits into
apache:mainfrom
Yicong-Huang:chore/lint-spec-http-client
May 24, 2026
Merged

test(frontend): route specs through HttpClientTestingModule and lint-enforce it#5185
Yicong-Huang merged 2 commits into
apache:mainfrom
Yicong-Huang:chore/lint-spec-http-client

Conversation

@Yicong-Huang
Copy link
Copy Markdown
Contributor

@Yicong-Huang Yicong-Huang commented May 24, 2026

What changes were proposed in this PR?

Two passes against the same anti-pattern — specs that wire the real Angular HttpClient instead of routing through HttpClientTestingModule:

  1. Data cleanup. Migrate every spec that was importing HttpClientModule, listing HttpClient as a bare provider, or hand-rolling an HttpClient stub over to HttpClientTestingModule.
  2. Lint guardrail. Add an *.spec.ts override to frontend/.eslintrc.json so the pattern can't grow back. no-restricted-imports blocks HttpClientModule; no-restricted-syntax flags HttpClient as a bare provider entry. Type usage (let h: HttpClient) and TestBed.inject(HttpClient) remain allowed.

The lint rule is what surfaced the last five offenders — the original grep audit missed them because they imported HttpClientTestingModule alongside the offending provider or import.

Any related issues, documentation, discussions?

Closes #5182, closes #5184.

How was this PR tested?

yarn lint exits 0; yarn format:ci clean. yarn ng test --watch=false --include=… on every touched spec passes locally.

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.7

`operator-reuse-cache-status.service.spec.ts`, `operator-menu.service.spec.ts`,
`execute-workflow.service.spec.ts`, and `user-computing-unit.component.spec.ts`
each wired the real Angular `HttpClient` rather than the testing variant —
either by importing `HttpClientModule`, providing `HttpClient` directly, or
hand-rolling a `StubHttpClient` that returns `of("a")` for every `post`. That
left the specs one missing mock away from a real network call in CI and drifted
from the convention in `frontend/TESTING.md`.

Swap each over to `HttpClientTestingModule`:

- The two `HttpClientModule` cases become a one-line import swap.
- `execute-workflow.service.spec.ts` drops the in-file `StubHttpClient`
  entirely and the now-unused `Observable` import; the testing module supplies
  `HttpClient` directly.
- `user-computing-unit.component.spec.ts` moves `HttpClient` out of `providers`
  and pulls `HttpClientTestingModule` into `imports`.

All four specs (20 tests) continue to pass.

Closes apache#5182
Add an `*.spec.ts` override to `frontend/.eslintrc.json` so the
anti-pattern that apache#5183 just swept the repo of can't grow back:

- `no-restricted-imports` blocks `HttpClientModule` from
  `@angular/common/http` in specs. HttpClientTestingModule is the only
  legitimate way to wire HttpClient into a unit test.
- `no-restricted-syntax` flags `HttpClient` listed as a bare provider
  inside a `providers: [...]` array. HttpClientTestingModule (in
  `imports`) already supplies HttpClient; layering the real one on top
  of that overrides the testing backend.

The rule fires on five more specs that apache#5183 missed — each one had
either redundant `HttpClientModule` imports or a stale `HttpClient`
provider sitting next to a working `HttpClientTestingModule`. Clean
those up so `yarn lint` stays green:

- `user-avatar.component.spec.ts` — drop the redundant `HttpClientModule` from imports.
- `user-workflow.component.spec.ts`, `coeditor-user-icon.component.spec.ts`, `coeditor-presence.service.spec.ts` — drop the bare `HttpClient` provider and the now-unused import.
- `operator-metadata.service.spec.ts` — drop the bare `HttpClient` provider. The import stays because the spec injects HttpClient via `TestBed.inject(HttpClient)` to talk to HttpTestingController.

All nine touched specs (37 tests) continue to pass.

Closes apache#5184
@github-actions github-actions Bot added the frontend Changes related to the frontend GUI label May 24, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 43.71%. Comparing base (b228d51) to head (b9d0e2b).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #5185   +/-   ##
=========================================
  Coverage     43.71%   43.71%           
  Complexity     2218     2218           
=========================================
  Files          1049     1049           
  Lines         40578    40578           
  Branches       4327     4327           
=========================================
  Hits          17739    17739           
  Misses        21733    21733           
  Partials       1106     1106           
Flag Coverage Δ *Carryforward flag
access-control-service 39.53% <ø> (ø) Carriedforward from 68c9f2c
agent-service 33.76% <ø> (ø) Carriedforward from 68c9f2c
amber 44.17% <ø> (ø) Carriedforward from 68c9f2c
computing-unit-managing-service 0.00% <ø> (ø) Carriedforward from 68c9f2c
config-service 0.00% <ø> (ø) Carriedforward from 68c9f2c
file-service 32.18% <ø> (ø) Carriedforward from 68c9f2c
frontend 35.18% <ø> (ø)
python 90.50% <ø> (ø) Carriedforward from 68c9f2c
workflow-compiling-service 56.81% <ø> (ø) Carriedforward from 68c9f2c

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Yicong-Huang Yicong-Huang changed the title chore(frontend): forbid the real HttpClient in specs via ESLint test(frontend): route specs through HttpClientTestingModule and lint-enforce it May 24, 2026
@Yicong-Huang Yicong-Huang added this pull request to the merge queue May 24, 2026
Merged via the queue into apache:main with commit cfc6d9a May 24, 2026
41 checks passed
@Yicong-Huang Yicong-Huang deleted the chore/lint-spec-http-client branch May 24, 2026 22:21
github-merge-queue Bot pushed a commit that referenced this pull request May 24, 2026
… specs (#5189)

### What changes were proposed in this PR?

Three coordinated passes against the "specs that replace the parent
template at test time" anti-pattern:

1. **`frontend/TESTING.md`**: anti-patterns 2 and 3 are rewritten to
describe what actually goes wrong (the real `.component.html` never
executes, so v8 coverage stays at 0%), and a new Recipe F + anti-pattern
8 cover the right way to handle a heavy child component when one truly
cannot be instantiated — additive `overrideComponent({ remove, add })`
with a minimal stub child.
2. **Spec cleanups**: every spec that carried `template: ""`, a
stub-markup template substitute, `NO_ERRORS_SCHEMA`, or the broader
`set: { imports: [], schemas: [...] }` override is migrated to render
the real template. Two specs (Formly-driven and the full
WorkspaceComponent) keep the old shape behind a scoped `eslint-disable`
with a TODO; both need a more thorough redesign that isn't in scope
here.
3. **ESLint guardrail**: an `*.spec.ts` override in `.eslintrc.json`
blocks the patterns going forward — `NO_ERRORS_SCHEMA` imports from
`@angular/core`, any `template` or `imports` key inside
`overrideComponent({ set: ... })`. Each rule's message points at the
right replacement.

### Any related issues, documentation, discussions?

Closes #5188. Builds on the testing guide introduced in #5170 and the
lint-guardrail pattern from #5185.

### How was this PR tested?

`yarn ng test --watch=false --include=…` over the touched specs runs 13
files / 108 tests, all green. `yarn lint` and `yarn format:ci` both
clean. The three template files that originally pinned at 0% —
`preset-wrapper.component.html`, `operator-menu.component.html`,
`menu.component.html` — now report 44 / 79 / 59% line coverage under the
new shape.

### Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Changes related to the frontend GUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an ESLint rule that forbids the real HttpClient in frontend specs Four frontend specs bypass HttpClientTestingModule

3 participants