Skip to content

Commit a4c969e

Browse files
committed
make tests more reliable
1 parent 74d7000 commit a4c969e

File tree

26 files changed

+532
-39
lines changed

26 files changed

+532
-39
lines changed

exercises/03.complex/01.problem.iframe/app/routes/healthcheck.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import * as react from 'react'
55
import * as errorBoundary from 'react-error-boundary'
66
import * as misc from '#app/utils/misc.ts'
7+
import { type Route } from './+types/healthcheck.tsx'
8+
9+
export async function loader({ context }: Route.LoaderArgs) {
10+
await context.db.getEntries()
11+
return { ok: true }
12+
}
713

814
export default function Healthcheck() {
915
react.useEffect(() => {

exercises/03.complex/01.solution.iframe/app/routes/healthcheck.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import * as react from 'react'
55
import * as errorBoundary from 'react-error-boundary'
66
import * as misc from '#app/utils/misc.ts'
7+
import { type Route } from './+types/healthcheck.tsx'
8+
9+
export async function loader({ context }: Route.LoaderArgs) {
10+
await context.db.getEntries()
11+
return { ok: true }
12+
}
713

814
export default function Healthcheck() {
915
react.useEffect(() => {

exercises/03.complex/02.problem.ready/app/routes/healthcheck.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import * as react from 'react'
55
import * as errorBoundary from 'react-error-boundary'
66
import * as misc from '#app/utils/misc.ts'
7+
import { type Route } from './+types/healthcheck.tsx'
8+
9+
export async function loader({ context }: Route.LoaderArgs) {
10+
await context.db.getEntries()
11+
return { ok: true }
12+
}
713

814
export default function Healthcheck() {
915
react.useEffect(() => {

exercises/03.complex/02.problem.ready/test/index.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client'
33
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
4-
import { chromium } from 'playwright'
4+
import { chromium, type Page } from 'playwright'
55
import { test, expect, inject } from 'vitest'
66
import { z } from 'zod'
77

@@ -56,16 +56,16 @@ test('journal viewer sends ui-lifecycle-iframe-ready message', async () => {
5656
.object({ resource: z.object({}).passthrough() })
5757
.parse(result.content[0])
5858

59-
// pre-fetch because vite may need to optimize deps 🙃 https://x.com/kentcdodds/status/1972793943265038731
60-
await fetch(resource.text as string, { method: 'HEAD' })
61-
6259
const url = new URL('http://localhost:7787/mcp-ui-renderer')
6360
url.searchParams.set('resourceData', JSON.stringify(resource))
6461

6562
await using browserSetup = await setupBrowser()
6663
const { page } = browserSetup
6764

6865
await page.goto(url.toString())
66+
67+
await handleViteDeps(page)
68+
6969
await page
7070
.getByRole('log')
7171
.getByText('ui-lifecycle-iframe-ready')
@@ -77,3 +77,20 @@ test('journal viewer sends ui-lifecycle-iframe-ready message', async () => {
7777
)
7878
})
7979
})
80+
81+
// because vite needs to optimize deps 😭😡
82+
async function handleViteDeps(page: Page) {
83+
await page
84+
.frameLocator('iframe')
85+
.locator('vite-error-overlay')
86+
.waitFor({ timeout: 200 })
87+
.then(
88+
async () => {
89+
await page.reload()
90+
await new Promise((resolve) => setTimeout(resolve, 400))
91+
},
92+
() => {
93+
// good...
94+
},
95+
)
96+
}

exercises/03.complex/02.solution.ready/app/routes/healthcheck.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import * as react from 'react'
55
import * as errorBoundary from 'react-error-boundary'
66
import * as misc from '#app/utils/misc.ts'
7+
import { type Route } from './+types/healthcheck.tsx'
8+
9+
export async function loader({ context }: Route.LoaderArgs) {
10+
await context.db.getEntries()
11+
return { ok: true }
12+
}
713

814
export default function Healthcheck() {
915
react.useEffect(() => {

exercises/03.complex/02.solution.ready/test/index.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client'
33
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
4-
import { chromium } from 'playwright'
4+
import { chromium, type Page } from 'playwright'
55
import { test, expect, inject } from 'vitest'
66
import { z } from 'zod'
77

@@ -56,16 +56,16 @@ test('journal viewer sends ui-lifecycle-iframe-ready message', async () => {
5656
.object({ resource: z.object({}).passthrough() })
5757
.parse(result.content[0])
5858

59-
// pre-fetch because vite may need to optimize deps 🙃 https://x.com/kentcdodds/status/1972793943265038731
60-
await fetch(resource.text as string, { method: 'HEAD' })
61-
6259
const url = new URL('http://localhost:7787/mcp-ui-renderer')
6360
url.searchParams.set('resourceData', JSON.stringify(resource))
6461

6562
await using browserSetup = await setupBrowser()
6663
const { page } = browserSetup
6764

6865
await page.goto(url.toString())
66+
67+
await handleViteDeps(page)
68+
6969
await page
7070
.getByRole('log')
7171
.getByText('ui-lifecycle-iframe-ready')
@@ -77,3 +77,20 @@ test('journal viewer sends ui-lifecycle-iframe-ready message', async () => {
7777
)
7878
})
7979
})
80+
81+
// because vite needs to optimize deps 😭😡
82+
async function handleViteDeps(page: Page) {
83+
await page
84+
.frameLocator('iframe')
85+
.locator('vite-error-overlay')
86+
.waitFor({ timeout: 200 })
87+
.then(
88+
async () => {
89+
await page.reload()
90+
await new Promise((resolve) => setTimeout(resolve, 400))
91+
},
92+
() => {
93+
// good...
94+
},
95+
)
96+
}

exercises/03.complex/03.problem.sizing/app/routes/healthcheck.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import * as react from 'react'
55
import * as errorBoundary from 'react-error-boundary'
66
import * as misc from '#app/utils/misc.ts'
7+
import { type Route } from './+types/healthcheck.tsx'
8+
9+
export async function loader({ context }: Route.LoaderArgs) {
10+
await context.db.getEntries()
11+
return { ok: true }
12+
}
713

814
export default function Healthcheck() {
915
react.useEffect(() => {

exercises/03.complex/03.solution.sizing/app/routes/healthcheck.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import * as react from 'react'
55
import * as errorBoundary from 'react-error-boundary'
66
import * as misc from '#app/utils/misc.ts'
7+
import { type Route } from './+types/healthcheck.tsx'
8+
9+
export async function loader({ context }: Route.LoaderArgs) {
10+
await context.db.getEntries()
11+
return { ok: true }
12+
}
713

814
export default function Healthcheck() {
915
react.useEffect(() => {

exercises/03.complex/04.problem.dynamic-sizing/app/routes/healthcheck.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import * as react from 'react'
55
import * as errorBoundary from 'react-error-boundary'
66
import * as misc from '#app/utils/misc.ts'
7+
import { type Route } from './+types/healthcheck.tsx'
8+
9+
export async function loader({ context }: Route.LoaderArgs) {
10+
await context.db.getEntries()
11+
return { ok: true }
12+
}
713

814
export default function Healthcheck() {
915
react.useEffect(() => {

exercises/03.complex/04.problem.dynamic-sizing/test/index.test.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { invariant } from '@epic-web/invariant'
22
import { Client } from '@modelcontextprotocol/sdk/client'
33
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
4-
import { chromium } from 'playwright'
4+
import { chromium, type Page } from 'playwright'
55
import { test, expect, inject } from 'vitest'
66
import { z } from 'zod'
77

@@ -56,15 +56,14 @@ test('journal viewer sends ui-size-change message', async () => {
5656
.object({ resource: z.object({}).passthrough() })
5757
.parse(result.content[0])
5858

59-
// pre-fetch because vite may need to optimize deps 🙃 https://x.com/kentcdodds/status/1972793943265038731
60-
await fetch(resource.text as string, { method: 'HEAD' })
61-
6259
const url = new URL('http://localhost:7787/mcp-ui-renderer')
6360
url.searchParams.set('resourceData', JSON.stringify(resource))
6461

6562
await using browserSetup = await setupBrowser()
6663
const { page } = browserSetup
6764

65+
await handleViteDeps(page)
66+
6867
await page.goto(url.toString())
6968
const message = page.getByRole('log').getByText('ui-size-change')
7069
await message.waitFor({ timeout: 1000 }).catch((e) => {
@@ -74,9 +73,10 @@ test('journal viewer sends ui-size-change message', async () => {
7473
)
7574
})
7675

77-
const textContent = JSON.parse(await message.textContent())
76+
const textContent = await message.textContent()
77+
const messageContent = JSON.parse(textContent!)
7878
expect(
79-
textContent,
79+
messageContent,
8080
'🚨 the ui-size-change message is not the correct format',
8181
).toEqual({
8282
type: 'ui-size-change',
@@ -86,3 +86,20 @@ test('journal viewer sends ui-size-change message', async () => {
8686
},
8787
})
8888
})
89+
90+
// because vite needs to optimize deps 😭😡
91+
async function handleViteDeps(page: Page) {
92+
await page
93+
.frameLocator('iframe')
94+
.locator('vite-error-overlay')
95+
.waitFor({ timeout: 200 })
96+
.then(
97+
async () => {
98+
await page.reload()
99+
await new Promise((resolve) => setTimeout(resolve, 400))
100+
},
101+
() => {
102+
// good...
103+
},
104+
)
105+
}

0 commit comments

Comments
 (0)