Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove existing logs and warnings from tests #48391

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/react-native-fantom/runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ function parseRNTesterCommandResult(result: ReturnType<typeof runBuck2>): {
} {
const stdout = result.stdout.toString();

const outputArray = stdout
.trim()
.split('\n')
.filter(log => !log.startsWith('Running "')); // remove AppRegistry logs.
const outputArray = stdout.trim().split('\n');

// The last line should be the test output in JSON format
const testResultJSON = outputArray.pop();
Expand Down
68 changes: 43 additions & 25 deletions packages/react-native-fantom/src/__tests__/Fantom-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,30 @@ describe('Fantom', () => {

// TODO: when error handling is fixed, this should verify using `toThrow`
it('should throw when running a task inside another task', () => {
let lastCallbackExecuted = 0;
let threw = false;

runTask(() => {
lastCallbackExecuted = 1;
runTask(() => {
lastCallbackExecuted = 2;
throw new Error('Recursive runTask should be unreachable');
});
// TODO replace with expect(() => { ... }).toThrow() when error handling is fixed
try {
runTask(() => {});
} catch {
threw = true;
}
});
expect(lastCallbackExecuted).toBe(1);
expect(threw).toBe(true);

threw = false;

runTask(() => {
queueMicrotask(() => {
lastCallbackExecuted = 3;
runTask(() => {
lastCallbackExecuted = 4;
throw new Error(
'Recursive runTask from micro-task should be unreachable',
);
});
try {
runTask(() => {});
} catch {
threw = true;
}
});
});
expect(lastCallbackExecuted).toBe(3);
expect(threw).toBe(true);
});
});

Expand Down Expand Up @@ -125,16 +127,24 @@ describe('Fantom', () => {
runTask(() => {
root.render(
<>
<View style={{width: 100, height: 100}} collapsable={false} />
<View style={{width: 100, height: 100}} collapsable={false} />
<View
key="first"
style={{width: 100, height: 100}}
collapsable={false}
/>
<View
key="second"
style={{width: 100, height: 100}}
collapsable={false}
/>
</>,
);
});

expect(root.getRenderedOutput().toJSX()).toEqual(
<>
<rn-view width="100.000000" height="100.000000" />
<rn-view width="100.000000" height="100.000000" />
<rn-view key="0" width="100.000000" height="100.000000" />
<rn-view key="1" width="100.000000" height="100.000000" />
</>,
);

Expand Down Expand Up @@ -233,18 +243,26 @@ describe('Fantom', () => {
runTask(() => {
root.render(
<>
<View style={{width: 100, height: 100}} collapsable={false} />
<Text>hello world!</Text>
<View style={{width: 200, height: 300}} collapsable={false} />
<View
key="first"
style={{width: 100, height: 100}}
collapsable={false}
/>
<Text key="second">hello world!</Text>
<View
key="third"
style={{width: 200, height: 300}}
collapsable={false}
/>
</>,
);
});

expect(root.getRenderedOutput({props: []}).toJSX()).toEqual(
<>
<rn-view />
<rn-paragraph>hello world!</rn-paragraph>
<rn-view />
<rn-view key="0" />
<rn-paragraph key="1">hello world!</rn-paragraph>
<rn-view key="2" />
</>,
);

Expand Down
15 changes: 10 additions & 5 deletions packages/react-native-fantom/src/getFantomRenderedOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @oncall react_native
*/

import FantomModule from './specs/NativeFantomModule';
import NativeFantom from './specs/NativeFantom';
// $FlowExpectedError[untyped-import]
import micromatch from 'micromatch';
import * as React from 'react';
Expand Down Expand Up @@ -114,7 +114,7 @@ export default function getFantomRenderedOutput(
} = config;
return new FantomRenderedOutput(
JSON.parse(
FantomModule.getRenderedOutput(surfaceId, {
NativeFantom.getRenderedOutput(surfaceId, {
includeRoot,
includeLayoutMetrics,
}),
Expand Down Expand Up @@ -152,16 +152,20 @@ function convertRawJsonToJSX(
function createJSXElementForTestComparison(
type: string,
props: mixed,
key?: ?string,
): React.Node {
const Tag = type;
return <Tag {...props} />;
return <Tag key={key} {...props} />;
}

function rnTypeToTestType(type: string): string {
return `rn-${type.substring(0, 1).toLowerCase() + type.substring(1)}`;
}

function jsonChildToJSXChild(jsonChild: FantomJsonObject | string): React.Node {
function jsonChildToJSXChild(
jsonChild: FantomJsonObject | string,
index?: ?number,
): React.Node {
if (typeof jsonChild === 'string') {
return jsonChild;
} else {
Expand All @@ -172,6 +176,7 @@ function jsonChildToJSXChild(jsonChild: FantomJsonObject | string): React.Node {
jsxChildren == null
? jsonChild.props
: {...jsonChild.props, children: jsxChildren},
index != null ? String(index) : undefined,
);
}
}
Expand All @@ -184,7 +189,7 @@ function jsonChildrenToJSXChildren(jsonChildren: FantomJsonObject['children']) {
let allJSXChildrenAreStrings = true;
let jsxChildrenString = '';
for (let i = 0; i < jsonChildren.length; i++) {
const jsxChild = jsonChildToJSXChild(jsonChildren[i]);
const jsxChild = jsonChildToJSXChild(jsonChildren[i], i);
jsxChildren.push(jsxChild);
if (allJSXChildrenAreStrings) {
if (typeof jsxChild === 'string') {
Expand Down
12 changes: 6 additions & 6 deletions packages/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import type {MixedElement} from 'react';

import getFantomRenderedOutput from './getFantomRenderedOutput';
import FantomModule from './specs/NativeFantomModule';
import NativeFantom from './specs/NativeFantom';
import ReactFabric from 'react-native/Libraries/Renderer/shims/ReactFabric';

let globalSurfaceIdCounter = 1;
Expand All @@ -35,21 +35,21 @@ class Root {

render(element: MixedElement) {
if (!this.#hasRendered) {
FantomModule.startSurface(this.#surfaceId);
NativeFantom.startSurface(this.#surfaceId);
this.#hasRendered = true;
}

ReactFabric.render(element, this.#surfaceId, () => {}, true);
}

getMountingLogs(): Array<string> {
return FantomModule.getMountingManagerLogs(this.#surfaceId);
return NativeFantom.getMountingManagerLogs(this.#surfaceId);
}

destroy() {
// TODO: check for leaks.
FantomModule.stopSurface(this.#surfaceId);
FantomModule.flushMessageQueue();
NativeFantom.stopSurface(this.#surfaceId);
NativeFantom.flushMessageQueue();
}

getRenderedOutput(config: RenderOutputConfig = {}): FantomRenderedOutput {
Expand Down Expand Up @@ -100,7 +100,7 @@ export function runWorkLoop(): void {

try {
flushingQueue = true;
FantomModule.flushMessageQueue();
NativeFantom.flushMessageQueue();
} finally {
flushingQueue = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ interface Spec extends TurboModule {
getRenderedOutput: (surfaceId: number, config: RenderFormatOptions) => string;
}

export default TurboModuleRegistry.getEnforcing<Spec>('Fantom') as Spec;
export default TurboModuleRegistry.getEnforcing<Spec>(
'NativeFantomCxx',
) as Spec;
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -549,7 +548,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -610,7 +608,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -942,7 +939,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -1003,7 +999,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -1314,13 +1309,9 @@ describe('IntersectionObserver', () => {
});
expect(node.isConnected).toBe(false);

Fantom.runTask(() => {
observer = new IntersectionObserver(() => {});
observer.observe(node);
// TODO what happens if this throws an exception?
observer.unobserve(node);
throw new Error('unobserve should not throw');
});
observer = new IntersectionObserver(() => {});
observer.observe(node);
observer.unobserve(node);
});

it('should not report the initial state if the target is unobserved before it is delivered', () => {
Expand Down Expand Up @@ -1497,19 +1488,16 @@ describe('IntersectionObserver', () => {

const node = ensureReactNativeElement(maybeNode);

Fantom.runTask(() => {
observer1 = new IntersectionObserver(() => {});
observer2 = new IntersectionObserver(() => {});
observer1 = new IntersectionObserver(() => {});
observer2 = new IntersectionObserver(() => {});

observer1.observe(node);
observer2.observe(node);
observer1.observe(node);
observer2.observe(node);

observer1.unobserve(node);
observer1.unobserve(node);

// The second call shouldn't log errors (that would make the test fail).
observer2.unobserve(node);
throw new Error('unobserve should not throw');
});
// The second call shouldn't log errors (that would make the test fail).
observer2.unobserve(node);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,14 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}>
<View key="node1-1" />
</View>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

const observerCallback = jest.fn();
const observer = new MutationObserver(observerCallback);
Expand Down Expand Up @@ -968,13 +968,13 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}
/>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

const observerCallback = jest.fn();
const observer = new MutationObserver(observerCallback);
Expand Down Expand Up @@ -1016,13 +1016,13 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}
/>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

const observerCallback = jest.fn();
const observer = new MutationObserver(observerCallback);
Expand All @@ -1048,13 +1048,13 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}
/>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

Fantom.runTask(() => {
root.render(<></>);
Expand Down
Loading