Skip to content

Commit

Permalink
refactor: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 31, 2024
1 parent 9d88e76 commit 5178d65
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/presets/netlify/legacy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function generateCatchAllRedirects(
if (!catchAllPath) return "";

return [
// e.g.: /_nuxt/* /_nuxt/:splat 200
// e.g.: /static/* /static/:splat 200
// Because of Netlify CDN shadowing
// (https://docs.netlify.com/routing/redirects/rewrites-proxies/#shadowing),
// this config avoids function invocations for all static paths, even 404s.
Expand Down
67 changes: 60 additions & 7 deletions test/presets/netlify-legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ import type { APIGatewayEvent, Handler } from "aws-lambda";
import destr from "destr";
import { resolve } from "pathe";
import { describe, expect, it } from "vitest";
import { generateCatchAllRedirects } from "../../src/presets/netlify/legacy/utils";
import { getPresetTmpDir, setupTest, testNitro } from "../tests";

describe("nitro:preset:netlify-legacy", async () => {
const ctx = await setupTest("netlify-legacy", {
compatibilityDate: "2024-01-01",
config: {
publicAssets: [
{
dir: "dist/_nuxt",
baseURL: "_nuxt",
},
],
output: {
publicDir: resolve(getPresetTmpDir("netlify-legacy"), "dist"),
},
Expand Down Expand Up @@ -76,7 +71,6 @@ describe("nitro:preset:netlify-legacy", async () => {
/rules/isr-ttl/* /.netlify/builders/server 200
/rules/isr/* /.netlify/builders/server 200
/rules/dynamic /.netlify/functions/server 200
/_nuxt/* /_nuxt/:splat 200
/build/* /build/:splat 200
/* /.netlify/functions/server 200"
`);
Expand Down Expand Up @@ -118,4 +112,63 @@ describe("nitro:preset:netlify-legacy", async () => {
});
}
);

describe("generateCatchAllRedirects", () => {
it("returns empty string if `catchAllPath` is not defined", () => {
expect(generateCatchAllRedirects([], undefined)).toEqual("");
});

it("includes a redirect from `/*` to `catchAllPath` if defined", () => {
expect(generateCatchAllRedirects([], "/catch-all")).toEqual(
"/* /catch-all 200"
);
});

it("includes a splat redirect for each non-fallthrough non-root public asset path, BEFORE the catch-all", () => {
const publicAssets = [
{
fallthrough: true,
baseURL: "with-fallthrough",
dir: "with-fallthrough-dir",
maxAge: 0,
},
{
fallthrough: true,
dir: "with-fallthrough-no-baseURL-dir",
maxAge: 0,
},
{
fallthrough: false,
dir: "no-fallthrough-no-baseURL-dir",
maxAge: 0,
},
{
fallthrough: false,
dir: "no-fallthrough-root-baseURL-dir",
baseURL: "/",
maxAge: 0,
},
{
baseURL: "with-default-fallthrough",
dir: "with-default-fallthrough-dir",
maxAge: 0,
},
{
fallthrough: false,
baseURL: "nested/no-fallthrough",
dir: "nested/no-fallthrough-dir",
maxAge: 0,
},
];
expect(
generateCatchAllRedirects(publicAssets, "/catch-all")
).toMatchInlineSnapshot(

Check failure on line 165 in test/presets/netlify-legacy.test.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

test/presets/netlify-legacy.test.ts > nitro:preset:netlify-legacy > generateCatchAllRedirects > includes a splat redirect for each non-fallthrough non-root public asset path, BEFORE the catch-all

Error: Snapshot `nitro:preset:netlify-legacy > generateCatchAllRedirects > includes a splat redirect for each non-fallthrough non-root public asset path, BEFORE the catch-all 1` mismatched - Expected + Received "/with-default-fallthrough/* /with-default-fallthrough/:splat 200 - /nested/no-fallthrough/* /nested/no-fallthrough/:splat 200 + /nested/no-fallthrough/* /nested/no-fallthrough/:splat 200 - /* /catch-all 200" + /* /catch-all 200" ❯ test/presets/netlify-legacy.test.ts:165:9
`
"/with-default-fallthrough/* /with-default-fallthrough/:splat 200
/nested/no-fallthrough/* /nested/no-fallthrough/:splat 200
/* /catch-all 200"
`.trim()
);
});
});
});
61 changes: 0 additions & 61 deletions test/unit/netlify-legacy.utils.test.ts

This file was deleted.

0 comments on commit 5178d65

Please sign in to comment.