-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C3: Use latest version of @cloudflare/workers-types in workers projec…
…ts (#4525) * C3: Stop using stale wrangler and workers-types versions Installing these as part of the generator instead of relying on a stale version in the template we copied ensures we have the most up-to-date version. * C3: Default stringification spacing with writeJSON * changeset * Fix unused import * C3: Restoring wrangler in worker template devDependencies * Fix unit test on windows
- Loading branch information
Showing
12 changed files
with
233 additions
and
43 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,7 @@ | ||
--- | ||
"create-cloudflare": minor | ||
--- | ||
|
||
C3: Use latest version of `wrangler` and `@cloudflare/workers-types`. | ||
|
||
Also updates the `types` entry of the project's `tsconfig.json` to use type definitions corresponding to the latest compatibility date. |
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,19 @@ | ||
import { C3_DEFAULTS } from "helpers/cli"; | ||
import type { C3Args, PagesGeneratorContext as Context } from "types"; | ||
|
||
export const createTestArgs = (args?: Partial<C3Args>) => { | ||
return { | ||
...C3_DEFAULTS, | ||
...args, | ||
}; | ||
}; | ||
|
||
export const createTestContext = (name = "test", args?: C3Args): Context => { | ||
const path = `./${name}`; | ||
return { | ||
project: { name, path }, | ||
args: args ?? createTestArgs(), | ||
originalCWD: path, | ||
gitRepoAlreadyExisted: false, | ||
}; | ||
}; |
116 changes: 116 additions & 0 deletions
116
packages/create-cloudflare/src/__tests__/workers.test.ts
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,116 @@ | ||
import { existsSync, readdirSync } from "fs"; | ||
import { readFile, writeFile } from "helpers/files"; | ||
import { describe, expect, test, vi, afterEach, beforeEach } from "vitest"; | ||
import * as workers from "../workers"; | ||
import { createTestContext } from "./helpers"; | ||
import type { Dirent } from "fs"; | ||
import type { PagesGeneratorContext } from "types"; | ||
|
||
const mockWorkersTypesDirListing = [ | ||
"2021-11-03", | ||
"2022-03-21", | ||
"2022-11-30", | ||
"2023-03-01", | ||
"2023-07-01", | ||
"experimental", | ||
"index.d.ts", | ||
"index.ts", | ||
"oldest", | ||
"package.json", | ||
]; | ||
|
||
vi.mock("fs"); | ||
vi.mock("helpers/files"); | ||
|
||
describe("getLatestTypesEntrypoint", () => { | ||
const ctx = createTestContext(); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
test("happy path", async () => { | ||
vi.mocked(readdirSync).mockImplementation( | ||
// vitest won't resolve the type for the correct overload thus the trickery | ||
() => [...mockWorkersTypesDirListing] as unknown as Dirent[] | ||
); | ||
|
||
const entrypoint = workers.getLatestTypesEntrypoint(ctx); | ||
expect(entrypoint).toBe("2023-07-01"); | ||
}); | ||
|
||
test("read error", async () => { | ||
vi.mocked(readdirSync).mockImplementation(() => { | ||
throw new Error("ENOENT: no such file or directory"); | ||
}); | ||
|
||
const entrypoint = workers.getLatestTypesEntrypoint(ctx); | ||
expect(entrypoint).toBe(null); | ||
}); | ||
|
||
test("empty directory", async () => { | ||
vi.mocked(readdirSync).mockImplementation(() => []); | ||
|
||
const entrypoint = workers.getLatestTypesEntrypoint(ctx); | ||
expect(entrypoint).toBe(null); | ||
}); | ||
|
||
test("no compat dates found", async () => { | ||
vi.mocked(readdirSync).mockImplementation( | ||
() => ["foo", "bar"] as unknown as Dirent[] | ||
); | ||
|
||
const entrypoint = workers.getLatestTypesEntrypoint(ctx); | ||
expect(entrypoint).toBe(null); | ||
}); | ||
}); | ||
|
||
describe("updateTsConfig", () => { | ||
let ctx: PagesGeneratorContext; | ||
|
||
beforeEach(() => { | ||
ctx = createTestContext(); | ||
|
||
ctx.args.ts = true; | ||
vi.mocked(existsSync).mockImplementation(() => true); | ||
// mock getLatestTypesEntrypoint | ||
vi.mocked(readdirSync).mockImplementation( | ||
() => ["2023-07-01"] as unknown as Dirent[] | ||
); | ||
|
||
// Mock the read of tsconfig.json | ||
vi.mocked(readFile).mockImplementation( | ||
() => `{types: ["@cloudflare/workers-types"]}` | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
test("happy path", async () => { | ||
await workers.updateTsConfig(ctx); | ||
|
||
expect(vi.mocked(writeFile).mock.calls[0][1]).toEqual( | ||
`{types: ["@cloudflare/workers-types/2023-07-01"]}` | ||
); | ||
}); | ||
|
||
test("not using ts", async () => { | ||
ctx.args.ts = false; | ||
expect(writeFile).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("tsconfig.json not found", async () => { | ||
vi.mocked(existsSync).mockImplementation(() => false); | ||
expect(writeFile).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("latest entrypoint not found", async () => { | ||
vi.mocked(readdirSync).mockImplementation( | ||
() => ["README.md"] as unknown as Dirent[] | ||
); | ||
|
||
expect(writeFile).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
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