Skip to content

Commit

Permalink
DevTools: fork FastRefresh test for <18 versions of React (#31893)
Browse files Browse the repository at this point in the history
We currently have a failing test for React DevTools against React 17.
This started failing in #30899,
where we changed logic for error tracking and started relying on
`onPostCommitFiberRoot` hook.

Looking at #21183,
`onPostCommitFiberRoot` was shipped in 18, which means that any console
errors / warnings emitted in passive effects won't be recorded by React
DevTools for React < 18.
  • Loading branch information
hoxyq authored Jan 2, 2025
1 parent 694d3e1 commit 62208be
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,83 @@ describe('Fast Refresh', () => {
expect(getContainer().firstChild).not.toBe(element);
});

// @reactVersion < 18.0
// @reactVersion >= 16.9
it('should not break when there are warnings in between patching', () => {
it('should not break when there are warnings in between patching (before post commit hook)', () => {
withErrorsOrWarningsIgnored(['Expected:'], () => {
render(`
const {useState} = React;
export default function Component() {
const [state, setState] = useState(1);
console.warn("Expected: warning during render");
return null;
}
`);
});
expect(store).toMatchInlineSnapshot(`
✕ 0, ⚠ 1
[root]
<Component> ⚠
`);

withErrorsOrWarningsIgnored(['Expected:'], () => {
patch(`
const {useEffect, useState} = React;
export default function Component() {
const [state, setState] = useState(1);
console.warn("Expected: warning during render");
return null;
}
`);
});
expect(store).toMatchInlineSnapshot(`
✕ 0, ⚠ 2
[root]
<Component> ⚠
`);

withErrorsOrWarningsIgnored(['Expected:'], () => {
patch(`
const {useEffect, useState} = React;
export default function Component() {
const [state, setState] = useState(1);
useEffect(() => {
console.error("Expected: error during effect");
});
console.warn("Expected: warning during render");
return null;
}
`);
});
expect(store).toMatchInlineSnapshot(`
✕ 0, ⚠ 1
[root]
<Component> ⚠
`);

withErrorsOrWarningsIgnored(['Expected:'], () => {
patch(`
const {useEffect, useState} = React;
export default function Component() {
const [state, setState] = useState(1);
console.warn("Expected: warning during render");
return null;
}
`);
});
expect(store).toMatchInlineSnapshot(`
✕ 0, ⚠ 1
[root]
<Component> ⚠
`);
});

// @reactVersion >= 18.0
it('should not break when there are warnings in between patching (with post commit hook)', () => {
withErrorsOrWarningsIgnored(['Expected:'], () => {
render(`
const {useState} = React;
Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-shared/src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ export function installHook(
checkDCE,
onCommitFiberUnmount,
onCommitFiberRoot,
// React v18.0+
onPostCommitFiberRoot,
setStrictMode,

Expand Down

0 comments on commit 62208be

Please sign in to comment.