Skip to content

test: Reset global state in tests to avoid cross-test pollution #57

@electather

Description

@electather

Problem / motivation

The test suite in tests/ mutates package-level global state (for example viper config values and apiutil.OverrideServerURL) without consistently resetting it afterwards.

Examples:

  • viper.Set("seerr.server", ...)
  • viper.Set("seerr.api_key", ...)
  • apiutil.OverrideServerURL = server.URL + "/api/v1"

Because this state is global, one test can affect the next test depending on execution order. This becomes especially risky if tests are later marked parallel or if additional tests rely on clean defaults.

Proposed solution

Add cleanup/reset logic to every test that mutates global state.

Suggested pattern:

t.Cleanup(func() {
    apiutil.OverrideServerURL = 
    viper.Reset()
})

Where full viper.Reset() is too broad, use explicit restoration of the keys touched by the test.

A good follow-up improvement would be to remove the need for package-level test overrides entirely by injecting configuration/client dependencies into commands.

Alternatives considered

  • Leave the tests as-is and rely on current execution order.
  • Avoid parallel tests forever.

Both options make the suite more fragile over time.

Additional context

This is a reliability improvement rather than a functional bug, but it will make the test suite safer to extend and easier to debug when intermittent failures appear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions