Skip to content

Instantly share code, notes, and snippets.

@tzmfreedom
Last active March 2, 2025 13:05
Show Gist options
  • Save tzmfreedom/05bc2bf320e278079b803ded39864889 to your computer and use it in GitHub Desktop.
Save tzmfreedom/05bc2bf320e278079b803ded39864889 to your computer and use it in GitHub Desktop.
Vitest Example: Alpine.js with JSDOM
import { expect, test } from 'vitest'
import { Alpine } from 'alpinejs';
import { fireEvent, screen } from "@testing-library/dom";
test('testing alpinejs', () => {
document.body.innerHTML = `<div x-data="{ open: true }">
<button data-testid="button" @click.prevent="open = !open;">Expand</button>
<span data-testid="content" x-show="open">
Content...
</span>
</div>`;
Alpine.initTree(document.body);
await fireEvent.click(screen.getByTestId('button'));
await new Promise((resolve) => setTimeout(resolve, 30));
expect(screen.getByTestId("content").style.display).toBe('none');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment