@describe-me/react 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Grzegorz Łotysz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @describe-me/react
2
+
3
+ The React adapter for [describe-me](https://github.com/grzehub/describe-me): a
4
+ drop-in `render` for `vitest-browser-react` that records a frame after mount and
5
+ after every `rerender`, and reads the component's name and props for the docs.
6
+ This is the only React-specific code in the project.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ pnpm add -D @describe-me/react
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ The package re-exports everything `vitest-browser-react` offers, with `render`
17
+ swapped for the recording one, so switching an existing test is one import.
18
+
19
+ ```tsx
20
+ import { render, step } from '@describe-me/react'
21
+
22
+ const screen = await render(<Counter initial={1} />)
23
+ await step('increment', () => screen.getByRole('button', { name: 'add' }).click())
24
+ ```
25
+
26
+ With `@describe-me/vitest`'s `describeMe()` plugin you do not even need that:
27
+ the plugin redirects `vitest-browser-react` imports to this adapter, so tests
28
+ record frames unchanged.
29
+
30
+ See the [root README](https://github.com/grzehub/describe-me#readme) for the
31
+ full picture.
@@ -0,0 +1,4 @@
1
+ import type { ReactElement } from 'react';
2
+ import { type ComponentInfo } from '@describe-me/core';
3
+ /** Pull the component name and JSON-safe props out of a React element. */
4
+ export declare function describeElement(ui: ReactElement): ComponentInfo;
@@ -0,0 +1,12 @@
1
+ import { serializeValue } from '@describe-me/core';
2
+ /** Pull the component name and JSON-safe props out of a React element. */
3
+ export function describeElement(ui) {
4
+ const type = ui.type;
5
+ const name = typeof type === 'string'
6
+ ? type
7
+ : (type?.displayName ??
8
+ type?.name ??
9
+ 'Anonymous');
10
+ const props = serializeValue(ui.props ?? {});
11
+ return { name, props };
12
+ }
@@ -0,0 +1,3 @@
1
+ export * from 'vitest-browser-react';
2
+ export { step } from '@describe-me/core';
3
+ export { render } from './render.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ // Everything vitest-browser-react offers, with `render` swapped for the recording one.
2
+ export * from 'vitest-browser-react';
3
+ export { step } from '@describe-me/core';
4
+ export { render } from './render.js';
@@ -0,0 +1,3 @@
1
+ import type { ComponentInfo } from '@describe-me/core';
2
+ /** A JSX-looking one-liner for a frame label, e.g. `<Counter initial=1 />`. */
3
+ export declare function labelFor(info: ComponentInfo): string;
@@ -0,0 +1,9 @@
1
+ /** A JSX-looking one-liner for a frame label, e.g. `<Counter initial=1 />`. */
2
+ export function labelFor(info) {
3
+ const shown = Object.entries(info.props)
4
+ .filter(([key, value]) => key !== 'children' &&
5
+ (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'))
6
+ .slice(0, 3)
7
+ .map(([key, value]) => (value === true ? key : `${key}=${JSON.stringify(value)}`));
8
+ return `<${info.name}${shown.length ? ' ' + shown.join(' ') : ''} />`;
9
+ }
@@ -0,0 +1,11 @@
1
+ import { render as baseRender } from 'vitest-browser-react';
2
+ import { type ReactElement } from 'react';
3
+ type BaseRender = typeof baseRender;
4
+ type RenderOptions = Parameters<BaseRender>[1];
5
+ type Screen = Awaited<ReturnType<BaseRender>>;
6
+ /**
7
+ * Drop-in for `render` from vitest-browser-react. Records a frame after mount
8
+ * and after every `rerender`.
9
+ */
10
+ export declare function render(ui: ReactElement, options?: RenderOptions): Promise<Screen>;
11
+ export {};
package/dist/render.js ADDED
@@ -0,0 +1,27 @@
1
+ import { render as baseRender } from 'vitest-browser-react';
2
+ import { isValidElement } from 'react';
3
+ import { recorder } from '@describe-me/core';
4
+ import { describeElement } from './describe-element.js';
5
+ import { labelFor } from './label-for.js';
6
+ /**
7
+ * Drop-in for `render` from vitest-browser-react. Records a frame after mount
8
+ * and after every `rerender`.
9
+ */
10
+ export async function render(ui, options) {
11
+ const screen = await baseRender(ui, options);
12
+ if (isValidElement(ui)) {
13
+ const info = describeElement(ui);
14
+ recorder.setComponent(info);
15
+ await recorder.capture('render', labelFor(info), { props: info.props });
16
+ }
17
+ const originalRerender = screen.rerender.bind(screen);
18
+ screen.rerender = (async (next) => {
19
+ const result = await originalRerender(next);
20
+ if (isValidElement(next)) {
21
+ const info = describeElement(next);
22
+ await recorder.capture('render', `rerender ${labelFor(info)}`, { props: info.props });
23
+ }
24
+ return result;
25
+ });
26
+ return screen;
27
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@describe-me/react",
3
+ "version": "0.1.0",
4
+ "description": "React adapter for describe-me: a drop-in render that records frames",
5
+ "keywords": [
6
+ "vitest",
7
+ "storybook",
8
+ "component documentation",
9
+ "visual testing",
10
+ "react",
11
+ "testing"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "Grzegorz Łotysz",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/grzehub/describe-me.git",
18
+ "directory": "packages/react"
19
+ },
20
+ "homepage": "https://grzehub.github.io/describe-me/",
21
+ "bugs": "https://github.com/grzehub/describe-me/issues",
22
+ "type": "module",
23
+ "engines": {
24
+ "node": ">=20"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ }
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "README.md",
38
+ "LICENSE"
39
+ ],
40
+ "sideEffects": false,
41
+ "dependencies": {
42
+ "@describe-me/core": "^0.1.0"
43
+ },
44
+ "peerDependencies": {
45
+ "react": ">=18",
46
+ "vitest-browser-react": ">=2"
47
+ },
48
+ "devDependencies": {
49
+ "@types/react": "^19",
50
+ "react": "^19.3.0",
51
+ "vitest-browser-react": "^2.3.0",
52
+ "vitest": "^5.0.1"
53
+ },
54
+ "scripts": {
55
+ "build": "rimraf dist && tsc -p tsconfig.json"
56
+ }
57
+ }