Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use trpc directly in playwright testing? #123

Open
WhyAsh5114 opened this issue Jun 18, 2024 · 2 comments
Open

How to use trpc directly in playwright testing? #123

WhyAsh5114 opened this issue Jun 18, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@WhyAsh5114
Copy link

Describe the bug
Unable to use trpc client in playwright tests

To Reproduce
Steps to reproduce the behavior:

  1. Setup a sveltekit project with playwright enabled
  2. Create a basic tRPC config with an endpoint
  3. Try to directly call it from playwright

Expected behavior
The client should work as it does in Svelte files in the frontend

Additional context
I tried various methods to create the client in playwright:

  1. I tried using it without args and it gave me an
    Calling createTRPCClient() on the server requires passing a valid LoadEvent argument
  2. Maybe I'm supposed to use createCaller() with createContext() like we do in server load functions, but what do we pass to the createContext()?
  3. Creating the client with { url: { origin: page.url() } } also doesn't seem to work and give this error:
    TRPCClientError: Unexpected token '<', "<!doctype "... is not valid JSON
@WhyAsh5114 WhyAsh5114 added the bug Something isn't working label Jun 18, 2024
@mass8326
Copy link

Playwright runs in a node environment, so you are unable to create a browser trpc client with trpc-sveltekit. It is missing the necessary context to know where to send the requests.

if (typeof window === 'undefined' && !init) {
throw new Error(
'Calling createTRPCClient() on the server requires passing a valid LoadEvent argument'
);
}
return internalCreateTRPCClient<Router>({
transformer,
links: [
httpBatchLink({
url:
typeof window === 'undefined' ? `${init.url.origin}${url}` : `${location.origin}${url}`,
fetch: typeof window === 'undefined' ? init.fetch : init?.fetch ?? window.fetch,
headers
})
]
});

If you want to test your endpoint, you will need to manually create a client with @trpc/client and pass in where your web server is running.

import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';

const client = createTRPCProxyClient({
  links: [httpBatchLink({ url: "..." })],
});

@WhyAsh5114
Copy link
Author

I tried this too, the method returns a weird string union
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants