-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
tests[react-devtools]: added tests for Compiler integration #31241
Merged
hoxyq
merged 1 commit into
facebook:main
from
hoxyq:react-devtools/compiler-integration-tests
Oct 17, 2024
+120
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
101 changes: 101 additions & 0 deletions
101
packages/react-devtools-shared/src/__tests__/compiler-integration-test.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,101 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import {getVersionedRenderImplementation} from './utils'; | ||
|
||
describe('CompilerIntegration', () => { | ||
global.IS_REACT_ACT_ENVIRONMENT = true; | ||
let React; | ||
let act; | ||
let useMemoCache; | ||
|
||
beforeEach(() => { | ||
React = require('react'); | ||
require('react-dom'); | ||
require('react-dom/client'); | ||
useMemoCache = require('react/compiler-runtime').c; | ||
|
||
const utils = require('./utils'); | ||
act = utils.act; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
const {render} = getVersionedRenderImplementation(); | ||
|
||
// @reactVersion >= 18.2 | ||
it('By default, component display names should not have Forget prefix', () => { | ||
const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__; | ||
const reactDOMFiberRendererInterface = hook.rendererInterfaces.get(1); | ||
expect(reactDOMFiberRendererInterface).not.toBeFalsy(); | ||
|
||
const Foo = () => { | ||
// eslint-disable-next-line no-unused-vars | ||
const [val, setVal] = React.useState(null); | ||
|
||
return ( | ||
<div> | ||
<Bar /> | ||
</div> | ||
); | ||
}; | ||
const Bar = () => <div>Hi!</div>; | ||
|
||
act(() => render(<Foo />)); | ||
|
||
expect( | ||
reactDOMFiberRendererInterface | ||
.getDisplayNameForElementID(2) | ||
.indexOf('Forget'), | ||
).toBe(-1); | ||
expect( | ||
reactDOMFiberRendererInterface | ||
.getDisplayNameForElementID(3) | ||
.indexOf('Forget'), | ||
).toBe(-1); | ||
}); | ||
|
||
// For React 18.2, this will install uMC polyfill from react-compiler-runtime available on npm. | ||
// @reactVersion >= 18.2 | ||
it('If useMemoCache used, the corresponding displayName for a component should have Forget prefix', () => { | ||
const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__; | ||
const reactDOMFiberRendererInterface = hook.rendererInterfaces.get(1); | ||
expect(reactDOMFiberRendererInterface).not.toBeFalsy(); | ||
|
||
const Foo = () => { | ||
// eslint-disable-next-line no-unused-vars | ||
const $ = useMemoCache(1); | ||
// eslint-disable-next-line no-unused-vars | ||
const [val, setVal] = React.useState(null); | ||
|
||
return ( | ||
<div> | ||
<Bar /> | ||
</div> | ||
); | ||
}; | ||
const Bar = () => <div>Hi!</div>; | ||
|
||
act(() => render(<Foo />)); | ||
|
||
// useMemoCache is only used by Foo component | ||
expect( | ||
reactDOMFiberRendererInterface | ||
.getDisplayNameForElementID(2) | ||
.indexOf('Forget'), | ||
).toBe(0); | ||
expect( | ||
reactDOMFiberRendererInterface | ||
.getDisplayNameForElementID(3) | ||
.indexOf('Forget'), | ||
).toBe(-1); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems confusing and maybe unnecessary to overrwrite react/compiler-runtime.js, but I see why it's useful to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
react/compiler-runtime.js
is only shipped in React 19, and this check is gated for React [18.2, 19), so we don't override anything, just copying a script to replicate samereact
package structure