|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import { mkdtempSync, readFileSync, realpathSync, writeFileSync } from "fs"; |
| 3 | +import { tmpdir } from "os"; |
| 4 | +import * as path from "path"; |
| 5 | +import { join } from "path"; |
| 6 | +import { beforeAll, describe, it } from "vitest"; |
| 7 | + |
| 8 | +describe("`wrangler types` - file comment", () => { |
| 9 | + let tempDir: string; |
| 10 | + |
| 11 | + beforeAll(() => { |
| 12 | + tempDir = realpathSync(mkdtempSync(join(tmpdir(), "wrangler-types-test"))); |
| 13 | + const tomlFile = join(tempDir, "wrangler.toml"); |
| 14 | + const tomlFileA = join(tempDir, "wranglerA.toml"); |
| 15 | + writeFileSync(tomlFile, '\n[vars]\nMY_VAR = "my-var-value"\n'); |
| 16 | + writeFileSync(tomlFileA, '\n[vars]\nMY_VAR = "my-var-value"\n'); |
| 17 | + }); |
| 18 | + |
| 19 | + function runWranglerTypesCommand( |
| 20 | + args = "", |
| 21 | + expectedOutputFile = "worker-configuration.d.ts" |
| 22 | + ): string { |
| 23 | + const wranglerPath = path.resolve( |
| 24 | + __dirname, |
| 25 | + "..", |
| 26 | + "..", |
| 27 | + "..", |
| 28 | + "packages", |
| 29 | + "wrangler" |
| 30 | + ); |
| 31 | + execSync(`npx ${wranglerPath} types ${args}`, { |
| 32 | + cwd: tempDir, |
| 33 | + }); |
| 34 | + const typesFile = join(tempDir, expectedOutputFile); |
| 35 | + return readFileSync(typesFile, "utf-8"); |
| 36 | + } |
| 37 | + |
| 38 | + describe("includes a comment specifying the command run", () => { |
| 39 | + it("(base command)", async ({ expect }) => { |
| 40 | + const typesCommandOutput = runWranglerTypesCommand(); |
| 41 | + expect(typesCommandOutput).toContain("by running `wrangler types`"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("(with types customization)", async ({ expect }) => { |
| 45 | + const typesCommandOutput = runWranglerTypesCommand( |
| 46 | + "--env-interface MyCloudflareEnv ./cflare-env.d.ts", |
| 47 | + "./cflare-env.d.ts" |
| 48 | + ); |
| 49 | + expect(typesCommandOutput).toContain( |
| 50 | + "by running `wrangler types --env-interface MyCloudflareEnv ./cflare-env.d.ts`" |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it("(with wrangler top level options)", async ({ expect }) => { |
| 55 | + const typesCommandOutput = runWranglerTypesCommand("-c wranglerA.toml"); |
| 56 | + expect(typesCommandOutput).toContain( |
| 57 | + "by running `wrangler types -c wranglerA.toml`" |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments