-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add future.v2_errorBoundary flag (#4918)
- Add ability for integration tests to specify future flags - Remove duplicate RemixErrorBoundary - Add v2_errorBoundary future flag Co-authored-by: Sergio Xalambrí <[email protected]> Co-authored-by: Mehdi Achour <[email protected]> Co-authored-by: Michaël De Boey <[email protected]>
- Loading branch information
1 parent
809f519
commit ef66307
Showing
22 changed files
with
415 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
"@remix-run/react": patch | ||
"@remix-run/server-runtime": patch | ||
--- | ||
|
||
Add `future.v2_errorBoundary` flag to opt-into v2 `ErrorBoundary` behavior. This removes the separate `CatchBoundary` and `ErrorBoundary` and consolidates them into a single `ErrorBoundary` following the logic used by `errorElement` in React Router. You can then use `isRouteErrorResponse` to differentiate between thrown `Response`/`Error` instances. | ||
|
||
```jsx | ||
// Current (Remix v1 default) | ||
import { useCatch } from "@remix-run/react"; | ||
|
||
export function CatchBoundary() { | ||
let caught = useCatch(); | ||
return <p>{caught.status} {caught.data}</p>; | ||
} | ||
|
||
export function ErrorBoundary({ error }) { | ||
return <p>{error.message}</p>; | ||
} | ||
|
||
|
||
// Using future.v2_errorBoundary | ||
import { isRouteErrorResponse, useRouteError } from "@remix-run/react"; | ||
|
||
export function ErrorBoundary() { | ||
let error = useRouteError(); | ||
|
||
return isRouteErrorResponse(error) ? | ||
<p>{error.status} {error.data}</p> : | ||
<p>{error.message}</p>; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
import type { Page } from "@playwright/test"; | ||
import { test, expect } from "@playwright/test"; | ||
import { ServerMode } from "@remix-run/server-runtime/mode"; | ||
|
||
import { createAppFixture, createFixture, js } from "./helpers/create-fixture"; | ||
import type { Fixture, AppFixture } from "./helpers/create-fixture"; | ||
import { PlaywrightFixture } from "./helpers/playwright-fixture"; | ||
|
||
test.describe("V2 Singular ErrorBoundary (future.v2_errorBoundary)", () => { | ||
let fixture: Fixture; | ||
let appFixture: AppFixture; | ||
let oldConsoleError: () => void; | ||
|
||
test.beforeAll(async () => { | ||
fixture = await createFixture({ | ||
future: { | ||
v2_errorBoundary: true, | ||
}, | ||
files: { | ||
"app/root.jsx": js` | ||
import { Links, Meta, Outlet, Scripts } from "@remix-run/react"; | ||
export default function Root() { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<Meta /> | ||
<Links /> | ||
</head> | ||
<body> | ||
<main> | ||
<Outlet /> | ||
</main> | ||
<Scripts /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
`, | ||
|
||
"app/routes/parent.jsx": js` | ||
import { | ||
Link, | ||
Outlet, | ||
isRouteErrorResponse, | ||
useLoaderData, | ||
useRouteError, | ||
} from "@remix-run/react"; | ||
export function loader() { | ||
return "PARENT LOADER"; | ||
} | ||
export default function Component() { | ||
return ( | ||
<div> | ||
<nav> | ||
<ul> | ||
<li><Link to="/parent/child-with-boundary">Link</Link></li> | ||
<li><Link to="/parent/child-with-boundary?type=error">Link</Link></li> | ||
<li><Link to="/parent/child-with-boundary?type=response">Link</Link></li> | ||
<li><Link to="/parent/child-with-boundary?type=render">Link</Link></li> | ||
<li><Link to="/parent/child-without-boundary?type=error">Link</Link></li> | ||
<li><Link to="/parent/child-without-boundary?type=response">Link</Link></li> | ||
<li><Link to="/parent/child-without-boundary?type=render">Link</Link></li> | ||
</ul> | ||
</nav> | ||
<p id="parent-data">{useLoaderData()}</p> | ||
<Outlet /> | ||
</div> | ||
) | ||
} | ||
export function ErrorBoundary() { | ||
let error = useRouteError(); | ||
return isRouteErrorResponse(error) ? | ||
<p id="parent-error-response">{error.status + ' ' + error.data}</p> : | ||
<p id="parent-error">{error.message}</p>; | ||
} | ||
`, | ||
|
||
"app/routes/parent/child-with-boundary.jsx": js` | ||
import { | ||
isRouteErrorResponse, | ||
useLoaderData, | ||
useLocation, | ||
useRouteError, | ||
} from "@remix-run/react"; | ||
export function loader({ request }) { | ||
let errorType = new URL(request.url).searchParams.get('type'); | ||
if (errorType === 'response') { | ||
throw new Response('Loader Response', { status: 418 }); | ||
} else if (errorType === 'error') { | ||
throw new Error('Loader Error'); | ||
} | ||
return "CHILD LOADER"; | ||
} | ||
export default function Component() {; | ||
let data = useLoaderData(); | ||
if (new URLSearchParams(useLocation().search).get('type') === "render") { | ||
throw new Error("Render Error"); | ||
} | ||
return <p id="child-data">{data}</p>; | ||
} | ||
export function ErrorBoundary() { | ||
let error = useRouteError(); | ||
return isRouteErrorResponse(error) ? | ||
<p id="child-error-response">{error.status + ' ' + error.data}</p> : | ||
<p id="child-error">{error.message}</p>; | ||
} | ||
`, | ||
|
||
"app/routes/parent/child-without-boundary.jsx": js` | ||
import { useLoaderData, useLocation } from "@remix-run/react"; | ||
export function loader({ request }) { | ||
let errorType = new URL(request.url).searchParams.get('type'); | ||
if (errorType === 'response') { | ||
throw new Response('Loader Response', { status: 418 }); | ||
} else if (errorType === 'error') { | ||
throw new Error('Loader Error'); | ||
} | ||
return "CHILD LOADER"; | ||
} | ||
export default function Component() {; | ||
let data = useLoaderData(); | ||
if (new URLSearchParams(useLocation().search).get('type') === "render") { | ||
throw new Error("Render Error"); | ||
} | ||
return <p id="child-data">{data}</p>; | ||
} | ||
`, | ||
}, | ||
}); | ||
|
||
appFixture = await createAppFixture(fixture, ServerMode.Development); | ||
}); | ||
|
||
test.afterAll(() => { | ||
appFixture.close(); | ||
}); | ||
|
||
test.beforeEach(({ page }) => { | ||
oldConsoleError = console.error; | ||
console.error = () => {}; | ||
}); | ||
|
||
test.afterEach(() => { | ||
console.error = oldConsoleError; | ||
}); | ||
|
||
test.describe("without JavaScript", () => { | ||
test.use({ javaScriptEnabled: false }); | ||
runBoundaryTests(); | ||
}); | ||
|
||
test.describe("with JavaScript", () => { | ||
test.use({ javaScriptEnabled: true }); | ||
runBoundaryTests(); | ||
}); | ||
|
||
function runBoundaryTests() { | ||
// Shorthand util to wait for an element to appear before asserting it | ||
async function waitForAndAssert( | ||
page: Page, | ||
app: PlaywrightFixture, | ||
selector: string, | ||
match: string | ||
) { | ||
await page.waitForSelector(selector); | ||
expect(await app.getHtml(selector)).toMatch(match); | ||
} | ||
|
||
test("No errors", async ({ page }) => { | ||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/parent"); | ||
await app.clickLink("/parent/child-with-boundary"); | ||
await waitForAndAssert(page, app, "#child-data", "CHILD LOADER"); | ||
}); | ||
|
||
test("Throwing a Response to own boundary", async ({ page }) => { | ||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/parent"); | ||
await app.clickLink("/parent/child-with-boundary?type=response"); | ||
await waitForAndAssert( | ||
page, | ||
app, | ||
"#child-error-response", | ||
"418 Loader Response" | ||
); | ||
}); | ||
|
||
test("Throwing an Error to own boundary", async ({ page }) => { | ||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/parent"); | ||
await app.clickLink("/parent/child-with-boundary?type=error"); | ||
await waitForAndAssert(page, app, "#child-error", "Loader Error"); | ||
}); | ||
|
||
test("Throwing a render error to own boundary", async ({ page }) => { | ||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/parent"); | ||
await app.clickLink("/parent/child-with-boundary?type=render"); | ||
await waitForAndAssert(page, app, "#child-error", "Render Error"); | ||
}); | ||
|
||
test("Throwing a Response to parent boundary", async ({ page }) => { | ||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/parent"); | ||
await app.clickLink("/parent/child-without-boundary?type=response"); | ||
await waitForAndAssert( | ||
page, | ||
app, | ||
"#parent-error-response", | ||
"418 Loader Response" | ||
); | ||
}); | ||
|
||
test("Throwing an Error to parent boundary", async ({ page }) => { | ||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/parent"); | ||
await app.clickLink("/parent/child-without-boundary?type=error"); | ||
await waitForAndAssert(page, app, "#parent-error", "Loader Error"); | ||
}); | ||
|
||
test("Throwing a render error to parent boundary", async ({ page }) => { | ||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/parent"); | ||
await app.clickLink("/parent/child-without-boundary?type=render"); | ||
await waitForAndAssert(page, app, "#parent-error", "Render Error"); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.