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.
Joe Savona committed
May 15, 2023 at 12:12 UTC
2a80ba544e4ad3ce46b896a75cc29d4a751d9842
5 files changed
+10
-114
compiler/forget/src/__tests__/compiler-test.ts
+2
@@ -44,6 +44,8 @@ describe("React Forget", () => {
44
enableOnlyOnUseForgetDirective:
45
options.enableOnlyOnUseForgetDirective,
46
environment: {
47
+ memoizeJsxElements:
48
+ options.environment?.memoizeJsxElements ?? true,
49
customHooks: new Map([
50
[
51
"useFreeze",
compiler/forget/src/__tests__/disableMemoizeJsxElements-test.ts
deleted
-112
@@ -1,112 +0,0 @@
1
-/**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
-"use strict";
9
-
10
-import path from "path";
11
-import { runReactForgetBabelPlugin } from "../Babel/RunReactForgetBabelPlugin";
12
-import { toggleLogging } from "../Utils/logger";
13
-import { Effect, ValueKind } from "../index";
14
-import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures";
15
-
16
-type TestOutput = {
17
- js: string;
18
-};
19
-
20
-function wrapWithTripleBackticks(s: string, ext?: string) {
21
- return `\`\`\`${ext ?? ""}
22
-${s}
23
-\`\`\``;
24
-}
25
-
26
-describe("React Forget (Disable memoization of JSX elements)", () => {
27
- generateTestsFromFixtures(
28
- path.join(__dirname, "fixtures", "disableMemoizeJsxElements"),
29
- (input, file, options) => {
30
- let items: Array<TestOutput> = [];
31
- let error: Error | null = null;
32
- if (options.debug) {
33
- toggleLogging(options.debug);
34
- }
35
- try {
36
- items.push({
37
- js: runReactForgetBabelPlugin(input, file, options.language, {
38
- enableOnlyOnUseForgetDirective:
39
- options.enableOnlyOnUseForgetDirective,
40
- environment: {
41
- memoizeJsxElements: false,
42
- customHooks: new Map([
43
- [
44
- "useFreeze",
45
- {
46
- name: "useFreeze",
47
- kind: "Custom",
48
- valueKind: ValueKind.Frozen,
49
- effectKind: Effect.Freeze,
50
- },
51
- ],
52
- ]),
53
- inlineUseMemo: options.environment?.inlineUseMemo ?? false,
54
- },
55
- logger: null,
56
- gating: options.gating,
57
- panicOnBailout: options.panicOnBailout,
58
- }).code,
59
- });
60
- } catch (e) {
61
- error = e;
62
- }
63
- let outputs: Array<string>;
64
-
65
- const expectError = file.startsWith("error.");
66
- if (expectError) {
67
- if (error === null) {
68
- throw new Error(
69
- `Expected an error to be thrown for fixture: '${file}', remove the 'error.' prefix if an error is not expected.`
70
- );
71
- } else {
72
- outputs = [formatErrorOutput(error)];
73
- }
74
- } else {
75
- if (error !== null) {
76
- error.message = `Expected fixture '${file}' to succeed but it failed with error:\n\n${error.message}`;
77
- throw error;
78
- }
79
- if (items === null || items.length === 0) {
80
- throw new Error(`Expected at least one output for file '${file}'.`);
81
- }
82
- outputs = formatOutput(items);
83
- }
84
- return `
85
-## Input
86
-
87
-${wrapWithTripleBackticks(input, "javascript")}
88
-
89
-${outputs.join("\n")}
90
- `;
91
- }
92
- );
93
-});
94
-
95
-function formatErrorOutput(error: Error): string {
96
- error.message = error.message.replace(/^\/.*?:\s/, "");
97
- return `
98
-## Error
99
-
100
-${wrapWithTripleBackticks(error.message)}
101
- `;
102
-}
103
-
104
-function formatOutput(items: Array<TestOutput>): Array<string> {
105
- return items.map(({ js }) => {
106
- return `
107
-## Code
108
-
109
-${wrapWithTripleBackticks(js, "javascript")}
110
- `.trim();
111
- });
112
-}
compiler/forget/src/__tests__/fixtures/compiler/disable-jsx-memoization.expect.md
renamed
+2
-1
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @memoizeJsxElements false
6
function Component(props) {
7
const [name, setName] = useState(null);
8
const onChange = function (e) {
@@ -19,7 +20,7 @@ function Component(props) {
20
## Code
21
22
```javascript
22
-import { unstable_useMemoCache as useMemoCache } from "react";
23
+import { unstable_useMemoCache as useMemoCache } from "react"; // @memoizeJsxElements false
24
function Component(props) {
25
const $ = useMemoCache(2);
26
const [name, setName] = useState(null);
compiler/forget/src/__tests__/fixtures/compiler/disable-jsx-memoization.js
renamed
+1
@@ -1,3 +1,4 @@
1
+// @memoizeJsxElements false
2
function Component(props) {
3
const [name, setName] = useState(null);
4
const onChange = function (e) {
compiler/forget/src/__tests__/test-utils/generateTestsFromFixtures.ts
+5
-1
@@ -96,6 +96,7 @@ export default function generateTestsFromFixtures(
96
let gating: GatingOptions | null = null;
97
let inlineUseMemo = true;
98
let panicOnBailout = true;
99
+ let memoizeJsxElements = true;
100
101
if (inputFile != null) {
102
input = fs.readFileSync(inputFile, "utf8");
@@ -124,13 +125,16 @@ export default function generateTestsFromFixtures(
125
if (lines[0]!.indexOf("@panicOnBailout false") !== -1) {
126
panicOnBailout = false;
127
}
128
+ if (lines[0]!.indexOf("@memoizeJsxElements false") !== -1) {
129
+ memoizeJsxElements = false;
130
+ }
131
}
132
133
testCommand(basename, () => {
134
let receivedOutput;
135
if (input !== null) {
136
receivedOutput = transform(input, basename, {
133
- environment: { inlineUseMemo },
137
+ environment: { inlineUseMemo, memoizeJsxElements },
138
logger: null,
139
debug,
140
enableOnlyOnUseForgetDirective,