#vitest for astro actions
22 messages · Page 1 of 1 (latest)
anyone around with the same issue ??
Houston, we have a problem... and <@&1129102257422610512>, you’re our mission control! 🚀
Absoutely no one at all in the whole wide world with this issue ? <@&1129102257422610512> ?
No, sorry mate. Hopefully, someone will chime in after the weekend.
Do you have a repo of what you’re trying, I’ll try to take a look soon
A simple Vitest for any astro action at all; and vitest starts failing at resolving the virtual module :
FAIL tests/actions.test.ts [ tests/actions.test.ts ]
Error: Failed to load url astro:actions (resolved id: astro:actions) in /src/actions/index.ts. Does the file exist?
Can you paste the code?
import { server } from "../src/actions/index";
import { test, expect } from "vitest";
test("Issue Certification :: Member Qualified : R3", async () => {
const form = new FormData();
form.append("email", "aviator@coolgoose.com");
form.append("certification", "R3");
const result =
await server.PersonCredentialActions.issueCertificationToMember(form);
console.log(result);
expect(result).toBe("aviator@coolgoose.com");
});
and here is the quite immediate error :
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL tests/actions.test.ts [ tests/actions.test.ts ]
Error: Failed to load url astro:actions (resolved id: astro:actions) in /src/actions/index.ts. Does the file exist?
❯ loadAndTransform ../node_modules/.pnpm/vite@6.2.1_@types+node@24.2.1_jiti@2.4.2_lightningcss@1.29.2_tsx@4.19.3_yaml@2.8.0/node_modules/vite/dist/node/chunks/dep-glQox-ep.js:41310:17
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
Test Files 1 failed (1)
Tests no tests
Start at 12:03:15
Duration 0ms
FAIL Tests failed. Watching for file changes...
press h to show help, press q to quit
ELIFECYCLE Test failed. See above for more details.
Please note that we are all volunteers.
Ok yeah, for unit tests for actions I think youll need to test the handler function or the input if you want
or you can do an integration test and interact with the actions url, by starting dev server
maybe @fierce hearth or @muted marsh have another idea though
You need to build your project prior to running the tests or hook Astro's entire module resolution into vitest. The later is possible but way way too convoluted and deals with a lot of internal that may break at any moment.
Building the project for the tests can be done programmatically using the advanced APIs, even with different build options, tho that is more useful for libraries and not for a project.
I also have a test helper library for that as part of inox tools: https://inox-tools.fryuni.dev/astro-tests
to answer your question more directly though you need to use import { getViteConfig } from 'astro/config'; to be able to resolve astro virtual modules in tests but afaik you still cant run full actions since it actions detect the actual Astro.callAction()
Thanks a ton folks ! I suspected i needed a build ; but good to see you folks confirm it.
Will check it out ! for sure ! Thanks a ton for sharing.
Oh yeah, I forgot we added that. So there is a stable way to hook into Astro's module resolution without relying on internals. I should write some helper run vitest without a pre-build then.
To the pile it goes!
Yeah... That util is my friend for my repos 😅
Did you get something working @swift lake
working on it as we speak 🙂 ! need to refactor some of my Actions to accomodate both the unit and integration tests ! Will have an update for you in the next few hours ! but nonetheless thanks a ton already; conceptually your github repo which i cloned is already a win for me.
Base URL http://0.0.0.0:9999/
❯ tests/actions.test.ts (1 test | 1 failed) 12684ms
× actions - integration test > hello action via HTTP endpoint 244ms
→ expected '<!DOCTYPE html><html lang="en" class=…' to contain 'Hello, Integration!'
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL tests/actions.test.ts > actions - integration test > hello action via HTTP endpoint
AssertionError: expected '<!DOCTYPE html><html lang="en" class=…' to contain 'Hello, Integration!'
- Expected
- Received
- Hello, Integration!
- <!DOCTYPE html><html lang="en" class="h-full bg-zinc-950 dark overflow-hidden" data-astro-cid-sckkx
Yup it is working Beautifully ! excellent stuff; I know it shows the test failed but that's by design it needs a Authentication Step priorly to run the actions; the html received is the redirect to login due to a lack of token! which in itself is a test !
Thank You & the Wonderful folks from this community !!
FYI. this is the integration tests and of course i had to set up some additional config keys - "development" db, etc..
The unit test still does not work for me because there are some libsql modules whcih aren't getting resolved; unfortunately I don't have the time to go down that path; to get those configs successfully resolved.
I will make do with integration tests for now !