@next/playwright 16.2.0-canary.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # @next/playwright
2
+
3
+ > - **Status:** Experimental. This API is not yet stable.
4
+ > - **Requires:** [Cache Components](https://nextjs.org/docs) to be enabled.
5
+
6
+ Playwright helpers for testing Next.js applications.
7
+
8
+ ## Instant Navigation Testing
9
+
10
+ An **instant navigation** commits immediately without waiting for data fetching.
11
+ The cached shell — including any Suspense loading boundaries — renders right
12
+ away, and dynamic data streams in afterward. The shell is the instant part, not
13
+ the full page.
14
+
15
+ `instant()` lets you test whether a route achieves this. While the callback is
16
+ active, navigations render only cached and prefetched content. Dynamic data is
17
+ deferred until the callback returns. This lets you make deterministic assertions
18
+ against the shell without race conditions.
19
+
20
+ The tool assumes a warm cache: all prefetches have completed and all cacheable
21
+ data is available. This way you're testing whether the route is *structured
22
+ correctly* for instant navigation, independent of network timing. If content you
23
+ expected to be cached is missing inside the callback, it points to a problem — a
24
+ missing `use cache` directive, a misplaced Suspense boundary, or a similar gap.
25
+
26
+ ### Examples
27
+
28
+ **Loading shell appears instantly** (dynamic content behind a Suspense boundary):
29
+
30
+ ```ts
31
+ import { instant } from '@next/playwright'
32
+
33
+ test('shows loading shell during navigation', async ({ page }) => {
34
+ await page.goto('/')
35
+
36
+ await instant(page, async () => {
37
+ await page.click('a[href="/dashboard"]')
38
+
39
+ // The loading shell is visible — dynamic data is deferred
40
+ await expect(page.locator('[data-testid="loading"]')).toBeVisible()
41
+ })
42
+
43
+ // After instant() returns, dynamic data streams in normally
44
+ await expect(page.locator('[data-testid="content"]')).toBeVisible()
45
+ })
46
+ ```
47
+
48
+ **Fully instant navigation** (all content is cached):
49
+
50
+ ```ts
51
+ test('navigates to profile instantly', async ({ page }) => {
52
+ await page.goto('/')
53
+
54
+ await instant(page, async () => {
55
+ await page.click('a[href="/profile"]')
56
+
57
+ // All content renders immediately
58
+ await expect(page.locator('[data-testid="profile-name"]')).toBeVisible()
59
+ await expect(page.locator('[data-testid="profile-bio"]')).toBeVisible()
60
+ })
61
+ })
62
+ ```
63
+
64
+ ### Enabling in production builds
65
+
66
+ In development (`next dev`), the testing API is available by default. In
67
+ production builds, it is disabled unless you explicitly opt in:
68
+
69
+ ```js
70
+ // next.config.js
71
+ module.exports = {
72
+ experimental: {
73
+ exposeTestingApiInProductionBuild: true,
74
+ },
75
+ }
76
+ ```
77
+
78
+ This is not meant to be deployed to live production sites. Only enable it in
79
+ controlled testing environments like preview deployments or CI.
80
+
81
+ ## How it works
82
+
83
+ `instant()` sets a cookie that tells Next.js to serve only cached data during
84
+ navigations. While the cookie is active:
85
+
86
+ - **Client-side navigations**: The router renders only what is available in the
87
+ prefetch cache. Dynamic data is deferred until the cookie is cleared.
88
+ - **Server renders (initial load, reload, MPA navigation)**: The server responds
89
+ with only the static shell, without any per-request dynamic data.
90
+
91
+ When the callback completes, the cookie is cleared and normal behavior resumes.
92
+
93
+ ## Design
94
+
95
+ The layering between this package and Next.js is intentionally very thin. This
96
+ serves as a reference implementation that other testing frameworks and dev tools
97
+ can replicate with minimal effort. The entire mechanism is a single cookie:
98
+
99
+ ```ts
100
+ // Set the cookie to enter instant mode
101
+ document.cookie = 'next-instant-navigation-testing=1; path=/'
102
+
103
+ // ... run assertions ...
104
+
105
+ // Clear the cookie to resume normal behavior
106
+ document.cookie = 'next-instant-navigation-testing=; path=/; max-age=0'
107
+ ```
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Minimal interfaces for Playwright's Page and BrowserContext. We use
3
+ * structural types rather than importing from a specific Playwright package
4
+ * so this works with any version of playwright, playwright-core, or
5
+ * @playwright/test.
6
+ */
7
+ interface PlaywrightBrowserContext {
8
+ addCookies(cookies: Array<{
9
+ name: string;
10
+ value: string;
11
+ url?: string;
12
+ domain?: string;
13
+ path?: string;
14
+ }>): Promise<void>;
15
+ cookies(): Promise<Array<{
16
+ name: string;
17
+ value: string;
18
+ }>>;
19
+ clearCookies(options?: {
20
+ name?: string;
21
+ domain?: string;
22
+ path?: string;
23
+ }): Promise<void>;
24
+ }
25
+ interface PlaywrightPage {
26
+ url(): string;
27
+ context(): PlaywrightBrowserContext;
28
+ }
29
+ /**
30
+ * Runs a function with instant navigation enabled. Within this scope,
31
+ * navigations render the prefetched UI immediately and wait for the
32
+ * callback to complete before streaming in dynamic data.
33
+ *
34
+ * Uses the cookie-based protocol: setting the cookie acquires the
35
+ * navigation lock (via CookieStore change event), and clearing it
36
+ * releases the lock.
37
+ *
38
+ * If the page is already loaded, the URL is inferred
39
+ * automatically. For a fresh page (before any navigation), pass
40
+ * `baseURL` so the cookie can be scoped to the correct domain:
41
+ *
42
+ * await instant(page, async () => {
43
+ * await page.goto(url)
44
+ * // ...
45
+ * }, { baseURL: 'http://localhost:3000' })
46
+ *
47
+ * When `@playwright/test` is installed, acquire/release actions appear
48
+ * as labeled steps in the Playwright UI.
49
+ */
50
+ export declare function instant<T>(page: PlaywrightPage, fn: () => Promise<T>, options?: {
51
+ baseURL?: string;
52
+ }): Promise<T>;
53
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.instant = instant;
4
+ const step_1 = require("./step");
5
+ const INSTANT_COOKIE = 'next-instant-navigation-testing';
6
+ /**
7
+ * Runs a function with instant navigation enabled. Within this scope,
8
+ * navigations render the prefetched UI immediately and wait for the
9
+ * callback to complete before streaming in dynamic data.
10
+ *
11
+ * Uses the cookie-based protocol: setting the cookie acquires the
12
+ * navigation lock (via CookieStore change event), and clearing it
13
+ * releases the lock.
14
+ *
15
+ * If the page is already loaded, the URL is inferred
16
+ * automatically. For a fresh page (before any navigation), pass
17
+ * `baseURL` so the cookie can be scoped to the correct domain:
18
+ *
19
+ * await instant(page, async () => {
20
+ * await page.goto(url)
21
+ * // ...
22
+ * }, { baseURL: 'http://localhost:3000' })
23
+ *
24
+ * When `@playwright/test` is installed, acquire/release actions appear
25
+ * as labeled steps in the Playwright UI.
26
+ */
27
+ async function instant(page, fn, options) {
28
+ // Check for nested instant() calls. The cookie is scoped to the browser
29
+ // context, so we can detect nesting by checking if it's already set.
30
+ const existingCookies = await page.context().cookies();
31
+ if (existingCookies.some((c) => c.name === INSTANT_COOKIE)) {
32
+ throw new Error('An instant() scope is already active. Nesting instant() ' +
33
+ 'calls is not supported. Did you forget to await the ' +
34
+ 'previous instant() call?');
35
+ }
36
+ // Acquire the lock by setting the cookie via the browser context. This
37
+ // ensures the cookie is present even on the very first navigation.
38
+ // The cookie triggers the CookieStore change event in
39
+ // navigation-testing-lock.ts, which acquires the in-memory navigation lock.
40
+ const { hostname } = new URL(resolveURL(page, options));
41
+ await (0, step_1.step)('Acquire Instant Lock', () => page
42
+ .context()
43
+ .addCookies([
44
+ { name: INSTANT_COOKIE, value: '[0]', domain: hostname, path: '/' },
45
+ ]));
46
+ try {
47
+ return await fn();
48
+ }
49
+ finally {
50
+ // Release the lock by clearing the cookie. Next.js may have updated the
51
+ // cookie value (e.g. from [0] to [1,null]) during the lock scope. We
52
+ // clear by name to remove the cookie regardless of its current value or
53
+ // which domain variant it was stored under.
54
+ await (0, step_1.step)('Release Instant Lock', () => page.context().clearCookies({ name: INSTANT_COOKIE }));
55
+ }
56
+ }
57
+ /**
58
+ * Resolves the URL to scope the instant navigation cookie to. Prefers
59
+ * an explicit `baseURL` option, then falls back to the page's current URL.
60
+ * Throws a descriptive error if neither is available (e.g. fresh page
61
+ * before any navigation).
62
+ */
63
+ function resolveURL(page, options) {
64
+ var _a;
65
+ const url = (_a = options === null || options === void 0 ? void 0 : options.baseURL) !== null && _a !== void 0 ? _a : page.url();
66
+ if (url && url !== 'about:blank') {
67
+ return url;
68
+ }
69
+ const error = new Error(`Could not infer the base URL of the application.
70
+
71
+ instant() needs to know the base URL so it can configure the
72
+ browser before the first page load. If the page is already
73
+ loaded, the base URL is detected automatically.
74
+ Otherwise, you can fix this in one of two ways:
75
+
76
+ 1. Pass a baseURL option:
77
+
78
+ await instant(page, async () => {
79
+ await page.goto('http://localhost:3000')
80
+ // ...
81
+ }, { baseURL: 'http://localhost:3000' })
82
+
83
+ Tip: If you use baseURL in your Playwright config, you can
84
+ get it from the test fixture:
85
+
86
+ test('my test', async ({ page, baseURL }) => {
87
+ await instant(page, async () => {
88
+ // ...
89
+ }, { baseURL })
90
+ })
91
+
92
+ 2. Navigate to a page before calling instant():
93
+
94
+ await page.goto('http://localhost:3000')
95
+ await instant(page, async () => {
96
+ // ...
97
+ })`);
98
+ // Remove resolveURL and instant from the stack trace so the error
99
+ // points at the caller's code.
100
+ Error.captureStackTrace(error, instant);
101
+ throw error;
102
+ }
package/dist/step.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export type Step = <T>(title: string, body: () => Promise<T>) => Promise<T>;
2
+ /**
3
+ * When `@playwright/test` is installed and we're running inside the Playwright
4
+ * test runner, wraps the body in a `test.step()` so it appears as a labeled
5
+ * step in the Playwright UI. Otherwise just executes the body directly.
6
+ */
7
+ declare let step: Step;
8
+ export { step };
package/dist/step.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.step = void 0;
5
+ /**
6
+ * When `@playwright/test` is installed and we're running inside the Playwright
7
+ * test runner, wraps the body in a `test.step()` so it appears as a labeled
8
+ * step in the Playwright UI. Otherwise just executes the body directly.
9
+ */
10
+ let step = (_title, body) => body();
11
+ exports.step = step;
12
+ try {
13
+ const pw = require('@playwright/test');
14
+ if (typeof ((_a = pw.test) === null || _a === void 0 ? void 0 : _a.step) === 'function') {
15
+ const playwrightStep = pw.test.step;
16
+ exports.step = step = async (title, body) => {
17
+ try {
18
+ return await playwrightStep(title, body);
19
+ }
20
+ catch (e) {
21
+ // If test.step fails because we're not inside the Playwright test
22
+ // runner (e.g., running under Jest), fall back to executing the body
23
+ // directly without step labels.
24
+ if (e instanceof Error &&
25
+ e.message.includes('can only be called from a test')) {
26
+ return body();
27
+ }
28
+ throw e;
29
+ }
30
+ };
31
+ }
32
+ }
33
+ catch { }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@next/playwright",
3
+ "version": "16.2.0-canary.100",
4
+ "repository": {
5
+ "url": "vercel/next.js",
6
+ "directory": "packages/next-playwright"
7
+ },
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "license": "MIT",
14
+ "scripts": {
15
+ "build": "node ../../scripts/rm.mjs dist && tsc -d -p tsconfig.json",
16
+ "prepublishOnly": "cd ../../ && turbo run build",
17
+ "dev": "tsc -d -w -p tsconfig.json",
18
+ "typescript": "tsec --noEmit -p tsconfig.json"
19
+ },
20
+ "peerDependencies": {
21
+ "@playwright/test": ">=1.0.0"
22
+ },
23
+ "peerDependenciesMeta": {
24
+ "@playwright/test": {
25
+ "optional": true
26
+ }
27
+ },
28
+ "devDependencies": {
29
+ "typescript": "5.9.2"
30
+ }
31
+ }