FastRefreshDevToolsIntegration Test
FastRefreshDevToolsIntegration Test
afterEach(() => {
jest.resetModules();
});
beforeEach(() => {
exportsObj = undefined;
container = document.createElement('div');
babel = require('@babel/core');
freshPlugin = require('react-refresh/babel');
store = global.store;
React = require('react');
ReactFreshRuntime = require('react-refresh/runtime');
ReactFreshRuntime.injectIntoGlobalHook(global);
function execute(source) {
const compiled = babel.transform(source, {
babelrc: false,
presets: ['@babel/react'],
plugins: [
[freshPlugin, {skipEnvCheck: true}],
'@babel/plugin-transform-modules-commonjs',
'@babel/plugin-transform-destructuring',
].filter(Boolean),
}).code;
exportsObj = {};
// eslint-disable-next-line no-new-func
new Function(
'global',
'React',
'exports',
'$RefreshReg$',
'$RefreshSig$',
compiled,
)(global, React, exportsObj, $RefreshReg$, $RefreshSig$);
// Module systems will register exports as a fallback.
// This is useful for cases when e.g. a class is exported,
// and we don't want to propagate the update beyond this module.
$RefreshReg$(exportsObj.default, 'exports.default');
return exportsObj.default;
}
function render(source) {
const Component = execute(source);
act(() => {
legacyRender(<Component />, container);
});
// Module initialization shouldn't be counted as a hot update.
expect(ReactFreshRuntime.performReactRefresh()).toBe(null);
}
function patch(source) {
const prevExports = exportsObj;
execute(source);
const nextExports = exportsObj;
function $RefreshSig$() {
return ReactFreshRuntime.createSignatureFunctionForTransform();
}
function Child() {
return <div />;
};
patch(`
function Parent() {
return <Child key="A" />;
};
function Child() {
return <div />;
};
patch(`
function Parent() {
return <Child key="B" />;
};
function Child() {
return <div />;
};
export default Parent;
`);
expect(store).toMatchInlineSnapshot(`
[root]
▾ <Parent>
<Child key="B">
`);
withErrorsOrWarningsIgnored(['Expected:'], () => {
patch(`
const {useEffect, useState} = React;
withErrorsOrWarningsIgnored(['Expected:'], () => {
patch(`
const {useEffect, useState} = React;
withErrorsOrWarningsIgnored(['Expected:'], () => {
patch(`
const {useEffect, useState} = React;
// TODO (bvaughn) Write a test that checks in between the steps of patch
});