-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_utils.ts
64 lines (60 loc) · 1.32 KB
/
test_utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { _env, AppWindow, getEnv } from "./env.ts";
export async function inBrowser(fn: () => Promise<void>) {
try {
(window as AppWindow).app = {
env: {
"APP_ENV": getEnv("APP_ENV"),
},
context: {},
};
_env.isServer = false;
await fn();
} finally {
delete (window as Partial<AppWindow>).app;
_env.isServer = true;
}
}
export function inBrowserSync(fn: () => void) {
try {
(window as AppWindow).app = {
env: {
"APP_ENV": getEnv("APP_ENV"),
},
context: {},
};
_env.isServer = false;
fn();
} finally {
delete (window as Partial<AppWindow>).app;
_env.isServer = true;
}
}
export async function inEnvironment(
environment: string,
fn: () => Promise<void>,
): Promise<void> {
const original = Deno.env.get("APP_ENV");
try {
Deno.env.set("APP_ENV", environment);
await fn();
} finally {
if (original) {
Deno.env.set("APP_ENV", original);
} else {
Deno.env.delete("APP_ENV");
}
}
}
export function inEnvironmentSync(environment: string, fn: () => void): void {
const original = Deno.env.get("APP_ENV");
try {
Deno.env.set("APP_ENV", environment);
fn();
} finally {
if (original) {
Deno.env.set("APP_ENV", original);
} else {
Deno.env.delete("APP_ENV");
}
}
}