Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Warn on NaN in dep arrays instead of throwing #4527

Merged
merged 2 commits into from
Oct 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: andrewiggins/download-base-artifact@v3
with:
artifact: npm-package
workflow: build-test.yml
workflow: ci.yml
Copy link
Member Author

@rschristian rschristian Oct 12, 2024

Choose a reason for hiding this comment

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

The CI should now be completely on track again.

Sorry the recent changes had so many issues, the fact that our setup runs both the PR & main workflows (as they exist on both branches) simultaneously made this a bit weird to figure out all the details on.

required: true
- run: mv preact.tgz preact-main.tgz
- name: Upload locally build & base preact package
Expand Down
2 changes: 1 addition & 1 deletion debug/src/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ export function initDebug() {
const arg = hook._args[j];
if (isNaN(arg)) {
const componentName = getDisplayName(vnode);
throw new Error(
console.warn(
`Invalid argument passed to hook. Hooks should not be called with NaN in the dependency array. Hook index ${i} in component ${componentName} was called with NaN.`
);
}
Expand Down
21 changes: 14 additions & 7 deletions debug/test/browser/validateHookArgs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ describe('Hook argument validation', () => {
};

it(`should error if ${name} is mounted with NaN as an argument`, async () => {
expect(() =>
render(<TestComponent initialValue={NaN} />, scratch)
).to.throw(/Hooks should not be called with NaN in the dependency array/);
render(<TestComponent initialValue={NaN} />, scratch);
expect(console.warn).to.be.calledOnce;
expect(console.warn.args[0]).to.match(
/Hooks should not be called with NaN in the dependency array/
);
});

it(`should error if ${name} is updated with NaN as an argument`, async () => {
render(<TestComponent initialValue={0} />, scratch);

expect(() => {
scratch.querySelector('button').click();
rerender();
}).to.throw(
scratch.querySelector('button').click();
rerender();

expect(console.warn).to.be.calledOnce;
expect(console.warn.args[0]).to.match(
/Hooks should not be called with NaN in the dependency array/
);
});
Expand All @@ -52,14 +55,18 @@ describe('Hook argument validation', () => {
let scratch;
/** @type {() => void} */
let rerender;
let warnings = [];

beforeEach(() => {
scratch = setupScratch();
rerender = setupRerender();
warnings = [];
sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
});

afterEach(() => {
teardown(scratch);
console.warn.restore();
});

validateHook('useEffect', arg => useEffect(() => {}, [arg]));
Expand Down