S
SolidJS2d ago
Luka

Testing server functions

Hallo! For my application I’d like to write automated API tests, but I noticed that there’s currently no way to access the endpoint URL of a server function declared using query() with "use server". Would it be possible to expose or simulate something like a .url or .fetch() helper so that we can test these functions from tools like Playwright or other API clients? Here’s an example of what I mean:
export const getCustomerOrganizations = query(async () => {
"use server";
const user = await authGuard();

const customerOrganizations = await db.customerOrganization.findMany({
where: {
isActive: true,
id: {
in: checkPermission({ user, resource: "customerOrganizations", action: "viewMemberOrganizations" }) ? user.customerOrganizations.map(co => co.id) : Prisma.skip
}
},
select: {
id: true,
name: true,

},
});
return customerOrganizations;
}, "getCustomerOrganizations");
export const getCustomerOrganizations = query(async () => {
"use server";
const user = await authGuard();

const customerOrganizations = await db.customerOrganization.findMany({
where: {
isActive: true,
id: {
in: checkPermission({ user, resource: "customerOrganizations", action: "viewMemberOrganizations" }) ? user.customerOrganizations.map(co => co.id) : Prisma.skip
}
},
select: {
id: true,
name: true,

},
});
return customerOrganizations;
}, "getCustomerOrganizations");
Then in the Playwright test, something like this would be very helpful:
test.describe('Customer Organization access', () => {
test('should not show customer organizations that the user is not a member of', async ({ customerPage }) => {
const url: string = getCustomerOrganizations.url
const response = await customerPage.page.request.get(url);
const customerOrgs = await response.json();
assert reponse...
});
})
test.describe('Customer Organization access', () => {
test('should not show customer organizations that the user is not a member of', async ({ customerPage }) => {
const url: string = getCustomerOrganizations.url
const response = await customerPage.page.request.get(url);
const customerOrgs = await response.json();
assert reponse...
});
})
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?