diff --git a/.github/actions/run-fantom-tests/action.yml b/.github/actions/run-fantom-tests/action.yml index 68fc0adc8200..95192ade7865 100644 --- a/.github/actions/run-fantom-tests/action.yml +++ b/.github/actions/run-fantom-tests/action.yml @@ -27,7 +27,7 @@ runs: run: | for attempt in 1 2 3; do echo "Attempt $attempt of 3" - if yarn fantom; then + if yarn fantom --ci; then exit 0 fi if [ "$attempt" -lt 3 ]; then diff --git a/packages/react-native/Libraries/LogBox/UI/__tests__/LogBoxInspectorHeader-itest.js b/packages/react-native/Libraries/LogBox/UI/__tests__/LogBoxInspectorHeader-itest.js index 17d10850fd2b..7b95ae6b0370 100644 --- a/packages/react-native/Libraries/LogBox/UI/__tests__/LogBoxInspectorHeader-itest.js +++ b/packages/react-native/Libraries/LogBox/UI/__tests__/LogBoxInspectorHeader-itest.js @@ -12,6 +12,7 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; import LogBoxInspectorHeader from '../LogBoxInspectorHeader'; import * as Fantom from '@react-native/fantom'; +import nullthrows from 'nullthrows'; import * as React from 'react'; describe('LogBoxInspectorHeader', () => { @@ -49,18 +50,29 @@ describe('LogBoxInspectorHeader', () => { it('should render two buttons for three or more total', () => { const root = Fantom.createRoot(); + const onSelectIndex = jest.fn(); Fantom.runTask(() => { root.render( {}} + onSelectIndex={onSelectIndex} selectedIndex={0} - total={1} + total={3} level="warn" />, ); }); expect(root.getRenderedOutput().toJSX()).toMatchSnapshot(); + + Fantom.dispatchNativeEvent( + nullthrows(root.document.getElementById('logbox_header_button_prev')), + 'click', + ); + Fantom.dispatchNativeEvent( + nullthrows(root.document.getElementById('logbox_header_button_next')), + 'click', + ); + expect(onSelectIndex.mock.calls).toEqual([[2], [1]]); }); it('should render syntax error header', () => { diff --git a/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspector-itest.js.snap b/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspector-itest.js.snap index f1fe5f55a826..a59895fb00fe 100644 --- a/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspector-itest.js.snap +++ b/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspector-itest.js.snap @@ -8,67 +8,69 @@ exports[`LogBoxInspector should render fatal with selectedIndex 2 1`] = ` > - - - - - Log 3 of 3 - - - + + + + > + Log 3 of 3 + + + + - - Dismiss - + + Dismiss + + - - Minimize - + + Minimize + + - - Copy - + + Copy + + @@ -197,67 +214,69 @@ exports[`LogBoxInspector should render warning with selectedIndex 0 1`] = ` > - - - - - Log 1 of 3 - - - + + + + > + Log 1 of 3 + + + + - - Dismiss - + + Dismiss + + - - Minimize - + + Minimize + + - - Copy - + + Copy + + diff --git a/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorFooter-itest.js.snap b/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorFooter-itest.js.snap index e9dfe66c817d..3d47dcfdde25 100644 --- a/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorFooter-itest.js.snap +++ b/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorFooter-itest.js.snap @@ -32,58 +32,73 @@ exports[`LogBoxInspectorFooter should render two buttons for error 1`] = ` flexDirection="row" > - - Dismiss - + + Dismiss + + - - Minimize - + + Minimize + + - - Copy - + + Copy + + `; @@ -94,58 +109,73 @@ exports[`LogBoxInspectorFooter should render two buttons for fatal 1`] = ` flexDirection="row" > - - Dismiss - + + Dismiss + + - - Minimize - + + Minimize + + - - Copy - + + Copy + + `; @@ -156,58 +186,73 @@ exports[`LogBoxInspectorFooter should render two buttons for warning 1`] = ` flexDirection="row" > - - Dismiss - + + Dismiss + + - - Minimize - + + Minimize + + - - Copy - + + Copy + + `; diff --git a/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorHeader-itest.js.snap b/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorHeader-itest.js.snap index b6136c251f63..2f617c689a0d 100644 --- a/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorHeader-itest.js.snap +++ b/packages/react-native/Libraries/LogBox/UI/__tests__/__snapshots__/LogBoxInspectorHeader-itest.js.snap @@ -1,10 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`LogBoxInspectorHeader should render both buttons for two total 1`] = ` - - + - + `; exports[`LogBoxInspectorHeader should render no buttons for one total 1`] = ` - - + - + `; exports[`LogBoxInspectorHeader should render syntax error header 1`] = ` - - + Failed to compile - + `; exports[`LogBoxInspectorHeader should render two buttons for three or more total 1`] = ` - - + + > + + - Log 1 of 1 + Log 1 of 3 - + > + + + `; diff --git a/private/react-native-fantom/runner/__tests__/snapshotCI-test.js b/private/react-native-fantom/runner/__tests__/snapshotCI-test.js new file mode 100644 index 000000000000..9ff3703800bd --- /dev/null +++ b/private/react-native-fantom/runner/__tests__/snapshotCI-test.js @@ -0,0 +1,89 @@ +/** + * 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. + * + * @flow strict-local + * @format + */ + +import type {TestSnapshotResults} from '../../runtime/snapshotContext'; + +import {updateSnapshotsAndGetJestSnapshotResult} from '../snapshotUtils'; +import {SnapshotState} from 'jest-snapshot'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; + +function evaluate(state: SnapshotState, snapshotResults: TestSnapshotResults) { + return updateSnapshotsAndGetJestSnapshotResult(state, [snapshotResults]); +} + +describe('Fantom snapshot update modes', () => { + let directory: string; + + beforeEach(() => { + directory = fs.mkdtempSync(path.join(os.tmpdir(), 'fantom-snapshot-ci-')); + }); + + afterEach(() => { + fs.rmSync(directory, {recursive: true, force: true}); + }); + + it('preserves existing baselines in CI and default update modes', () => { + const file = path.join(directory, 'example.snap'); + const contents = + '// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`test 1`] = `committed baseline`;\n'; + for (const mode of ['none', 'new'] as const) { + fs.writeFileSync(file, contents); + const state = new SnapshotState(file, { + updateSnapshot: mode, + rootDir: directory, + snapshotFormat: {}, + }); + const result = evaluate(state, { + 'test 1': {pass: false, value: 'received value'}, + }); + + expect(fs.readFileSync(file, 'utf8')).toBe(contents); + expect(result.unmatched).toBe(1); + expect(result.updated).toBe(0); + } + }); + + it('does not create missing baselines in CI mode', () => { + const file = path.join(directory, 'missing.snap'); + const state = new SnapshotState(file, { + updateSnapshot: 'none', + rootDir: directory, + snapshotFormat: {}, + }); + const result = evaluate(state, { + 'test 1': {pass: false, value: 'received value'}, + }); + + expect(result.unmatched).toBe(1); + expect(result.added).toBe(0); + expect(fs.existsSync(file)).toBe(false); + }); + + it('allows explicit baseline updates outside CI', () => { + const file = path.join(directory, 'example.snap'); + fs.writeFileSync( + file, + '// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`test 1`] = `committed baseline`;\n', + ); + const state = new SnapshotState(file, { + updateSnapshot: 'all', + rootDir: directory, + snapshotFormat: {}, + }); + const result = evaluate(state, { + 'test 1': {pass: false, value: 'received value'}, + }); + + expect(result.updated).toBe(1); + expect(fs.readFileSync(file, 'utf8')).toContain('received value'); + }); +}); diff --git a/private/react-native-fantom/runner/snapshotUtils.js b/private/react-native-fantom/runner/snapshotUtils.js index c6bcc9ec5b5e..f44edbbe6540 100644 --- a/private/react-native-fantom/runner/snapshotUtils.js +++ b/private/react-native-fantom/runner/snapshotUtils.js @@ -320,6 +320,11 @@ export const updateSnapshotsAndGetJestSnapshotResult = ( continue; } + if (snapshotState._updateSnapshot !== 'all') { + snapshotState.unmatched++; + snapshotState._uncheckedKeys.delete(key); + continue; + } snapshotState._dirty = true; snapshotState._snapshotData[key] = addExtraLineBreaks(result.value); snapshotState.updated++;