Testing
MeoNode UI components are React components, so any React test setup renders them. This page uses Vitest with React Testing Library, under either jsdom or happy-dom; both work, and every example below runs under each.
Two things are specific to MeoNode, and both catch out style assertions written the way you would for plain Emotion:
ThemeProvider renders a <style> element ahead of your children, and a theme.* token becomes a CSS variable, not
the value you wrote in your tokens. Asserting styles covers both.
Setup
Install Vitest, Testing Library and one DOM environment:
npm install --save-dev vitest @testing-library/react @testing-library/dom jsdom # or, for happy-dom instead of jsdom: npm install --save-dev vitest @testing-library/react @testing-library/dom happy-dom
In a Vite app, extend your Vite configuration so tests resolve the same aliases and plugins as the app:
import { defineConfig, mergeConfig } from 'vitest/config' import viteConfig from './vite.config.ts' export default mergeConfig( viteConfig, defineConfig({ test: { environment: 'jsdom', // or 'happy-dom' setupFiles: ['./src/test/setup.ts'], }, }), )
A Next.js app has no Vite configuration to extend. Add the React plugin, and let Vite read the @/* paths from
tsconfig.json. Name the file .mts: a Next.js package.json does not set "type": "module", so a .ts config
would be loaded as CommonJS.
npm install --save-dev vitest @vitejs/plugin-react @testing-library/react @testing-library/dom jsdom
import { defineConfig } from 'vitest/config' import react from '@vitejs/plugin-react' export default defineConfig({ plugins: [react()], resolve: { tsconfigPaths: true }, test: { environment: 'jsdom', // or 'happy-dom' setupFiles: ['./src/test/setup.ts'], }, })
Testing Library unmounts after each test on its own only when Vitest's globals are enabled. Without them, register the
cleanup yourself — and reset what the theme provider persists, so one test's setMode does not leak into the next:
import { afterEach } from 'vitest' import { cleanup } from '@testing-library/react' afterEach(() => { cleanup() localStorage.clear() document.documentElement.removeAttribute('data-theme') document.documentElement.removeAttribute('data-theme-preference') })
Rendering under ThemeProvider
Any component that reads a theme.* token or calls useTheme() needs the provider above it. A small helper keeps that
out of every test:
import { render } from '@testing-library/react' import { PortalHost, PortalProvider, ThemeProvider, type Children } from '@meonode/ui' export const testTokens = { base: { default: 'rgb(255, 255, 255)', content: 'rgb(26, 26, 26)' }, primary: { default: 'rgb(0, 85, 170)', content: 'rgb(255, 255, 255)' }, spacing: { sm: '4px', md: '8px', lg: '16px' }, } export function renderWithTheme(children: Children) { return render( ThemeProvider({ modes: ['light', 'dark'], defaultMode: 'light', tokens: testTokens, children: PortalProvider({ children: [children, PortalHost()] }), }).render(), ) }
The tokens here are literal values on purpose. In an app, tokens often point at CSS variables defined in a stylesheet
('var(--base-default)'), and Vitest does not load your .css imports into the test document — so those would
resolve to nothing. Literal test tokens give every style assertion a concrete value to check. Include PortalProvider
and PortalHost only if the components under test open portals.
Asserting styles
Target the element, not container.firstChild. ThemeProvider renders its token variables as a
<style data-meonode-theme-vars> element before your children, so the first child of the container is that style
element. Query for the element you mean — by role, text or test id.
Read resolved values with getComputedStyle. A theme.* token is written into the stylesheet as a CSS variable,
var(--meonode-theme-primary), whose value the provider's <style> defines. Both jsdom and happy-dom resolve the
variable when you ask for the computed style, so you can assert the colour itself:
import { expect, it } from 'vitest' import { act, screen } from '@testing-library/react' import { Button, Div, Node, useTheme } from '@meonode/ui' import { renderWithTheme } from './render' it('resolves theme tokens to their values', () => { const { container } = renderWithTheme( Div({ 'data-testid': 'box', color: 'theme.primary', padding: 'theme.spacing.md', children: 'Hello' }), ) // The provider's token <style> comes first; the Div is not container.firstChild. expect(container.firstElementChild?.tagName).toBe('STYLE') const box = screen.getByTestId('box') expect(getComputedStyle(box).color).toBe('rgb(0, 85, 170)') expect(getComputedStyle(box).padding).toBe('8px') }) function ModeSwitch() { const { mode, setMode } = useTheme() return Div({ 'data-testid': 'panel', css: { color: theme => (theme.mode === 'dark' ? 'rgb(240, 240, 245)' : 'rgb(26, 26, 26)') }, children: Button(`Switch from ${mode}`, { onClick: () => setMode(mode === 'dark' ? 'light' : 'dark') }), }).render() } it('re-resolves a theme function when the mode changes', async () => { renderWithTheme(Node(ModeSwitch)) const panel = screen.getByTestId('panel') expect(getComputedStyle(panel).color).toBe('rgb(26, 26, 26)') await act(async () => screen.getByRole('button').click()) expect(document.documentElement.getAttribute('data-theme')).toBe('dark') expect(getComputedStyle(panel).color).toBe('rgb(240, 240, 245)') })
Wrap the click in act so React commits the mode change before you read the style back.
Optional: @emotion/jest
@emotion/jest works under Vitest. Its toHaveStyleRule matcher reads the
rule as written, not as computed — so a token asserts as the variable reference, and a pseudo-class or media query is
checked through its target and media options. Its serializer replaces generated class names in snapshots with
stable ones:
npm install --save-dev @emotion/jest
import { expect, it } from 'vitest' import { screen } from '@testing-library/react' import { createSerializer, matchers } from '@emotion/jest' import { Div } from '@meonode/ui' import { renderWithTheme } from './render' expect.extend({ ...matchers }) expect.addSnapshotSerializer(createSerializer()) // The package types its matchers for Jest; this declares them on Vitest's `expect`. declare module 'vitest' { interface Matchers<R extends void | Promise<void> = void | Promise<void>, T = unknown> { toHaveStyleRule(property: string, value: unknown, options?: { target?: string | RegExp; media?: string }): R } } it('checks the rule as written', () => { renderWithTheme( Div({ 'data-testid': 'box', color: 'theme.primary', css: { '&:hover': { color: 'red' } }, children: 'Hello' }), ) const box = screen.getByTestId('box') expect(box).toHaveStyleRule('color', 'var(--meonode-theme-primary)') expect(box).toHaveStyleRule('color', 'red', { target: ':hover' }) expect(box).toMatchSnapshot() })
The same rule applies here as above: pass the matcher the element you mean. Given container.firstChild, it receives
the provider's <style> element and reports Property not found.
Async server components
React Testing Library renders on the client, so it cannot render an async server component. Rendered under Vitest,
async function Page() is treated as a client component: React logs that <Page> is an async Client Component,
which only Server Components may be, and the content never appears.
Test what such a component renders through its client parts — the components it passes data to render under Vitest
as shown above — and test the server component itself end to end: build the app with next build, start it with
next start, and drive it in a browser with a tool such as Playwright.
On this page
- Testing