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

Add test utils to astro core #4200

Closed
wants to merge 1 commit into from
Closed

Add test utils to astro core #4200

wants to merge 1 commit into from

Conversation

natemoo-re
Copy link
Member

Changes

  • Still WIP, just opening a draft.
  • Adds astro/test with some helpful utilities for testing Astro components

Testing

TODO

Docs

TODO

@changeset-bot
Copy link

changeset-bot bot commented Aug 8, 2022

⚠️ No Changeset found

Latest commit: 9450da4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: example Related to an example package (scope) labels Aug 8, 2022
@@ -0,0 +1,2 @@
/// <reference path="./astro-jsx.d.ts" />
export function render(vnode: astroHTML.JSX.Element): Promise<{ container: HTMLTemplateElement }>;
Copy link
Contributor

@matthewp matthewp Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool if this function also worked inside of .astro components, can we make this accept a non-JSX astro type? I'm thinking I'd love to be able to do:

src/components/Greeting.test.astro

---
import { expect, test } from 'vitest'
import { render } from 'astro/test'
import Greeting from './Greeting.astro'

test('Greeting', async () => {
	const { container } = await render(Greeting, { props: 'here' })
	expect(container).toBeDefined()
	expect(container.textContent).toBe('Hello world')
})
---

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize that the above probably doesn't work for some reasons, just noting that it would be nice if this generically named render function can be extended in the future for non-JSX test files.

@natemoo-re natemoo-re added this to the v1.1.x milestone Aug 8, 2022
@matthewp matthewp added the semver: minor Change triggers a `minor` release label Aug 23, 2022
import { render } from 'astro/test'
import Greeting from './Greeting.astro'

test('Greeting', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:partyblob:

@matthewp
Copy link
Contributor

This is stale so closing, please reopen if you're about to update it.

@matthewp matthewp closed this Sep 22, 2022
@omidantilong
Copy link

Hey @natemoo-re 👋 I've been digging into possible unit testing strategies for Astro... Just wondering what happened with this PR, it looked promising!

@natemoo-re
Copy link
Member Author

natemoo-re commented Dec 24, 2023

Hey @natemoo-re 👋 I've been digging into possible unit testing strategies for Astro... Just wondering what happened with this PR, it looked promising!

Hey, thanks for asking! This is something we're interested in circling back to. We were originally blocked by the lack of a user-facing API for rendering Astro components to a string. Internally, it required the full Request and a decent amount of other metadata. We've accepted a proposal to implement that feature, but haven't had the bandwidth for it until recently. Once that is implemented, what this PR explored should land in one form or another.

The proposal actually came out of this exploration specifically. Hoping to implement in early 2024!

withastro/roadmap#533

@omidantilong
Copy link

Thanks for the quick reply man! Just to be clear, did the original proposal in this PR actually work? I assume the issue around the Request and accompanying metadata etc is what the createResult call is for. I'm just curious if you managed to get it working (even halfway) because it looks like it should, but when I tried to shoehorn it into my little sandbox project I just got Vite complaining (via Vitest) about not being able to parse the source because it's invalid syntax.

Really hopeful to see progress on 533 and would love to contribute if I can, hence all this poking around (at 11.30pm on Christmas Eve 🎄 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: example Related to an example package (scope) semver: minor Change triggers a `minor` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants