Open nicholas-wilcox opened 1 year ago
I advocated for exposing these from the start, for exactly this reason, but at the time @Rich-Harris was against it. I can't remember why, and I can't find the conversations around it, so maybe it's time to open the discussion back up?
Describe the problem
I would like to write unit tests for my server hooks. Specifically, I want to add test cases that make assertions about SvelteKit's "control" objects that get thrown by
redirect
,error
, andfail
.What I can do now, using Vitest as it comes preconfigured by
create-svelte
, is something like the following:However, any usage of the optional argument of
toThrowError()
will result in an error, because the objects that are thrown aren't actually errors.What I would like to to is write custom tests using
expect.extend
that use theinstanceof
operator to check that the thrown object is the correct type.The weird thing about this is that TypeScript lets me write an import statement that looks like it imports the
Redirect
class, not the type. Unfortunately, the symbol evaluates toundefined
during runtime, which makes code likeactual instanceof Redirect
orRedirect.prototype
throw runtime errors.In other words, this is valid code for a fresh SvelteKit application made with
create-svelte
:However, trying to print
Redirect.prototype
throws aTypeError
.Describe the proposed solution
Export
Redirect
,HttpError
, andActionFailure
in https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/index.js.Adding an export statement manually in
node_modules/@sveltejs/kit/src/exports/index.js
prevents the errors described above.Alternatives considered
I only want access to these objects for testing purpose, so I can also use Vitest's mocking features to create a mock for
@sveltejs/kit
. However, this solution requires me to redeclare the classes in my own module:Now, the thrown objects will have accessible prototypes, but they will be from the classes declared in my own module.
Importance
nice to have
Additional Information
I'm not sure if the quirk that prevents a type-check error qualifies as a bug.
Also, because we aren't supposed to catch these objects in our application code, I can't think of a reason that anyone would want to access
Redirect.prototype
outside of testing.