prepack/scripts/test-react.js

358 lines
10 KiB
JavaScript
Raw Normal View History

Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* @flow */
let fs = require("fs");
let path = require("path");
let { prepackSources } = require("../lib/prepack-node.js");
let babel = require("babel-core");
let React = require("react");
let ReactTestRenderer = require("react-test-renderer");
let { mergeAdacentJSONTextNodes } = require("../lib/utils/json.js");
/* eslint-disable no-undef */
let { expect, describe, it } = global;
function cxShim(...args) {
let classNames = [];
for (let arg of args) {
if (typeof arg === "string") {
classNames.push(arg);
} else if (typeof arg === "object" && arg !== null) {
let keys = Object.keys(arg);
for (let key of keys) {
if (arg[key]) {
classNames.push(key);
}
}
}
}
return classNames.join(" ");
}
// assign for tests that use the cx() global
global.cx = cxShim;
function runTestSuite(outputJsx) {
let reactTestRoot = path.join(__dirname, "../test/react/");
let prepackOptions = {
compatibility: "fb-www",
internalDebug: true,
serialize: true,
uniqueSuffix: "",
maxStackDepth: 100,
reactEnabled: true,
reactOutput: outputJsx ? "jsx" : "create-element",
inlineExpressions: true,
omitInvariants: true,
abstractEffectsInAdditionalFunctions: true,
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
};
function compileSourceWithPrepack(source) {
let code = `(function(){${source}})()`;
let serialized = prepackSources([{ filePath: "", fileContents: code, sourceMapContents: "" }], prepackOptions);
if (serialized == null || serialized.reactStatistics == null) {
throw new Error("React test runner failed during serialization");
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
}
return {
compiledSource: serialized.code,
statistics: serialized.reactStatistics,
};
}
function runSource(source) {
let transformedSource = babel.transform(source, {
presets: ["babel-preset-react"],
plugins: ["transform-object-rest-spread"],
}).code;
/* eslint-disable no-new-func */
let fn = new Function("require", "module", transformedSource);
let moduleShim = { exports: null };
let requireShim = name => {
switch (name) {
case "React":
case "react":
return React;
case "RelayModern":
return {
QueryRenderer(props) {
return props.render({ props: {}, error: null });
},
createFragmentContainer() {
return null;
},
graphql() {
return null;
},
};
case "cx":
return cxShim;
case "FBEnvironment":
return {};
default:
throw new Error(`Unrecognized import: "${name}".`);
}
};
try {
// $FlowFixMe flow doesn't new Function
fn(requireShim, moduleShim);
} catch (e) {
console.error(transformedSource);
throw e;
}
return moduleShim.exports;
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
}
async function runTest(directory, name) {
let source = fs.readFileSync(path.join(reactTestRoot, directory, name)).toString();
let { compiledSource, statistics } = compileSourceWithPrepack(source);
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
expect(statistics).toMatchSnapshot();
let A = runSource(source);
let B = runSource(compiledSource);
expect(typeof A).toBe(typeof B);
if (typeof A !== "function") {
// Test without exports just verifies that the file compiles.
return;
}
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
let rendererA = ReactTestRenderer.create(null);
let rendererB = ReactTestRenderer.create(null);
if (A == null || B == null) {
throw new Error("React test runner issue");
}
// Use the original version of the test in case transforming messes it up.
let { getTrials } = A;
// Run tests that assert the rendered output matches.
let resultA = getTrials(rendererA, A);
let resultB = getTrials(rendererB, B);
// The test has returned many values for us to check
for (let i = 0; i < resultA.length; i++) {
let [nameA, valueA] = resultA[i];
let [nameB, valueB] = resultB[i];
expect(mergeAdacentJSONTextNodes(valueB)).toEqual(mergeAdacentJSONTextNodes(valueA));
expect(nameB).toEqual(nameA);
}
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
}
// Jest tests
let originalConsoleError = console.error;
describe(`Test React (${outputJsx ? "JSX" : "create-element"})`, () => {
describe("Functional component folding", () => {
let directory = "functional-components";
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Simple", async () => {
await runTest(directory, "simple.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Simple 2", async () => {
await runTest(directory, "simple-2.js");
});
it("Simple 3", async () => {
await runTest(directory, "simple-3.js");
});
it("Simple 4", async () => {
await runTest(directory, "simple-4.js");
});
it("Simple 5", async () => {
await runTest(directory, "simple-5.js");
});
it("Simple 6", async () => {
await runTest(directory, "simple-6.js");
});
it("Simple fragments", async () => {
await runTest(directory, "simple-fragments.js");
});
it("Simple children", async () => {
await runTest(directory, "simple-children.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Simple refs", async () => {
await runTest(directory, "simple-refs.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Circular reference", async () => {
await runTest(directory, "circular-reference.js");
});
it("Conditional", async () => {
await runTest(directory, "conditional.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Key nesting", async () => {
await runTest(directory, "key-nesting.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Key nesting 2", async () => {
await runTest(directory, "key-nesting-2.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Key nesting 3", async () => {
await runTest(directory, "key-nesting-3.js");
});
it("Key change", async () => {
await runTest(directory, "key-change.js");
});
it("Key change with fragments", async () => {
await runTest(directory, "key-change-fragments.js");
});
it("Key not changing with fragments", async () => {
await runTest(directory, "key-not-change-fragments.js");
});
it("Component type change", async () => {
await runTest(directory, "type-change.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Component type same", async () => {
await runTest(directory, "type-same.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Dynamic props", async () => {
await runTest(directory, "dynamic-props.js");
});
Add React functional component folding Summary: Release note: Adds experimental React functional component folding optimizations This PR is stacked upon PRs #1118 and #1117. Thus, those PRs should be merged before this PR is merged to reduce noise in the diff. This PR adds a new React Reconciler into Prepack's serialization process, so that React components trees can be folded/inlined into a single component at build time. To fold a component tree, it must be explicitly done via `__registerReactComponentRoot(nameOfComponent)`. This PR only attempts to fold React functional components, not React ES2015 class components (that will come in another PR at a later date). Furthermore, the `props` parameter on a root component must contain Flow type annotations (otherwise we will have no idea what the values might be). Support flow `propTypes` might also be an addition, but not for this PR. If the reconciler comes across a component that it cannot fold/inline, it will "bail-out" and try and continue the process without that particular component being folded into the tree. An example of how this all works (input): ```jsx function App(props: {title: string}) { return ( <div> <ChildComponent title={props.title} /> </div> ); } function ChildComponent(props) { return ( <span> <SubChildComponent {...props} /> </span> ); } function SubChildComponent(props) { return <span>{props.title.toString()}</span> } __registerReactComponentRoot(App); global.App = App; ``` Output: ```jsx (function () { "use strict"; var _$1 = this; var _0 = function (props) { var _$0 = props.title; return <div><span><span>{_$0}</span></span></div>; }; _$1.App = _0; }).call(this); ``` Closes https://github.com/facebook/prepack/pull/1120 Differential Revision: D6237333 Pulled By: trueadm fbshipit-source-id: b58c7d8979ca79a766bb2ee2eb01a380d37c3101
2017-11-06 15:54:12 +03:00
it("Dynamic context", async () => {
await runTest(directory, "dynamic-context.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("React.cloneElement", async () => {
await runTest(directory, "clone-element.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Return text", async () => {
await runTest(directory, "return-text.js");
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Return undefined", async () => {
// this test will cause a React console.error to show
// we monkey patch it to stop it polluting the test output
// with a false-negative error
global.console.error = () => {};
try {
await runTest(directory, "return-undefined.js");
} finally {
global.console.error = originalConsoleError;
}
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
it("Class component as root", async () => {
await runTest(directory, "class-root.js");
});
it("Class component as root with multiple render methods", async () => {
await runTest(directory, "class-root-with-render-methods.js");
});
it("Class component as root with props", async () => {
await runTest(directory, "class-root-with-props.js");
});
it("Class component as root with state", async () => {
await runTest(directory, "class-root-with-state.js");
});
it("Class component as root with refs", async () => {
await runTest(directory, "class-root-with-refs.js");
});
it("Class component as root with instance variables", async () => {
await runTest(directory, "class-root-with-instance-vars.js");
});
it("Class component as root with instance variables #2", async () => {
await runTest(directory, "class-root-with-instance-vars-2.js");
});
it("Additional functions closure scope capturing", async () => {
await runTest(directory, "additional-function-regression.js");
});
});
describe("Class component folding", () => {
let directory = "class-components";
it("Simple classes", async () => {
await runTest(directory, "simple-classes.js");
});
it("Simple classes #2", async () => {
await runTest(directory, "simple-classes-2.js");
});
it("Simple classes #3", async () => {
await runTest(directory, "simple-classes-3.js");
});
it("Inheritance chaining", async () => {
await runTest(directory, "inheritance-chain.js");
});
it("Classes with state", async () => {
await runTest(directory, "classes-with-state.js");
});
});
describe("Factory class component folding", () => {
let directory = "factory-components";
it("Simple factory classes", async () => {
await runTest(directory, "simple.js");
});
it("Simple factory classes 2", async () => {
await runTest(directory, "simple2.js");
});
});
describe("fb-www mocks", () => {
let directory = "mocks";
it("fb-www", async () => {
await runTest(directory, "fb1.js");
});
it("fb-www 2", async () => {
await runTest(directory, "fb2.js");
});
it("fb-www 3", async () => {
await runTest(directory, "fb3.js");
});
it("fb-www 4", async () => {
await runTest(directory, "fb4.js");
});
it("fb-www 5", async () => {
await runTest(directory, "fb5.js");
});
it("fb-www 6", async () => {
await runTest(directory, "fb6.js");
});
});
Add React/Jest testing infrastructure Summary: Release note: none This PR adds the React/Jest testing infrastructure to Prepack and is stacked on the PR https://github.com/facebook/prepack/pull/1117. Thus, that PR should be merged before this PR is merged to reduce noise in the diff. This will allow us to test various runtime outputs of React 16 when running original source vs Prepacked source to see if there are any issues/differences that might have an impact on applications. The primary reason for this is to track regressions for the component folding PRs that will land in the future. Please note, this PR does not contain any reconciler changes, thus `__registerReactComponentRoot(App);` has been commented out of tests to ensure they don't prematurely fail. A follow up PR will enable them once the other React functional component folding PRs get landed. This PR also adds some mock React globals to be used within tests (and maybe to be further integrated into folding at a future point too). The mocks include `React.Component` and `React.cloneElement` for now. Furthermore, a `utils/json.js` utility file exists to help normalize the results from the React test renderer so that adjacent JSON text nodes get merged, which is something that may exist because of how the reconciler (once the PR lands) handles inlining of child nodes. The command to run React tests is `yarn test-react`. Closes https://github.com/facebook/prepack/pull/1118 Reviewed By: cblappert Differential Revision: D6208263 Pulled By: trueadm fbshipit-source-id: d54f3b0e1cc3240e1f142d3da08bc279e4153889
2017-11-02 14:28:37 +03:00
});
}
Fold simple class components Summary: Release note: Adds support for folding simple React class components This PR adds the concept of "simple" class components. A "simple" class component is defined as: - having only a "render" method or many method, i.e. `render()`, `_renderHeader()`, `_renderFooter()` - having no lifecycle events - having no state - having no instance variables The only things a class component should be able to access on "this" are: - `this.props` - `this.context` - `this._someRenderMethodX()` etc It does this by first attempting to evaluate any class component with the heuristics as if it were a simple class component. If that throws an error (entirely expected for most class components), it catches the error and falls back to trying to use the "complex" class component route for evaluation instead. Furthermore, I've added "transforms". This allows post-transforms of the serialized AST for additional functions. The simple class component adds a transform to convert AST nodes of `this.props` to `props` and `this.context` to `context` respectfully – whilst adding arguments to the function; something that is necessary to convert simple class component render methods to functional components. This PR also adds logic to squash simple class components into functional components at the root (as simple class components are easily convertible to functional components). Closes https://github.com/facebook/prepack/pull/1204 Differential Revision: D6485508 Pulled By: trueadm fbshipit-source-id: 06ebeebad5d23c0677a0e3f0cb8dd23c45735429
2017-12-05 19:10:54 +03:00
runTestSuite(true);
runTestSuite(false);