Skip to content

Commit

Permalink
Fix ReactFreshIntegration-test not running all tests as assumed (#28033)
Browse files Browse the repository at this point in the history
Fix ReactFreshIntegration-test not running all tests as assumed

`testCommon` was executed twice without setting `compileDestructuring`
ever to true.
This fixes this and removes one layer of abstraction in this test by
using `describe.each`.
  • Loading branch information
kassens committed Jan 23, 2024
1 parent bf32989 commit cb98999
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('ReactFreshIntegration', () => {
}
});

function executeCommon(source, compileDestructuring) {
function executeJavaScript(source, compileDestructuring) {
const compiled = babel.transform(source, {
babelrc: false,
presets: ['@babel/react'],
Expand All @@ -60,6 +60,22 @@ describe('ReactFreshIntegration', () => {
return executeCompiled(compiled);
}

function executeTypescript(source) {
const typescriptSource = babel.transform(source, {
babelrc: false,
configFile: false,
presets: ['@babel/react'],
plugins: [
[freshPlugin, {skipEnvCheck: true}],
['@babel/plugin-syntax-typescript', {isTSX: true}],
],
}).code;
const compiled = ts.transpileModule(typescriptSource, {
module: ts.ModuleKind.CommonJS,
}).outputText;
return executeCompiled(compiled);
}

function executeCompiled(compiled) {
exportsObj = {};
// eslint-disable-next-line no-new-func
Expand All @@ -86,33 +102,19 @@ describe('ReactFreshIntegration', () => {
return ReactFreshRuntime.createSignatureFunctionForTransform();
}

describe('with compiled destructuring', () => {
runTests(executeCommon, testCommon);
});

describe('without compiled destructuring', () => {
runTests(executeCommon, testCommon);
});

describe('with typescript syntax', () => {
runTests(function (source) {
const typescriptSource = babel.transform(source, {
babelrc: false,
configFile: false,
presets: ['@babel/react'],
plugins: [
[freshPlugin, {skipEnvCheck: true}],
['@babel/plugin-syntax-typescript', {isTSX: true}],
],
}).code;
const compiled = ts.transpileModule(typescriptSource, {
module: ts.ModuleKind.CommonJS,
}).outputText;
return executeCompiled(compiled);
}, testTypescript);
});

function runTests(execute, test) {
describe.each([
[
'JavaScript syntax with destructuring enabled',
source => executeJavaScript(source, true),
testJavaScript,
],
[
'JavaScript syntax with destructuring disabled',
source => executeJavaScript(source, false),
testJavaScript,
],
['TypeScript syntax', executeTypescript, testTypeScript],
])('%s', (language, execute, test) => {
function render(source) {
const Component = execute(source);
act(() => {
Expand Down Expand Up @@ -159,9 +161,9 @@ describe('ReactFreshIntegration', () => {
}

test(render, patch);
}
});

function testCommon(render, patch) {
function testJavaScript(render, patch) {
it('reloads function declarations', () => {
if (__DEV__) {
render(`
Expand Down Expand Up @@ -1975,7 +1977,7 @@ describe('ReactFreshIntegration', () => {
});
}

function testTypescript(render, patch) {
function testTypeScript(render, patch) {
it('reloads component exported in typescript namespace', () => {
if (__DEV__) {
render(`
Expand Down

0 comments on commit cb98999

Please sign in to comment.