Skip to content

encryptionKey.test.ts: vi.mock calls inside it() will break in a future vitest version #118

@bmersereau

Description

@bmersereau

Problem

In backend/src/lib/__tests__/encryptionKey.test.ts (PR #107 / fix/98), vi.mock() calls appear inside individual it() callbacks rather than at the top level of the module:

it("throws when no encryption secret is configured", async () => {
    vi.mock("../supabase.js", () => ({ ... }));  // ← nested inside it()
    ...
});

Vitest currently hoists all vi.mock() calls to before the test suite runs regardless of where they appear (this is the Babel/vite-plugin-vitest transform behaviour). The existing vitest run emits a warning:

Warning: A vi.mock("../supabase.js") call in "...encryptionKey.test.ts" is not at the top level of the module.
Although it appears nested, it will be hoisted and executed before any tests run.
Move it to the top level to reflect its actual execution order.
This will become an error in a future version.

Because all three vi.mock calls mock the same module identity with the same factory, the tests currently pass. But the warning explicitly states this will become an error in a future vitest release, and the nesting gives a false impression that the mock is scoped to the individual test.

Fix

Move the vi.mock("../supabase.js", ...)) call to the top level of the file (after imports, before the describe block), and use vi.mocked(...).mockImplementation() or mockReturnValue() inside each it() if different behaviour per test is needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions