sveltejs / sveltejs/kit

Unit tests of remote functions

Open
#14,796 7 comments 20 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

I haven't been able to find a way to write unit tests of remote functions without involving hacks that use SvelteKit internals.

I can import the function in the unit test and try to call it, but I get an error Could not get the request store. This is an internal error. at const { event, state } = get_request_store(); inside some internal SvelteKit code. My code is already mocking getRequestEvent but this isn't so straightforward as its an internal file and may involve some extra internal state.

(See below for my current solution which works but is also not great.)

Describe the proposed solution

I'm not really sure of a proper solution here yet that doesn't make too many assumptions about the user's code. But essentially "some simple way to unit test remote functions".

Actually calling the remote functions in unit tests is useful because it ensures that you're actually testing all the code. While it works to test the functions that the remote functions use, that's fragile.

Alternatives considered

Right now I'm mocking the functions in the $app/server module. But to make this work, I had to update the mock implementations to add the __ member that Kit looks for.

(For those unfamiliar, __ holds SvelteKit-specific metadata for remote functions and if it's missing then SvelteKit complains about exporting something that is not a remote function from a remote.ts file)

vi.mock('$app/server', async (importOriginal) => {
  const query = (schemaOrHandler: unknown, arg2: unknown) => {
    let handler = (arg2 ?? schemaOrHandler) as { __?: { type: string } };

    handler.__ = {
      type: 'query',
    };

    // Could wrap this in a schema check as well for good measure
    return handler;
  };

  return {
    ...(await importOriginal()),
    getRequestEvent: vi.fn(() => mockEvent),
    query,
     // and command, form, query.batch, etc.
  };
});

This actually works, but is obviously not a good general solution because it's relies on SvelteKit internals that may change, and expecting someone new to coding and/or SvelteKit to figure this out is asking a bit much.

Importance

nice to have

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the remote-function path through $app/server, remote.ts, and the internal get_request_store() call that raises the request-store error. Compare that path with the shown Vitest mock, including the __ metadata and getRequestEvent mock, then identify a supported testing boundary. Done means a documented, maintainable way to invoke remote functions in unit tests without relying on SvelteKit internals.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
backend-api-design, testing-qa, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.