Skip to content
Merged
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
49 changes: 28 additions & 21 deletions e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ files across them. The suite (13 tests on iOS, 12 on Android) takes about
across its devices. Remote devices fit the same shape: one pool entry per
device.

| Variable | Effect |
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `E2E_DEVICE` | Simulator or emulator name or UDID. Default: every booted device of the platform. |
| `E2E_DEVICES` | Comma-separated pool of names or UDIDs, for example `iPhone 17,iPhone 17 Pro`. One worker per device; the test files spread across them. Every device needs the app installed, or set `E2E_IOS_APP_PATH`. |
| Variable | Effect |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `E2E_DEVICE` | Simulator or emulator name or UDID. Default: every booted device of the platform. |
| `E2E_DEVICES` | Comma-separated pool of names or UDIDs, for example `iPhone 17,iPhone 17 Pro`. One worker per device; the test files spread across them. Every device needs the app installed, or set `E2E_IOS_APP_PATH`. |
| `E2E_IOS_APP_PATH` | `.app` bundle installed on every pooled device before its first test. Use the Xcode build product (`~/Library/Developer/Xcode/DerivedData/PagerViewExample-*/Build/Products/Release-iphonesimulator/PagerViewExample.app`), never a path inside a simulator's own container: installing replaces that container and the path disappears mid-run. |
| `E2E_ANDROID_APK_PATH` | `.apk` to install before the first test instead of using the installed build. |
| `E2E_ANDROID_APK_PATH` | `.apk` to install before the first test instead of using the installed build. |

## Layout

