test: use disposable tmp dirs (#2983)
Replaces the patterns below with consistent `AsyncDisposable` and `await
using` resource management for automatic cleanup.
- `await withTmpDir(async (dir) => { ... })` with `finally`
- Makes code a bit flatter, and reduces callback-hell a bit
- `await Deno.makeTempDir()` + `Deno.remove()`
- `await Deno.makeTempDir()`
Helps be more consistent in removing temporary folders. Among others,
ensures temp folders are removed in the tests below:
- `src/build_cache_test.ts`
- `src/dev/builder_test.ts`
- `src/dev/dev_build_cache_test.ts`
## TODO's
The `Deno.makeTempDir()` method is still used by `buildProd` as well,
but this method is used in the top module scope, where it does not work
correctly with `Deno.test` (it runs before the test methods). I removed
this part from the PR and leave this for later to avoid making the PR
too big.
-
https://github.com/csvn/fresh/blob/test%2Fdisposable-tmp-dirs/tests/test_utils.tsx/#L64
-
https://github.com/csvn/fresh/blob/test%2Fdisposable-tmp-dirs/www/main_test.ts/#L7
This commit is contained in:
+286
-296
@@ -6,17 +6,8 @@ import {
|
||||
updateProject,
|
||||
} from "./update.ts";
|
||||
import { expect } from "@std/expect";
|
||||
import { walk } from "https://deno.land/std@0.93.0/fs/walk.ts";
|
||||
|
||||
async function withTmpDir(fn: (dir: string) => void | Promise<void>) {
|
||||
const dir = await Deno.makeTempDir();
|
||||
|
||||
try {
|
||||
await fn(dir);
|
||||
} finally {
|
||||
await Deno.remove(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
import { walk } from "@std/fs/walk";
|
||||
import { withTmpDir } from "../../src/test_utils.ts";
|
||||
|
||||
async function writeFiles(dir: string, files: Record<string, string>) {
|
||||
const entries = Object.entries(files);
|
||||
@@ -49,77 +40,77 @@ async function readFiles(dir: string): Promise<Record<string, string>> {
|
||||
}
|
||||
|
||||
Deno.test("update - remove JSX pragma import", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { h, Fragment } from "preact";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { h, Fragment } from "preact";
|
||||
/** @jsx h */
|
||||
/** @jsxFrag Fragment */
|
||||
export default function Foo() {
|
||||
return null;
|
||||
}`,
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`export default function Foo() {
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`export default function Foo() {
|
||||
return null;
|
||||
}`);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("update - 1.x project deno.json", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(JSON.parse(files["/deno.json"]))
|
||||
.toEqual({
|
||||
imports: {
|
||||
"fresh": `jsr:@fresh/core@^${FRESH_VERSION}`,
|
||||
"@preact/signals": `npm:@preact/signals@^${PREACT_SIGNALS_VERSION}`,
|
||||
"preact": `npm:preact@^${PREACT_VERSION}`,
|
||||
},
|
||||
});
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(JSON.parse(files["/deno.json"]))
|
||||
.toEqual({
|
||||
imports: {
|
||||
"fresh": `jsr:@fresh/core@^${FRESH_VERSION}`,
|
||||
"@preact/signals": `npm:@preact/signals@^${PREACT_SIGNALS_VERSION}`,
|
||||
"preact": `npm:preact@^${PREACT_VERSION}`,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("update - 1.x project deno.json with imports", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{
|
||||
"imports": {
|
||||
"$fresh/": "foo"
|
||||
}
|
||||
}`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(JSON.parse(files["/deno.json"]))
|
||||
.toEqual({
|
||||
imports: {
|
||||
"fresh": `jsr:@fresh/core@^${FRESH_VERSION}`,
|
||||
"@preact/signals": `npm:@preact/signals@^${PREACT_SIGNALS_VERSION}`,
|
||||
"preact": `npm:preact@^${PREACT_VERSION}`,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(JSON.parse(files["/deno.json"]))
|
||||
.toEqual({
|
||||
imports: {
|
||||
"fresh": `jsr:@fresh/core@^${FRESH_VERSION}`,
|
||||
"@preact/signals": `npm:@preact/signals@^${PREACT_SIGNALS_VERSION}`,
|
||||
"preact": `npm:preact@^${PREACT_VERSION}`,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("update - 1.x project middlewares", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": "{}",
|
||||
"/routes/_middleware.ts":
|
||||
`import { FreshContext } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": "{}",
|
||||
"/routes/_middleware.ts": `import { FreshContext } from "$fresh/server.ts";
|
||||
|
||||
interface State {
|
||||
data: string;
|
||||
@@ -135,13 +126,13 @@ export async function handler(
|
||||
resp.headers.set("server", "fresh server");
|
||||
return resp;
|
||||
}`,
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/_middleware.ts"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
expect(files["/routes/_middleware.ts"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
|
||||
interface State {
|
||||
data: string;
|
||||
@@ -158,61 +149,61 @@ export async function handler(
|
||||
resp.headers.set("server", "fresh server");
|
||||
return resp;
|
||||
}`);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("update - 1.x project middlewares one arg", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": "{}",
|
||||
"/routes/_middleware.ts": `export async function handler(req: Request) {
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": "{}",
|
||||
"/routes/_middleware.ts": `export async function handler(req: Request) {
|
||||
return new Response("hello world from: " + req.url);
|
||||
}`,
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/_middleware.ts"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
expect(files["/routes/_middleware.ts"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
|
||||
export async function handler(ctx: FreshContext) {
|
||||
const req = ctx.req;
|
||||
|
||||
return new Response("hello world from: " + req.url);
|
||||
}`);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("update - 1.x update '$fresh/*' imports", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { PageProps } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { PageProps } from "$fresh/server.ts";
|
||||
export default function Foo(props: PageProps) {
|
||||
return null;
|
||||
}`,
|
||||
"/routes/foo.tsx": `import { asset, Head } from "$fresh/runtime.ts";`,
|
||||
});
|
||||
"/routes/foo.tsx": `import { asset, Head } from "$fresh/runtime.ts";`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { PageProps } from "fresh";
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { PageProps } from "fresh";
|
||||
export default function Foo(props: PageProps) {
|
||||
return null;
|
||||
}`);
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`import { asset, Head } from "fresh/runtime";`);
|
||||
});
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`import { asset, Head } from "fresh/runtime";`);
|
||||
});
|
||||
|
||||
Deno.test("update - 1.x update handler signature", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(req, ctx) {},
|
||||
@@ -221,7 +212,7 @@ export const handler: Handlers = {
|
||||
async PUT(req, ctx) {},
|
||||
async DELETE(req, ctx) {},
|
||||
};`,
|
||||
"/routes/foo.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
"/routes/foo.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_req, ctx) {},
|
||||
@@ -230,7 +221,7 @@ export const handler: Handlers = {
|
||||
async PUT(_req, ctx) {},
|
||||
async DELETE(_req, ctx) {},
|
||||
};`,
|
||||
"/routes/name.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
"/routes/name.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(request, ctx) {},
|
||||
@@ -239,7 +230,7 @@ export const handler: Handlers = {
|
||||
async PUT(request, ctx) {},
|
||||
async DELETE(request, ctx) {},
|
||||
};`,
|
||||
"/routes/name-unused.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
"/routes/name-unused.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_request, ctx) {},
|
||||
@@ -248,152 +239,152 @@ export const handler: Handlers = {
|
||||
async PUT(_request, ctx) {},
|
||||
async DELETE(_request, ctx) {},
|
||||
};`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async POST(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async PATCH(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async PUT(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async DELETE(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
};`);
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {},
|
||||
async POST(ctx) {},
|
||||
async PATCH(ctx) {},
|
||||
async PUT(ctx) {},
|
||||
async DELETE(ctx) {},
|
||||
};`);
|
||||
|
||||
expect(files["/routes/name.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async POST(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async PATCH(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async PUT(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async DELETE(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
};`);
|
||||
expect(files["/routes/name-unused.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {},
|
||||
async POST(ctx) {},
|
||||
async PATCH(ctx) {},
|
||||
async PUT(ctx) {},
|
||||
async DELETE(ctx) {},
|
||||
};`);
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async POST(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async PATCH(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async PUT(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
async DELETE(ctx) {
|
||||
const req = ctx.req;
|
||||
},
|
||||
};`);
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {},
|
||||
async POST(ctx) {},
|
||||
async PATCH(ctx) {},
|
||||
async PUT(ctx) {},
|
||||
async DELETE(ctx) {},
|
||||
};`);
|
||||
|
||||
expect(files["/routes/name.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async POST(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async PATCH(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async PUT(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
async DELETE(ctx) {
|
||||
const request = ctx.req;
|
||||
},
|
||||
};`);
|
||||
expect(files["/routes/name-unused.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {},
|
||||
async POST(ctx) {},
|
||||
async PATCH(ctx) {},
|
||||
async PUT(ctx) {},
|
||||
async DELETE(ctx) {},
|
||||
};`);
|
||||
});
|
||||
|
||||
Deno.test(
|
||||
"update - 1.x update handler signature method one arg",
|
||||
async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `export const handler: Handlers = {
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `export const handler: Handlers = {
|
||||
GET(req) {
|
||||
return Response.redirect(req.url);
|
||||
},
|
||||
};`,
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`export const handler: Handlers = {
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`export const handler: Handlers = {
|
||||
GET(ctx) {
|
||||
const req = ctx.req;
|
||||
|
||||
return Response.redirect(req.url);
|
||||
},
|
||||
};`);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test.ignore(
|
||||
"update - 1.x update handler signature variable",
|
||||
async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `export const handler: Handlers = {
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `export const handler: Handlers = {
|
||||
GET: (req) => Response.redirect(req.url)
|
||||
};`,
|
||||
"/routes/foo.tsx": `export const handler: Handlers = {
|
||||
"/routes/foo.tsx": `export const handler: Handlers = {
|
||||
GET: (req, ctx) => Response.redirect(req.url),
|
||||
};`,
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`export const handler: Handlers = {
|
||||
GET: (ctx) => {
|
||||
const req = ctx.req;
|
||||
|
||||
return Response.redirect(req.url);
|
||||
},
|
||||
};`);
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`export const handler: Handlers = {
|
||||
GET: (ctx) => {
|
||||
const req = ctx.req;
|
||||
|
||||
return Response.redirect(req.url);
|
||||
},
|
||||
};`);
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`export const handler: Handlers = {
|
||||
GET: (ctx) => {
|
||||
const req = ctx.req;
|
||||
|
||||
return Response.redirect(req.url);
|
||||
},
|
||||
};`);
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`export const handler: Handlers = {
|
||||
GET: (ctx) => {
|
||||
const req = ctx.req;
|
||||
|
||||
return Response.redirect(req.url);
|
||||
},
|
||||
};`);
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test(
|
||||
"update - 1.x update handler signature non-inferred",
|
||||
async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `export const handler = {
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `export const handler = {
|
||||
GET(req: Request){
|
||||
return Response.redirect(req.url);
|
||||
}
|
||||
};`,
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
|
||||
export const handler = {
|
||||
GET(ctx: FreshContext) {
|
||||
@@ -402,17 +393,17 @@ export const handler = {
|
||||
return Response.redirect(req.url);
|
||||
},
|
||||
};`);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test(
|
||||
"update - 1.x update handler signature with destructure",
|
||||
async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(req, { params, render, remoteAddr }) {},
|
||||
@@ -421,11 +412,11 @@ export const handler: Handlers = {
|
||||
async PUT(req, { params, render, remoteAddr }) {},
|
||||
async DELETE(req, { params, render, remoteAddr }) {},
|
||||
};`,
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh/compat";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET({ params, render, info, req }) {
|
||||
@@ -444,66 +435,66 @@ export const handler: Handlers = {
|
||||
const remoteAddr = info.remoteAddr;
|
||||
},
|
||||
};`);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test("update - 1.x update define* handler signatures", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/_app.tsx": `import { defineApp } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/_app.tsx": `import { defineApp } from "$fresh/server.ts";
|
||||
export default defineApp(async (req, ctx) => {
|
||||
return null;
|
||||
});`,
|
||||
"/routes/_layout.tsx": `import { defineLayout } from "$fresh/server.ts";
|
||||
"/routes/_layout.tsx": `import { defineLayout } from "$fresh/server.ts";
|
||||
export default defineLayout(async (req, ctx) => {
|
||||
return null;
|
||||
});`,
|
||||
"/routes/foo.tsx": `import { defineRoute } from "$fresh/server.ts";
|
||||
"/routes/foo.tsx": `import { defineRoute } from "$fresh/server.ts";
|
||||
export default defineRoute(async (req, ctx) => {
|
||||
return null;
|
||||
});`,
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/_app.tsx"])
|
||||
.toEqual(`import { defineApp } from "fresh/compat";
|
||||
expect(files["/routes/_app.tsx"])
|
||||
.toEqual(`import { defineApp } from "fresh/compat";
|
||||
|
||||
export default defineApp(async (ctx) => {
|
||||
const req = ctx.req;
|
||||
|
||||
return null;
|
||||
});`);
|
||||
expect(files["/routes/_layout.tsx"])
|
||||
.toEqual(`import { defineLayout } from "fresh/compat";
|
||||
expect(files["/routes/_layout.tsx"])
|
||||
.toEqual(`import { defineLayout } from "fresh/compat";
|
||||
|
||||
export default defineLayout(async (ctx) => {
|
||||
const req = ctx.req;
|
||||
|
||||
return null;
|
||||
});`);
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`import { defineRoute } from "fresh/compat";
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`import { defineRoute } from "fresh/compat";
|
||||
|
||||
export default defineRoute(async (ctx) => {
|
||||
const req = ctx.req;
|
||||
|
||||
return null;
|
||||
});`);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test(
|
||||
"update - 1.x update component signature async",
|
||||
async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx":
|
||||
`export default async function Index(req: Request, ctx: RouteContext) {
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx":
|
||||
`export default async function Index(req: Request, ctx: RouteContext) {
|
||||
if (true) {
|
||||
return ctx.renderNotFound();
|
||||
}
|
||||
@@ -513,11 +504,11 @@ Deno.test(
|
||||
}
|
||||
return new Response(req.url);
|
||||
}`,
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
});
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { FreshContext } from "fresh";
|
||||
|
||||
export default async function Index(ctx: FreshContext) {
|
||||
const req = ctx.req;
|
||||
@@ -531,33 +522,33 @@ export default async function Index(ctx: FreshContext) {
|
||||
}
|
||||
return new Response(req.url);
|
||||
}`);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test.ignore(
|
||||
"update - 1.x ctx.renderNotFound() -> ctx.throw()",
|
||||
async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_req, ctx) {
|
||||
return ctx.renderNotFound();
|
||||
},
|
||||
};`,
|
||||
"/routes/foo.tsx": `export const handler = (ctx) => {
|
||||
"/routes/foo.tsx": `export const handler = (ctx) => {
|
||||
return ctx.renderNotFound();
|
||||
}`,
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh";
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {
|
||||
@@ -565,21 +556,21 @@ export const handler: Handlers = {
|
||||
},
|
||||
};`);
|
||||
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`export const handler = (ctx) => {
|
||||
expect(files["/routes/foo.tsx"])
|
||||
.toEqual(`export const handler = (ctx) => {
|
||||
throw HttpError(404);
|
||||
};`);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test.ignore(
|
||||
"update - 1.x ctx.remoteAddr -> ctx.info.remoteAddr",
|
||||
async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_req, ctx) {
|
||||
@@ -588,13 +579,13 @@ export const handler: Handlers = {
|
||||
return new Response(msg);
|
||||
},
|
||||
};`,
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh";
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(ctx) {
|
||||
@@ -603,15 +594,15 @@ export const handler: Handlers = {
|
||||
return new Response(msg);
|
||||
},
|
||||
};`);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test.ignore("update - 1.x destructured ctx members", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/index.tsx": `import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_req, { url, renderNotFound, remoteAddr }) {
|
||||
@@ -623,13 +614,13 @@ export const handler: Handlers = {
|
||||
}
|
||||
},
|
||||
};`,
|
||||
});
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh";
|
||||
expect(files["/routes/index.tsx"])
|
||||
.toEqual(`import { Handlers } from "fresh";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET({ url, throw, info }) {
|
||||
@@ -644,40 +635,39 @@ export const handler: Handlers = {
|
||||
}
|
||||
},
|
||||
};`);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("update - 1.x remove reference comments", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/main.ts": `/// <reference no-default-lib="true" />
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/routes/main.ts": `/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/main.ts"]).toEqual("");
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/routes/main.ts"]).toEqual("");
|
||||
});
|
||||
|
||||
Deno.test("update - island files", async () => {
|
||||
await withTmpDir(async (dir) => {
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/islands/foo.tsx": `import { IS_BROWSER } from "$fresh/runtime.ts";`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/islands/foo.tsx"]).toEqual(
|
||||
`import { IS_BROWSER } from "fresh/runtime";`,
|
||||
);
|
||||
await using _tmp = await withTmpDir();
|
||||
const dir = _tmp.dir;
|
||||
await writeFiles(dir, {
|
||||
"/deno.json": `{}`,
|
||||
"/islands/foo.tsx": `import { IS_BROWSER } from "$fresh/runtime.ts";`,
|
||||
});
|
||||
|
||||
await updateProject(dir);
|
||||
const files = await readFiles(dir);
|
||||
|
||||
expect(files["/islands/foo.tsx"]).toEqual(
|
||||
`import { IS_BROWSER } from "fresh/runtime";`,
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user