Repository: sveltejs/kit
Pull request #6975 (2022-09-22 18:02:33)

[chore] fix flaky test
It looks like the test is still flaking out. I think it's because you could get the response, check the value, and then the value is updated. Writing it this way avoids that possibility

Rich-Harris: gonna revert to https://github.com/sveltejs/kit/pull/6975/commits/6d780b4d2d7a0ec4b496cd15590da8868ca3213c for now

Patch:
diff --git a/.changeset/silver-badgers-travel.md b/.changeset/silver-badgers-travel.md
new file mode 100644
index 000000000..ec3b7c4bc
--- /dev/null
+++ b/.changeset/silver-badgers-travel.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+[fix] unwrap promises for load function data on the client
diff --git a/packages/kit/src/runtime/client/ambient.d.ts b/packages/kit/src/runtime/client/ambient.d.ts
index b9240974d..f4698f2d8 100644
--- a/packages/kit/src/runtime/client/ambient.d.ts
+++ b/packages/kit/src/runtime/client/ambient.d.ts
@@ -24,3 +24,7 @@ declare module '__GENERATED__/client-manifest.js' {
 
 	export const hooks: ClientHooks;
 }
+
+declare module '__GENERATED__/root.svelte' {
+	export { SvelteComponent as default } from 'svelte';
+}
diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js
index 95591dd08..5066aed0c 100644
--- a/packages/kit/src/runtime/client/client.js
+++ b/packages/kit/src/runtime/client/client.js
@@ -9,6 +9,7 @@ import { nodes, server_loads, dictionary, matchers, hooks } from '__GENERATED__/
 import { HttpError, Redirect } from '../control.js';
 import { stores } from './singletons.js';
 import { DATA_SUFFIX } from '../../constants.js';
+import { unwrap_promises } from '../../utils/promises.js';
 
 const SCROLL_KEY = 'sveltekit:scroll';
 const INDEX_KEY = 'sveltekit:index';
@@ -608,6 +609,7 @@ export function create_client({ target, base, trailing_slash }) {
 			} else {
 				data = (await node.shared.load.call(null, load_input)) ?? null;
 			}
+			data = data ? await unwrap_promises(data) : null;
 		}
 
 		return {
diff --git a/packages/kit/src/runtime/server/page/load_data.js b/packages/kit/src/runtime/server/page/load_data.js
index 21a753e50..4849956b7 100644
--- a/packages/kit/src/runtime/server/page/load_data.js
+++ b/packages/kit/src/runtime/server/page/load_data.js
@@ -1,5 +1,5 @@
 import { disable_search, make_trackable } from '../../../utils/url.js';
-
+import { unwrap_promises } from '../../../utils/promises.js';
 /**
  * Calls the user's `load` function.
  * @param {{
@@ -110,15 +110,3 @@ export async function load_data({ event, fetcher, node, parent, server_data_prom
 
 	return data ? unwrap_promises(data) : null;
 }
-
-/** @param {Record<string, any>} object */
-async function unwrap_promises(object) {
-	/** @type {Record<string, any>} */
-	const unwrapped = {};
-
-	for (const key in object) {
-		unwrapped[key] = await object[key];
-	}
-
-	return unwrapped;
-}
diff --git a/packages/kit/src/utils/promises.js b/packages/kit/src/utils/promises.js
new file mode 100644
index 000000000..ac2b53a60
--- /dev/null
+++ b/packages/kit/src/utils/promises.js
@@ -0,0 +1,17 @@
+/**
+ * Given an object, return a new object where all top level values are awaited
+ *
+ * @param {Record<string, any>} object
+ * @returns {Promise<Record<string, any>>}
+ */
+export async function unwrap_promises(object) {
+	for (const key in object) {
+		if (typeof object[key]?.then === 'function') {
+			return Object.fromEntries(
+				await Promise.all(Object.entries(object).map(async ([key, value]) => [key, await value]))
+			);
+		}
+	}
+
+	return object;
+}


Test patch:
diff --git a/packages/kit/test/apps/basics/test/client.test.js b/packages/kit/test/apps/basics/test/client.test.js
index 9f8a5ed62..c350b161f 100644
--- a/packages/kit/test/apps/basics/test/client.test.js
+++ b/packages/kit/test/apps/basics/test/client.test.js
@@ -905,8 +905,10 @@ test('+server.js next to +page.svelte works', async ({ page }) => {
 	expect(await page.textContent('p')).toBe('Hi');
 
 	for (const method of ['GET', 'PUT', 'PATCH', 'POST', 'DELETE']) {
-		page.click(`button:has-text("${method}")`);
-		await page.waitForResponse('/routing/endpoint-next-to-page');
-		expect(await page.textContent('pre')).toBe(method);
+		await page.click(`button:has-text("${method}")`);
+		await page.waitForFunction(
+			(method) => document.querySelector('pre').textContent === method,
+			method
+		);
 	}
 });