Expand All @@ -87,11 +87,11 @@ device.
- `tests/*.e2e.ts` hold one scenario per example screen.
- `tests/issues/*.e2e.ts` hold regression scenarios, tagged `regression` and
`issue-<number>` where a GitHub issue exists.
- `tests/support/test.ts` re-exports `test` and `expect` (import them from
there so the engine can change in one place) and exports
`freshAppBeforeEach()`, which every test file calls to relaunch the app
before each test (installing the `E2E_*_APP_PATH` build first when set).
- `tests/support/app.ts` opens an example from the home list and forces the
- `tests/support/test.ts` re-exports `test` (wrapped to track whether the
last test passed) and `expect` (import them from there so the engine can
change in one place) and exports `freshAppBeforeEach()`, which every test
file calls to install the `E2E_*_APP_PATH` build before each test when set.
- `tests/support/app.ts` opens an example through its link and forces the
layout direction.
- `tests/support/basic-pager.ts` holds the checks shared by the LTR, vertical,
and RTL basic pager scenarios.
Expand All @@ -102,21 +102,28 @@ device.
moves the finger left and reveals the next page in LTR.
- Node swipes fling. Use `screen.scrollUntilVisible` with a target that stays
on screen after any fling (the last item) instead of a middle one.
- `openExample` gives every test a freshly mounted example. While the app is
running in the requested direction and the previous test passed, it opens
`pagerview://example/<testID>?mount=<n>&reset`, which replaces the
navigation stack in place (no relaunch, no push animation) and dismisses the
keyboard; it then waits for the old `example-mount-<n>` marker to leave and
the new one to show. Otherwise, or when the new marker stays covered (an
alert or Android's spell-check popup left over from the last test), it
relaunches the app, forces the direction on the home list, and pushes the
example with the link. Module, native, and window state outside the
example's React tree survives a reset.
- The layout direction toggle persists across launches. `openExample` forces
the direction it is given, and every RTL describe block calls
`restoreLtrAfterEach()`.
the direction it is given and switches only when it changes, so keep RTL
tests together in their describe block. A run can leave the app in RTL.
- The engine neither installs nor launches anything on its own, so every
test file calls `freshAppBeforeEach()` right after its imports: it
relaunches the app with `device.openApp(APP_ID, { relaunch: true })`, so
every test starts fresh at the home list. A module-level hook in
`tests/support/test.ts` would attach only to the first file evaluated in a
realm, and `app.open()` or `app.restart()` resume a running app the
surface does not know about. State shared between tests must be state the
app persists (the layout direction toggle is).
test file calls `freshAppBeforeEach()` right after its imports. A
module-level hook in `tests/support/test.ts` would attach only to the first
file evaluated in a realm.
- With `E2E_*_APP_PATH` set, the hook runs `device.installApp()` before
every relaunch (~2 s each on a simulator) instead of tracking which device
every test (~2 s each on a simulator) instead of tracking which device
already has the build: an install replaces the binary and keeps its data,
so repeating it is correct across device pools and realms.
so repeating it is correct across device pools and realms. The install
stops the app, so `openExample` relaunches it.
- A connected physical iPhone joins the default device pool and fails with
`ENGINE_FAILURE` when it is locked or lacks the app. Pin `E2E_DEVICE` to a
simulator when a phone is plugged in.
Expand Down
2 changes: 0 additions & 2 deletions e2e/e2e.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export default {
workers,
timeout: 180_000,
assertionTimeout: 10_000,
// restoreLtrAfterEach relaunches twice; ~30 s on an Android emulator.
cleanupTimeout: 60_000,
// github() only acts on GitHub Actions: job summary plus one PR comment.
// Every ios.yml variant runs as job `ios-build`, so the key keeps their
// comments apart.
Expand Down
3 changes: 0 additions & 3 deletions e2e/tests/basic-pager.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { freshAppBeforeEach, test } from './support/test.ts';
import { restoreLtrAfterEach } from './support/app.ts';
import {
openBasicPager,
verifyControls,
Expand All @@ -24,8 +23,6 @@ test.describe('basic pager', { tags: ['basic-pager'] }, () => {
});

test.describe('in RTL', () => {
restoreLtrAfterEach();

test('pages horizontally with the reversed gesture', async ({
device,
app,
Expand Down
7 changes: 1 addition & 6 deletions e2e/tests/issues/issue-1050-back-gesture.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { freshAppBeforeEach, expect, test } from '../support/test.ts';
import type { App, Device, Screen } from '../support/test.ts';
import {
expectHome,
openExample,
restoreLtrAfterEach,
} from '../support/app.ts';
import { expectHome, openExample } from '../support/app.ts';
import type { LayoutDirection } from '../support/app.ts';

freshAppBeforeEach();
Expand Down Expand Up @@ -113,7 +109,6 @@ test.describe('back gesture over the pager', () => {
});

test.describe('in RTL', () => {
restoreLtrAfterEach();
describeBackGesture('rtl');
});
});
Expand Down
4 changes: 1 addition & 3 deletions e2e/tests/issues/issue-1154-page-change-focus.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { freshAppBeforeEach, expect, test } from '../support/test.ts';
import type { App, Device, Screen } from '../support/test.ts';
import { openExample, restoreLtrAfterEach } from '../support/app.ts';
import { openExample } from '../support/app.ts';

freshAppBeforeEach();

Expand Down Expand Up @@ -293,8 +293,6 @@ test(
);

test.describe('right to left', () => {
restoreLtrAfterEach();

test(
'leaving a page with a swipe blurs its input',
{ ...options, platforms: ['ios'] },
Expand Down
6 changes: 1 addition & 5 deletions e2e/tests/set-page-animation.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { freshAppBeforeEach, expect, test } from './support/test.ts';
import type { App, Device, Screen } from './support/test.ts';
import { openExample, restoreLtrAfterEach } from './support/app.ts';
import { openExample } from './support/app.ts';
import type { LayoutDirection } from './support/app.ts';

freshAppBeforeEach();
Expand Down Expand Up @@ -35,10 +35,6 @@ function slideEvents(platform: string, page: number): string {

for (const direction of ['ltr', 'rtl'] as const) {
test.describe(direction, () => {
if (direction === 'rtl') {
restoreLtrAfterEach();
}

test(
`direct setPage selects only the target page (${direction})`,
options,
Expand Down
72 changes: 52 additions & 20 deletions e2e/tests/support/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from './test.ts';
import { appSession, expect } from './test.ts';
import type { App, Device, Screen } from './test.ts';
import { APP_ID } from '../../targets.ts';

export type LayoutDirection = 'ltr' | 'rtl';

Expand All @@ -8,6 +9,7 @@ const HOME_READY_TEST_ID = 'example-basic-horizontal';
// can keep the window off the accessibility tree for over 15 s.
const LAUNCH_TIMEOUT = 30_000;
const LINK_TIMEOUT = 10_000;
const RESET_TIMEOUT = 3_000;
// iOS asks "Open in “PagerViewExample”?" (localized, but always naming the
// app) the first time a simulator opens the scheme, and hides the app until
// it is answered. Matching the name keeps examples' own alerts, such as
Expand All @@ -27,14 +29,20 @@ export async function expectHome(screen: Screen): Promise<void> {
await expect(screen.getByTestId(HOME_READY_TEST_ID)).toBeVisible();
}

let mountCount = 0;

/**
* Opens an example by its home-list testID through the example app's
* `pagerview://example/<testID>` link, which pushes it over the home list as a
* tap on its row would, without scrolling the list to find the row. Examples
* without an explicit testID use their display name as the testID. The layout
* direction is forced first, LTR by default, because the toggle persists
* across launches and every gesture in the suite assumes the direction it
* asked for.
* `pagerview://example/<testID>` link, without scrolling the home list to find
* its row. Examples without an explicit testID use their display name as the
* testID. Every test gets a freshly mounted example in the layout direction it
* asked for, LTR by default, because the toggle persists across launches and
* every gesture in the suite assumes that direction.
*
* While the app is trusted and already in that direction, the link resets its
* stack in place: no relaunch and no push animation. Otherwise the app is
* relaunched, the direction forced on the home list, and the link pushes the
* example over it.
*/
export async function openExample(
device: Device,
Expand All @@ -43,8 +51,43 @@ export async function openExample(
testId: string,
direction: LayoutDirection = 'ltr'
): Promise<void> {
const mount = String(++mountCount);
const link = `pagerview://example/${encodeURIComponent(
testId
)}?mount=${mount}`;
const running = appSession.state;
appSession.state = undefined;

if (running?.direction === direction) {
await device.openLink(`${link}&reset`);
// The reset swaps the examples in one commit, so the old mount leaving
// the tree means the new one is in place.
await screen
.getByTestId(
running.mount === undefined
? HOME_LIST_TEST_ID
: `example-mount-${running.mount}`
)
.waitFor({ state: 'detached', timeout: LINK_TIMEOUT });
Comment thread
krozniata marked this conversation as resolved.
Resolved
// A window the last test left open survives the reset and covers the
// example: an alert, or Android's spell-check suggestions popup after a
// tap in a multiline input. Only a relaunch closes those reliably.
const shown = await screen
.getByTestId(`example-mount-${mount}`)
.waitFor({ timeout: RESET_TIMEOUT })
.then(
() => true,
() => false
);
if (shown) {
appSession.state = { direction, mount };
return;
}
}

await device.openApp(APP_ID, { relaunch: true });
await ensureLayoutDirection(app, screen, direction);
await device.openLink(`pagerview://example/${encodeURIComponent(testId)}`);
await device.openLink(link);

const prompt = screen.getByRole('alert', LINK_PROMPT_NAME);
const home = screen.getByTestId(HOME_READY_TEST_ID);
Expand All @@ -62,6 +105,7 @@ export async function openExample(
await screen
.getByTestId(HOME_LIST_TEST_ID)
.waitFor({ state: 'detached', timeout: LINK_TIMEOUT });
appSession.state = { direction, mount };
}

/**
Expand Down Expand Up @@ -92,15 +136,3 @@ export async function ensureLayoutDirection(
{ timeout: LAUNCH_TIMEOUT }
);
}

/**
* Registers an afterEach hook that hands the device back in LTR. Call it in
* every describe block that switches to RTL, because the direction persists
* across launches and the hook runs after failures too.
*/
export function restoreLtrAfterEach(): void {
test.afterEach(async ({ app, screen }) => {
await app.restart();
await ensureLayoutDirection(app, screen, 'ltr');
});
}
58 changes: 42 additions & 16 deletions e2e/tests/support/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { test } from '@e2e-dev/mobile';
import { APP_ID } from '../../targets.ts';
import { test as baseTest } from '@e2e-dev/mobile';

export { test };
export { expect } from 'e2e';
export type { App, Locator, Screen, ScrollDirection, TextMatch } from 'e2e';
export type { Device } from '@e2e-dev/mobile';
Expand All @@ -12,21 +10,49 @@
};

/**
* Registers a beforeEach that relaunches the app, so every test in the file
* starts fresh at the home list. Call it at the top of every test file: the
* engine neither installs nor launches anything on its own, a module-level
* hook in this shared file would only attach to the first file evaluated in
* a realm, and the explicit relaunch terminates first where app.open() or
* app.restart() only resume an app the surface does not know is running.
* With E2E_IOS_APP_PATH / E2E_ANDROID_APK_PATH set, that build is installed
* before every relaunch: the install replaces the binary and keeps its data,
* so it stays correct across device pools and realms without tracking what
* is already on which device. State survives the relaunch only when the app
* persists it (the layout direction toggle does).
* What this realm knows about the running app, while it can be trusted: the
* layout direction it launched with and the mount id of the example on
* screen (undefined when it shows home). Undefined means relaunch first.
*/
export const appSession: {
state: { direction: 'ltr' | 'rtl'; mount: string | undefined } | undefined;
lastTestPassed: boolean;
} = { state: undefined, lastTestPassed: false };

/**
* The engine's `test`, with every test body wrapped so a test that fails or
* never finishes (a timeout abandons the body without rejecting it) leaves
* the app untrusted for the next one: an alert, keyboard or modal it left up
* would survive an in-app reset.
*/
export const test: typeof baseTest = new Proxy(baseTest, {
apply(target, thisArg, args: unknown[]) {
const body = args.at(-1) as (fixtures: unknown) => Promise<void>;
args[args.length - 1] = async (fixtures: unknown) => {
if (!appSession.lastTestPassed) appSession.state = undefined;
appSession.lastTestPassed = false;
await body(fixtures);
appSession.lastTestPassed = true;
};
return Reflect.apply(target, thisArg, args);
},

Check warning on line 38 in e2e/tests/support/test.ts

View check run for this annotation

apex-sentinel-staging / Apex Sentinel

minor: A test.skip inside a body marks the next test's app as dirty

`skipWithoutContentPop` aborts the body with the engine's skip signal, so `await body(fixtures)` never resolves normally and `lastTestPassed` stays false. Every test following a skip then relaunches the app; on iOS without the content pop, all four issue-1050 tests take the slow path, cancelling the speed-up this PR targets on those devices. ### Also raised here Also raised at this location by persona `maintainability`: test wrapper assumes the last argument is the test body — The Proxy apply trap casts whatever is last in `args` to the test function and replaces it, so any declaration whose last argument is not a body — the body-less form vitest accepts for a todo/skipped scenario, or a call written as `test(name, options)` — gets a string or options object turned into an async function that is then called as `body(fixtures)`, and a todo silently becomes a failing implemented test. Add the pass/fail bookkeeping only when the last argument is a function, so the wrapper stays correct for every declaration form vitest supports.
Comment on lines +29 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor · A test.skip inside a body marks the next test's app as dirty

skipWithoutContentPop aborts the body with the engine's skip signal, so await body(fixtures) never resolves normally and lastTestPassed stays false. Every test following a skip then relaunches the app; on iOS without the content pop, all four issue-1050 tests take the slow path, cancelling the speed-up this PR targets on those devices.

Details

Also raised here

Also raised at this location by persona maintainability: test wrapper assumes the last argument is the test body — The Proxy apply trap casts whatever is last in args to the test function and replaces it, so any declaration whose last argument is not a body — the body-less form vitest accepts for a todo/skipped scenario, or a call written as test(name, options) — gets a string or options object turned into an async function that is then called as body(fixtures), and a todo silently becomes a failing implemented test. Add the pass/fail bookkeeping only when the last argument is a function, so the wrapper stays correct for every declaration form vitest supports.

persona: senior-developer, maintainability

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

});

/**
* Registers a beforeEach that installs the build when E2E_IOS_APP_PATH /
* E2E_ANDROID_APK_PATH is set. Call it at the top of every test file: the
* engine neither installs nor launches anything on its own, and a
* module-level hook in this shared file would only attach to the first file
* evaluated in a realm. The install replaces the binary and keeps its data, so
* it stays correct across device pools and realms without tracking what is
* already on which device; it also stops the app. Launching is left to
* openExample, which reuses a running app it can trust.
*/
export function freshAppBeforeEach(): void {
test.beforeEach(async ({ device, platform }) => {
if (APP_PATHS[platform]) await device.installApp();
await device.openApp(APP_ID, { relaunch: true });
if (APP_PATHS[platform]) {
appSession.state = undefined;
await device.installApp();
}
});
}
Loading
Loading