Remove unused parameters from Jest spec factories

Summary

Many Jest spec factories declare a parameter that no call in the file supplies. The parameter always holds its default. The goal of this issue is cleaner spec code. The scan found about 1,400 unused parameters.

Background

What the problem is

A spec factory declares a parameter and gives it a default. No call in the file passes a value for it. The parameter always holds its default.

Common parameter names are props, propsData, provide, and mountFn.

Why this matters:

  • The parameter advertises a control that no test uses.
  • A reader cannot tell which parameters work. The reader must check every call in the file.
  • A person who copies the pattern into a new test gets setup that does nothing.
  • Removing the parameter makes the factory state exactly what the file needs.

The goal is cleaner spec code. That is the point of this issue.

How to find the cases

  • Read each factory in a spec file and list its parameters.
  • Read each call to the factory in the same file.
  • Mark every parameter that no call supplies.
  • Run the scan again on current master before you start. The count above is old.

How to fix

  1. Confirm that no call in the file supplies the parameter.
  2. Check the default value of the parameter.
  3. If the default is empty, such as {}, delete the parameter and the block that it fed.
  4. If the default holds a real value, do not delete it. Write the value in place. Then delete the parameter.

A real example of the caution

File: ee/spec/frontend/security_orchestration/components/policy_editor/dependency_firewall/editor_component_spec.js

A factory declares glFeatures = { dependencyFirewallPhase2: true }. All 12 calls pass no arguments. The parameter is unused.

But dependency_firewall_phase2 is a live feature flag:

  • config/feature_flags/wip/dependency_firewall_phase2.yml defines it.
  • Both security policies controllers push it.
  • editor_component.vue reads it.

Deleting the parameter and its value would change what the test covers. The correct fix writes provide: { glFeatures: { dependencyFirewallPhase2: true } } in place. Then it deletes the parameter.

Why deletion is safe when the default is empty

  • No call supplies a value. The parameter always holds its default. Removing it cannot change what the test does.
  • For glFeatures, glFeatureFlagsMixin injects glFeatures with default: () => ({}). 238 components use the mixin. Removing a glFeatures entry from the provide of a spec changes nothing for them.
  • One component injects glFeatures without a default: app/assets/javascripts/vue_shared/components/source_viewer/components/chunk.vue. Before you remove glFeatures setup, check that the spec does not mount this component.

Scope and sizing

  • There are about 1,400 parameters. Do not try to clear them in one pass.
  • Prefer opportunistic cleanup. Remove the unused parameters when you edit a spec file for another reason.
  • A batched merge request also works. Keep each one to a single area. Keep it small enough to read.
  • The four merged merge requests in the predecessor issue are the precedent. They covered 6 to 12 files each.
  • Run Prettier after the edit. A removed parameter often lets a multi-line function signature collapse to one line.

🤖 AI-generated

Edited by 🤖 GitLab Bot 🤖