Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
react-version: [16, 17]
testing-library-version: [12]
include:
- react-version: 16
testing-library-version: 12
- react-version: 17
testing-library-version: 12
- react-version: 18
testing-library-version: 16
- react-version: 19
testing-library-version: 16.3.2
steps:
- name: Checkout
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"sanitize-html": "2.17.0"
},
"peerDependencies": {
"react": "^16.14.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0"
"react": "^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
"react-dom": "^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
},
"devDependencies": {
"@babel/core": "7.28.5",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/quiz/use-quiz.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, act } from "@testing-library/react-hooks";
import { renderHook, act } from "../test-utils/renderHook";

import { useQuiz } from "./use-quiz";

Expand Down
48 changes: 48 additions & 0 deletions src/test-utils/renderHook.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a temporary solution.

CI failed for the React 19 matrix because hook tests used @testing-library/react-hooks, which internally calls ReactDOM.render — a function removed in React 19. This caused TypeError: ReactDOM.render is not a function in tests.

Once /learn is upgraded to React 19. I'll update the library to drop support for older React versions and remove this test util.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Runtime `renderHook` helper that picks a compatible implementation based on
* the installed testing-library packages:
*
* - Prefer `@testing-library/react`'s `renderHook` (available in newer RTL).
* - Fallback to `@testing-library/react-hooks` for older setups.
*
* This keeps tests working across React versions (React 16/17/18 with RHTL,
* React 18/19 with RTL's `renderHook`).
*/

import * as rtl from "@testing-library/react";
import * as rhtl from "@testing-library/react-hooks";

type ActFn = (cb: () => void) => void;

type RenderHookResult<T = unknown> = {
result: { current: T };
};

// Generic renderHook signature used by our tests: accepts a callback returning
// a value and returns an object with a `result.current` property.
let renderHook: <T = unknown>(cb: () => T) => RenderHookResult<T>;
let actImpl: ActFn | undefined;

type RTLLike = {
renderHook?: <T = unknown>(cb: () => T) => RenderHookResult<T>;
act?: ActFn;
};

if (typeof (rtl as unknown as RTLLike).renderHook === "function") {
const r = rtl as unknown as Required<RTLLike>;
renderHook = r.renderHook;
actImpl = r.act;
} else {
const h = rhtl as unknown as Required<RTLLike>;
renderHook = h.renderHook;
actImpl = h.act;
}

const act: ActFn = (cb: () => void) => {
if (actImpl) return actImpl(cb);
// Fallback synchronous runner for environments without `act`.
return cb();
};

export { renderHook, act };
export default renderHook;