#vitest for astro actions

22 messages · Page 1 of 1 (latest)

swift lake
#

Is it possible to resolve the path to virtual modules with vitest ? I am trying to write unit test for actions and get stuck on vitest unable to resolve paths to ActionError and ActionInputError from the "astro:actions" virtual module.

swift lake
#

anyone around with the same issue ??

lime harnessBOT
#

Houston, we have a problem... and <@&1129102257422610512>, you’re our mission control! 🚀

swift lake
#

Absoutely no one at all in the whole wide world with this issue ? <@&1129102257422610512> ?

tranquil ocean
#

No, sorry mate. Hopefully, someone will chime in after the weekend.

open osprey
swift lake
swift lake
# open osprey 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.

tight tiger
open osprey
#

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

fierce hearth
#

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

open osprey
#

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()

open osprey
#

i could be way wrong here though haha

swift lake
swift lake
fierce hearth
little valve
open osprey
#

Did you get something working @swift lake

swift lake
# open osprey Did you get something working <@1043512657333985360>

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.

swift lake
# open osprey Did you get something working <@1043512657333985360>

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 !