-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn about variable number of dependencies
We don't check this in prod, since best practice is to always pass these inline. But we should still warn in dev.
- Loading branch information
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
* @jest-environment node | ||
*/ | ||
|
||
/* eslint-disable no-func-assign */ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactFeatureFlags; | ||
let ReactTestRenderer; | ||
|
||
// Additional tests can be found in ReactHooksWithNoopRenderer. Plan is to | ||
// gradually migrate those to this file. | ||
describe('ReactHooks', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false; | ||
ReactFeatureFlags.enableHooks = true; | ||
React = require('react'); | ||
ReactTestRenderer = require('react-test-renderer'); | ||
}); | ||
|
||
it('warns about variable number of dependencies', () => { | ||
const {useLayoutEffect} = React; | ||
function App(props) { | ||
useLayoutEffect(() => { | ||
ReactTestRenderer.unstable_yield( | ||
'Did commit: ' + props.dependencies.join(', '), | ||
); | ||
}, props.dependencies); | ||
return props.dependencies; | ||
} | ||
const root = ReactTestRenderer.create(<App dependencies={['A']} />); | ||
expect(ReactTestRenderer).toHaveYielded(['Did commit: A']); | ||
expect(() => { | ||
root.update(<App dependencies={['A', 'B']} />); | ||
}).toWarnDev([ | ||
'Warning: Detected a variable number of hook dependencies. The length ' + | ||
'of the dependencies array should be constant between renders.\n\n' + | ||
'Previous: A, B\n' + | ||
'Incoming: A', | ||
]); | ||
expect(ReactTestRenderer).toHaveYielded(['Did commit: A, B']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters