Skip to content

Commit d9b781a

Browse files
committed
add more tests
1 parent ad1cbae commit d9b781a

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import assert from "node:assert";
2+
import { mkdir, readFile, writeFile } from "node:fs/promises";
3+
import path from "node:path";
4+
import dedent from "ts-dedent";
5+
import { bundleWorker } from "../deployment-bundle/bundle";
6+
import { noopModuleCollector } from "../deployment-bundle/module-collection";
7+
import { isProcessEnvPopulated } from "../process-env";
8+
import { mockConsoleMethods } from "./helpers/mock-console";
9+
import { runInTempDir } from "./helpers/run-in-tmp";
10+
11+
/*
12+
* This file contains inline comments with the word "javascript"
13+
* This signals to a compatible editor extension that the template string
14+
* contents should be syntax-highlighted as JavaScript. One such extension
15+
* is zjcompt.es6-string-javascript, but there are others.
16+
*/
17+
18+
async function seedFs(files: Record<string, string>): Promise<void> {
19+
for (const [location, contents] of Object.entries(files)) {
20+
await mkdir(path.dirname(location), { recursive: true });
21+
await writeFile(location, contents);
22+
}
23+
}
24+
25+
describe("isProcessEnvPopulated", () => {
26+
test("default", () => {
27+
expect(isProcessEnvPopulated(undefined, ["nodejs_compat"])).toBe(false);
28+
});
29+
30+
test("future date", () => {
31+
expect(isProcessEnvPopulated("2026-01-01", ["nodejs_compat"])).toBe(true);
32+
});
33+
34+
test("old date", () => {
35+
expect(isProcessEnvPopulated("2000-01-01", ["nodejs_compat"])).toBe(false);
36+
});
37+
38+
test("switch date", () => {
39+
expect(isProcessEnvPopulated("2025-04-01", ["nodejs_compat"])).toBe(true);
40+
});
41+
42+
test("old date, but with flag", () => {
43+
expect(
44+
isProcessEnvPopulated("2000-01-01", [
45+
"nodejs_compat",
46+
"nodejs_compat_populate_process_env",
47+
])
48+
).toBe(true);
49+
});
50+
51+
test("old date, with disable flag", () => {
52+
expect(
53+
isProcessEnvPopulated("2000-01-01", [
54+
"nodejs_compat",
55+
"nodejs_compat_do_not_populate_process_env",
56+
])
57+
).toBe(false);
58+
});
59+
60+
test("future date, but with disable flag", () => {
61+
expect(
62+
isProcessEnvPopulated("2026-01-01", [
63+
"nodejs_compat",
64+
"nodejs_compat_do_not_populate_process_env",
65+
])
66+
).toBe(false);
67+
});
68+
69+
test("future date, with enable flag", () => {
70+
expect(
71+
isProcessEnvPopulated("2026-01-01", [
72+
"nodejs_compat",
73+
"nodejs_compat_populate_process_env",
74+
])
75+
).toBe(true);
76+
});
77+
78+
test("future date without nodejs_compat", () => {
79+
expect(isProcessEnvPopulated("2026-01-01")).toBe(false);
80+
});
81+
82+
test("future date, with enable flag but without nodejs_compat", () => {
83+
expect(
84+
isProcessEnvPopulated("2026-01-01", [
85+
"nodejs_compat_populate_process_env",
86+
])
87+
).toBe(false);
88+
});
89+
90+
test("errors with disable and enable flags specified", () => {
91+
try {
92+
isProcessEnvPopulated("2024-01-01", [
93+
"nodejs_compat_populate_process_env",
94+
"nodejs_compat_do_not_populate_process_env",
95+
]);
96+
assert(false, "Unreachable");
97+
} catch (e) {
98+
expect(e).toMatchInlineSnapshot(
99+
`[Error: Can't both enable and disable a flag]`
100+
);
101+
}
102+
});
103+
});

packages/wrangler/src/__tests__/type-generation.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,100 @@ describe("generate types", () => {
460460
`);
461461
});
462462

463+
it("should include stringified process.env types for vars, secrets, and json", async () => {
464+
fs.writeFileSync(
465+
"./index.ts",
466+
`import { DurableObject } from 'cloudflare:workers';
467+
export default { async fetch () {} };
468+
export class DurableDirect extends DurableObject {}
469+
export { DurableReexport } from './durable-2.js';
470+
// This should not be picked up, because it's external:
471+
export class DurableExternal extends DurableObject {}`
472+
);
473+
fs.writeFileSync(
474+
"./wrangler.toml",
475+
TOML.stringify({
476+
compatibility_date: "2022-01-12",
477+
compatibility_flags: [
478+
"nodejs_compat",
479+
"nodejs_compat_populate_process_env",
480+
],
481+
name: "test-name",
482+
main: "./index.ts",
483+
...bindingsConfigMock,
484+
unsafe: bindingsConfigMock.unsafe ?? {},
485+
} as unknown as TOML.JsonMap),
486+
"utf-8"
487+
);
488+
fs.writeFileSync("./.dev.vars", "SECRET=test", "utf-8");
489+
490+
await runWrangler("types --include-runtime=false");
491+
expect(std.out).toMatchInlineSnapshot(`
492+
"Generating project types...
493+
494+
declare namespace Cloudflare {
495+
interface Env {
496+
TEST_KV_NAMESPACE: KVNamespace;
497+
SOMETHING: \\"asdasdfasdf\\";
498+
ANOTHER: \\"thing\\";
499+
\\"some-other-var\\": \\"some-other-value\\";
500+
OBJECT_VAR: {\\"enterprise\\":\\"1701-D\\",\\"activeDuty\\":true,\\"captian\\":\\"Picard\\"};
501+
SECRET: string;
502+
DURABLE_DIRECT_EXPORT: DurableObjectNamespace<import(\\"./index\\").DurableDirect>;
503+
DURABLE_RE_EXPORT: DurableObjectNamespace<import(\\"./index\\").DurableReexport>;
504+
DURABLE_NO_EXPORT: DurableObjectNamespace /* DurableNoexport */;
505+
DURABLE_EXTERNAL: DurableObjectNamespace /* DurableExternal from external-worker */;
506+
R2_BUCKET_BINDING: R2Bucket;
507+
D1_TESTING_SOMETHING: D1Database;
508+
SERVICE_BINDING: Fetcher;
509+
AE_DATASET_BINDING: AnalyticsEngineDataset;
510+
NAMESPACE_BINDING: DispatchNamespace;
511+
LOGFWDR_SCHEMA: any;
512+
SOME_DATA_BLOB1: ArrayBuffer;
513+
SOME_DATA_BLOB2: ArrayBuffer;
514+
SOME_TEXT_BLOB1: string;
515+
SOME_TEXT_BLOB2: string;
516+
testing_unsafe: any;
517+
UNSAFE_RATELIMIT: RateLimit;
518+
TEST_QUEUE_BINDING: Queue;
519+
SEND_EMAIL_BINDING: SendEmail;
520+
VECTORIZE_BINDING: VectorizeIndex;
521+
HYPERDRIVE_BINDING: Hyperdrive;
522+
MTLS_BINDING: Fetcher;
523+
BROWSER_BINDING: Fetcher;
524+
AI_BINDING: Ai;
525+
IMAGES_BINDING: ImagesBinding;
526+
VERSION_METADATA_BINDING: { id: string; tag: string };
527+
ASSETS_BINDING: Fetcher;
528+
}
529+
}
530+
interface Env extends Cloudflare.Env {}
531+
type StringifyValues<EnvType extends Record<string, unknown>> = {
532+
[Binding in keyof EnvType]: EnvType[Binding] extends string ? EnvType[Binding] : string;
533+
};
534+
declare namespace NodeJS {
535+
interface ProcessEnv extends StringifyValues<Pick<Cloudflare.Env, \\"SOMETHING\\" | \\"ANOTHER\\" | \\"some-other-var\\" | \\"OBJECT_VAR\\" | \\"SECRET\\">> {}
536+
}
537+
declare module \\"*.txt\\" {
538+
const value: string;
539+
export default value;
540+
}
541+
declare module \\"*.webp\\" {
542+
const value: ArrayBuffer;
543+
export default value;
544+
}
545+
declare module \\"*.wasm\\" {
546+
const value: WebAssembly.Module;
547+
export default value;
548+
}
549+
────────────────────────────────────────────────────────────
550+
✨ Types written to worker-configuration.d.ts
551+
552+
📣 Remember to rerun 'wrangler types' after you change your wrangler.toml file.
553+
"
554+
`);
555+
});
556+
463557
it("should create a DTS file at the location that the command is executed from", async () => {
464558
fs.writeFileSync("./index.ts", "export default { async fetch () {} };");
465559
fs.writeFileSync(

0 commit comments

Comments
 (0)