Move DisableJsxMemoization-test into main compiler test w pragma

I originally created a separate test for the mode with JSX memoization disabled, 
but we can merge this into the main compiler-test and enable the feature with a 
pragma.
This commit is contained in:
Joe Savona
2023-05-15 12:12:53 -07:00
parent 8bf1ec97a4
commit 2a80ba544e
5 changed files with 10 additions and 114 deletions
@@ -44,6 +44,8 @@ describe("React Forget", () => {
enableOnlyOnUseForgetDirective:
options.enableOnlyOnUseForgetDirective,
environment: {
memoizeJsxElements:
options.environment?.memoizeJsxElements ?? true,
customHooks: new Map([
[
"useFreeze",
@@ -1,112 +0,0 @@
/**
* 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.
*/
"use strict";
import path from "path";
import { runReactForgetBabelPlugin } from "../Babel/RunReactForgetBabelPlugin";
import { toggleLogging } from "../Utils/logger";
import { Effect, ValueKind } from "../index";
import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures";
type TestOutput = {
js: string;
};
function wrapWithTripleBackticks(s: string, ext?: string) {
return `\`\`\`${ext ?? ""}
${s}
\`\`\``;
}
describe("React Forget (Disable memoization of JSX elements)", () => {
generateTestsFromFixtures(
path.join(__dirname, "fixtures", "disableMemoizeJsxElements"),
(input, file, options) => {
let items: Array<TestOutput> = [];
let error: Error | null = null;
if (options.debug) {
toggleLogging(options.debug);
}
try {
items.push({
js: runReactForgetBabelPlugin(input, file, options.language, {
enableOnlyOnUseForgetDirective:
options.enableOnlyOnUseForgetDirective,
environment: {
memoizeJsxElements: false,
customHooks: new Map([
[
"useFreeze",
{
name: "useFreeze",
kind: "Custom",
valueKind: ValueKind.Frozen,
effectKind: Effect.Freeze,
},
],
]),
inlineUseMemo: options.environment?.inlineUseMemo ?? false,
},
logger: null,
gating: options.gating,
panicOnBailout: options.panicOnBailout,
}).code,
});
} catch (e) {
error = e;
}
let outputs: Array<string>;
const expectError = file.startsWith("error.");
if (expectError) {
if (error === null) {
throw new Error(
`Expected an error to be thrown for fixture: '${file}', remove the 'error.' prefix if an error is not expected.`
);
} else {
outputs = [formatErrorOutput(error)];
}
} else {
if (error !== null) {
error.message = `Expected fixture '${file}' to succeed but it failed with error:\n\n${error.message}`;
throw error;
}
if (items === null || items.length === 0) {
throw new Error(`Expected at least one output for file '${file}'.`);
}
outputs = formatOutput(items);
}
return `
## Input
${wrapWithTripleBackticks(input, "javascript")}
${outputs.join("\n")}
`;
}
);
});
function formatErrorOutput(error: Error): string {
error.message = error.message.replace(/^\/.*?:\s/, "");
return `
## Error
${wrapWithTripleBackticks(error.message)}
`;
}
function formatOutput(items: Array<TestOutput>): Array<string> {
return items.map(({ js }) => {
return `
## Code
${wrapWithTripleBackticks(js, "javascript")}
`.trim();
});
}
@@ -2,6 +2,7 @@
## Input
```javascript
// @memoizeJsxElements false
function Component(props) {
const [name, setName] = useState(null);
const onChange = function (e) {
@@ -19,7 +20,7 @@ function Component(props) {
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import { unstable_useMemoCache as useMemoCache } from "react"; // @memoizeJsxElements false
function Component(props) {
const $ = useMemoCache(2);
const [name, setName] = useState(null);
@@ -1,3 +1,4 @@
// @memoizeJsxElements false
function Component(props) {
const [name, setName] = useState(null);
const onChange = function (e) {
@@ -96,6 +96,7 @@ export default function generateTestsFromFixtures(
let gating: GatingOptions | null = null;
let inlineUseMemo = true;
let panicOnBailout = true;
let memoizeJsxElements = true;
if (inputFile != null) {
input = fs.readFileSync(inputFile, "utf8");
@@ -124,13 +125,16 @@ export default function generateTestsFromFixtures(
if (lines[0]!.indexOf("@panicOnBailout false") !== -1) {
panicOnBailout = false;
}
if (lines[0]!.indexOf("@memoizeJsxElements false") !== -1) {
memoizeJsxElements = false;
}
}
testCommand(basename, () => {
let receivedOutput;
if (input !== null) {
receivedOutput = transform(input, basename, {
environment: { inlineUseMemo },
environment: { inlineUseMemo, memoizeJsxElements },
logger: null,
debug,
enableOnlyOnUseForgetDirective,