@describe-me/react 0.1.1 → 0.3.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/README.md CHANGED
@@ -1,9 +1,16 @@
1
1
  # @describe-me/react
2
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.
3
+ The React adapter for [describe-me](https://github.com/grzehub/describe-me):
4
+ drop-in `render` functions that record a frame after mount and after every
5
+ `rerender`, and read the component's name and props for the docs. This is the
6
+ only React-specific code in the project.
7
+
8
+ | entry point | wraps | for |
9
+ | ------------------------------------ | ------------------------ | ------------------------ |
10
+ | `@describe-me/react` | `vitest-browser-react` | Vitest browser mode |
11
+ | `@describe-me/react/testing-library` | `@testing-library/react` | jsdom and other DOM envs |
12
+
13
+ Both test libraries are optional peers: install the one your tests use.
7
14
 
8
15
  ## Install
9
16
 
@@ -13,7 +20,7 @@ pnpm add -D @describe-me/react
13
20
 
14
21
  ## Usage
15
22
 
16
- The package re-exports everything `vitest-browser-react` offers, with `render`
23
+ Each entry point re-exports everything its test library offers, with `render`
17
24
  swapped for the recording one, so switching an existing test is one import.
18
25
 
19
26
  ```tsx
@@ -23,9 +30,17 @@ const screen = await render(<Counter initial={1} />)
23
30
  await step('increment', () => screen.getByRole('button', { name: 'add' }).click())
24
31
  ```
25
32
 
33
+ ```tsx
34
+ import { render } from '@describe-me/react/testing-library'
35
+
36
+ // Synchronous, exactly like the original.
37
+ const { getByRole } = render(<Counter initial={1} />)
38
+ ```
39
+
26
40
  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.
41
+ the plugin redirects `vitest-browser-react` or `@testing-library/react` imports
42
+ to the matching entry point, including imports made from your own
43
+ `test-utils` wrapper, so tests record frames unchanged.
29
44
 
30
45
  See the [root README](https://github.com/grzehub/describe-me#readme) for the
31
46
  full picture.
@@ -0,0 +1,19 @@
1
+ import { render as baseRender } from '@testing-library/react';
2
+ import { type ReactElement } from 'react';
3
+ type RenderOptions = Parameters<typeof baseRender>[1];
4
+ type RenderResult = ReturnType<typeof baseRender>;
5
+ /**
6
+ * Drop-in for `render` from @testing-library/react (jsdom and other DOM
7
+ * environments). Records a frame after mount and after every `rerender`.
8
+ *
9
+ * Synchronous like the original, so `const { asFragment } = render(...)`
10
+ * keeps working. Testing Library wraps mounting in `act()`, which means the DOM
11
+ * is committed when it returns, so frames are captured without settling: the
12
+ * snapshot is taken right here, before the test's next line can change the DOM.
13
+ *
14
+ * Unmounting is handed to the recorder's teardown rather than to Testing
15
+ * Library's own auto-cleanup, which would run before the closing frame is
16
+ * taken. The plugin switches auto-cleanup off with `RTL_SKIP_AUTO_CLEANUP`.
17
+ */
18
+ export declare function render(ui: ReactElement, options?: RenderOptions): RenderResult;
19
+ export {};
@@ -0,0 +1,36 @@
1
+ import { cleanup, render as baseRender } from '@testing-library/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 @testing-library/react (jsdom and other DOM
8
+ * environments). Records a frame after mount and after every `rerender`.
9
+ *
10
+ * Synchronous like the original, so `const { asFragment } = render(...)`
11
+ * keeps working. Testing Library wraps mounting in `act()`, which means the DOM
12
+ * is committed when it returns, so frames are captured without settling: the
13
+ * snapshot is taken right here, before the test's next line can change the DOM.
14
+ *
15
+ * Unmounting is handed to the recorder's teardown rather than to Testing
16
+ * Library's own auto-cleanup, which would run before the closing frame is
17
+ * taken. The plugin switches auto-cleanup off with `RTL_SKIP_AUTO_CLEANUP`.
18
+ */
19
+ export function render(ui, options) {
20
+ recorder.onTeardown(cleanup);
21
+ const result = baseRender(ui, options);
22
+ if (isValidElement(ui)) {
23
+ const info = describeElement(ui);
24
+ recorder.setComponent(info);
25
+ void recorder.capture('render', labelFor(info), { props: info.props }, { settle: false });
26
+ }
27
+ const originalRerender = result.rerender;
28
+ result.rerender = (next) => {
29
+ originalRerender(next);
30
+ if (isValidElement(next)) {
31
+ const info = describeElement(next);
32
+ void recorder.capture('render', `rerender ${labelFor(info)}`, { props: info.props }, { settle: false });
33
+ }
34
+ };
35
+ return result;
36
+ }
@@ -0,0 +1,3 @@
1
+ export * from '@testing-library/react';
2
+ export { step } from '@describe-me/core';
3
+ export { render } from './render-testing-library.js';
@@ -0,0 +1,4 @@
1
+ // Everything @testing-library/react offers, with `render` swapped for the recording one.
2
+ export * from '@testing-library/react';
3
+ export { step } from '@describe-me/core';
4
+ export { render } from './render-testing-library.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@describe-me/react",
3
- "version": "0.1.1",
4
- "description": "React adapter for describe-me: a drop-in render that records frames",
3
+ "version": "0.3.0",
4
+ "description": "React adapter for describe-me: drop-in render functions that record frames",
5
5
  "keywords": [
6
6
  "vitest",
7
7
  "storybook",
@@ -31,6 +31,10 @@
31
31
  ".": {
32
32
  "types": "./dist/index.d.ts",
33
33
  "default": "./dist/index.js"
34
+ },
35
+ "./testing-library": {
36
+ "types": "./dist/testing-library.d.ts",
37
+ "default": "./dist/testing-library.js"
34
38
  }
35
39
  },
36
40
  "files": [
@@ -40,17 +44,30 @@
40
44
  ],
41
45
  "sideEffects": false,
42
46
  "dependencies": {
43
- "@describe-me/core": "^0.1.1"
47
+ "@describe-me/core": "^0.3.0"
44
48
  },
45
49
  "peerDependencies": {
50
+ "@testing-library/react": ">=16",
46
51
  "react": ">=18",
47
52
  "vitest-browser-react": ">=2"
48
53
  },
54
+ "peerDependenciesMeta": {
55
+ "@testing-library/react": {
56
+ "optional": true
57
+ },
58
+ "vitest-browser-react": {
59
+ "optional": true
60
+ }
61
+ },
49
62
  "devDependencies": {
63
+ "@testing-library/dom": "^10.4.0",
64
+ "@testing-library/react": "^16.3.0",
50
65
  "@types/react": "^19",
66
+ "@types/react-dom": "^19",
51
67
  "react": "^19.3.0",
52
- "vitest-browser-react": "^2.3.0",
53
- "vitest": "^5.0.1"
68
+ "react-dom": "^19.3.0",
69
+ "vitest": "^5.0.1",
70
+ "vitest-browser-react": "^2.3.0"
54
71
  },
55
72
  "scripts": {
56
73
  "build": "rimraf dist && tsc -p tsconfig.json"