@sigil-dev/grimoire 0.9.29 → 0.9.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
package/src/testing/dom.ts
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Preload for component tests: `sigil test` passes this to `bun test`.
|
|
3
|
-
*
|
|
4
|
-
* Gives a test file what a browser would: DOM globals, the compiler in `dom`
|
|
5
|
-
* mode so `.tsx` components import and run, and the `$env/*` modules. It also
|
|
6
|
-
* unmounts whatever a test rendered when the test ends.
|
|
7
|
-
*
|
|
8
|
-
* Route and server tests (`*.server.test.ts`) do not use this: they run
|
|
9
|
-
* the app on the server through `@sigil-dev/grimoire/testing/server`, which
|
|
10
|
-
* compiles for the server instead. The two cannot share a process, because a
|
|
11
|
-
* module compiles once, so `sigil test` runs them as separate passes.
|
|
12
|
-
*/
|
|
13
|
-
import { afterEach } from "bun:test";
|
|
14
|
-
import { sigil } from "@sigil-dev/compiler/bun";
|
|
15
|
-
import { Window } from "happy-dom";
|
|
16
|
-
import { ENV_MODULE, envModuleSource } from "../env/modules";
|
|
17
|
-
import { cleanup } from "./index";
|
|
18
|
-
|
|
19
|
-
const window = new Window({ url: "http://localhost/" });
|
|
20
|
-
const g = globalThis as Record<string, unknown>;
|
|
21
|
-
for (const name of [
|
|
22
|
-
"window",
|
|
23
|
-
"document",
|
|
24
|
-
"navigator",
|
|
25
|
-
"location",
|
|
26
|
-
"history",
|
|
27
|
-
"Node",
|
|
28
|
-
"Element",
|
|
29
|
-
"HTMLElement",
|
|
30
|
-
"SVGElement",
|
|
31
|
-
"Text",
|
|
32
|
-
"Comment",
|
|
33
|
-
"DocumentFragment",
|
|
34
|
-
"Event",
|
|
35
|
-
"CustomEvent",
|
|
36
|
-
"MouseEvent",
|
|
37
|
-
"KeyboardEvent",
|
|
38
|
-
"InputEvent",
|
|
39
|
-
"FocusEvent",
|
|
40
|
-
"MutationObserver",
|
|
41
|
-
"getComputedStyle",
|
|
42
|
-
"requestAnimationFrame",
|
|
43
|
-
"cancelAnimationFrame",
|
|
44
|
-
"DOMParser",
|
|
45
|
-
] as const) {
|
|
46
|
-
const value = (window as unknown as Record<string, unknown>)[name];
|
|
47
|
-
if (value !== undefined) g[name] = value;
|
|
48
|
-
}
|
|
49
|
-
g.window = window;
|
|
50
|
-
|
|
51
|
-
Bun.plugin(sigil({ mode: "dom" }));
|
|
52
|
-
|
|
53
|
-
// `$env/*` for browser code: public values are readable, private ones throw
|
|
54
|
-
// when imported, exactly as in the browser build.
|
|
55
|
-
Bun.plugin({
|
|
56
|
-
name: "sigil-test-env",
|
|
57
|
-
setup(build) {
|
|
58
|
-
for (const specifier of [
|
|
59
|
-
"$env/static/public",
|
|
60
|
-
"$env/static/private",
|
|
61
|
-
"$env/dynamic/public",
|
|
62
|
-
"$env/dynamic/private",
|
|
63
|
-
]) {
|
|
64
|
-
if (!ENV_MODULE.test(specifier)) continue;
|
|
65
|
-
build.module(specifier, () => ({
|
|
66
|
-
contents: envModuleSource(specifier, "browser"),
|
|
67
|
-
loader: "js",
|
|
68
|
-
}));
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
afterEach(cleanup);
|
|
1
|
+
/**
|
|
2
|
+
* Preload for component tests: `sigil test` passes this to `bun test`.
|
|
3
|
+
*
|
|
4
|
+
* Gives a test file what a browser would: DOM globals, the compiler in `dom`
|
|
5
|
+
* mode so `.tsx` components import and run, and the `$env/*` modules. It also
|
|
6
|
+
* unmounts whatever a test rendered when the test ends.
|
|
7
|
+
*
|
|
8
|
+
* Route and server tests (`*.server.test.ts`) do not use this: they run
|
|
9
|
+
* the app on the server through `@sigil-dev/grimoire/testing/server`, which
|
|
10
|
+
* compiles for the server instead. The two cannot share a process, because a
|
|
11
|
+
* module compiles once, so `sigil test` runs them as separate passes.
|
|
12
|
+
*/
|
|
13
|
+
import { afterEach } from "bun:test";
|
|
14
|
+
import { sigil } from "@sigil-dev/compiler/bun";
|
|
15
|
+
import { Window } from "happy-dom";
|
|
16
|
+
import { ENV_MODULE, envModuleSource } from "../env/modules";
|
|
17
|
+
import { cleanup } from "./index";
|
|
18
|
+
|
|
19
|
+
const window = new Window({ url: "http://localhost/" });
|
|
20
|
+
const g = globalThis as Record<string, unknown>;
|
|
21
|
+
for (const name of [
|
|
22
|
+
"window",
|
|
23
|
+
"document",
|
|
24
|
+
"navigator",
|
|
25
|
+
"location",
|
|
26
|
+
"history",
|
|
27
|
+
"Node",
|
|
28
|
+
"Element",
|
|
29
|
+
"HTMLElement",
|
|
30
|
+
"SVGElement",
|
|
31
|
+
"Text",
|
|
32
|
+
"Comment",
|
|
33
|
+
"DocumentFragment",
|
|
34
|
+
"Event",
|
|
35
|
+
"CustomEvent",
|
|
36
|
+
"MouseEvent",
|
|
37
|
+
"KeyboardEvent",
|
|
38
|
+
"InputEvent",
|
|
39
|
+
"FocusEvent",
|
|
40
|
+
"MutationObserver",
|
|
41
|
+
"getComputedStyle",
|
|
42
|
+
"requestAnimationFrame",
|
|
43
|
+
"cancelAnimationFrame",
|
|
44
|
+
"DOMParser",
|
|
45
|
+
] as const) {
|
|
46
|
+
const value = (window as unknown as Record<string, unknown>)[name];
|
|
47
|
+
if (value !== undefined) g[name] = value;
|
|
48
|
+
}
|
|
49
|
+
g.window = window;
|
|
50
|
+
|
|
51
|
+
Bun.plugin(sigil({ mode: "dom" }));
|
|
52
|
+
|
|
53
|
+
// `$env/*` for browser code: public values are readable, private ones throw
|
|
54
|
+
// when imported, exactly as in the browser build.
|
|
55
|
+
Bun.plugin({
|
|
56
|
+
name: "sigil-test-env",
|
|
57
|
+
setup(build) {
|
|
58
|
+
for (const specifier of [
|
|
59
|
+
"$env/static/public",
|
|
60
|
+
"$env/static/private",
|
|
61
|
+
"$env/dynamic/public",
|
|
62
|
+
"$env/dynamic/private",
|
|
63
|
+
]) {
|
|
64
|
+
if (!ENV_MODULE.test(specifier)) continue;
|
|
65
|
+
build.module(specifier, () => ({
|
|
66
|
+
contents: envModuleSource(specifier, "browser"),
|
|
67
|
+
loader: "js",
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
afterEach(cleanup);
|
package/src/testing/index.ts
CHANGED
|
@@ -1,169 +1,169 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test helpers for components and for the functions a route is made of.
|
|
3
|
-
*
|
|
4
|
-
* ```tsx
|
|
5
|
-
* import { expect, test } from "bun:test";
|
|
6
|
-
* import { render, click, tick } from "@sigil-dev/grimoire/testing";
|
|
7
|
-
* import { Counter } from "./Counter.tsx";
|
|
8
|
-
*
|
|
9
|
-
* test("counts", async () => {
|
|
10
|
-
* const { $, text } = render(Counter, { start: 1 });
|
|
11
|
-
* click($("button")!);
|
|
12
|
-
* await tick();
|
|
13
|
-
* expect(text()).toContain("2");
|
|
14
|
-
* });
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* Run with `sigil test`. Component tests need the DOM preload that command
|
|
18
|
-
* adds; for routes see `@sigil-dev/grimoire/testing/server`.
|
|
19
|
-
*/
|
|
20
|
-
import { insert, tick, withEffectScope } from "@sigil-dev/runtime";
|
|
21
|
-
import { createCookies } from "../server/cookie-utils";
|
|
22
|
-
import type { Cookies } from "../server/hooks";
|
|
23
|
-
|
|
24
|
-
export { tick };
|
|
25
|
-
|
|
26
|
-
export interface Rendered {
|
|
27
|
-
/** The element the component was rendered into. */
|
|
28
|
-
container: HTMLElement;
|
|
29
|
-
/** First element matching a selector inside the render. */
|
|
30
|
-
$(selector: string): HTMLElement | null;
|
|
31
|
-
/** Every element matching a selector inside the render. */
|
|
32
|
-
$$(selector: string): HTMLElement[];
|
|
33
|
-
/** The rendered text. */
|
|
34
|
-
text(): string;
|
|
35
|
-
/** The rendered markup. */
|
|
36
|
-
html(): string;
|
|
37
|
-
/** Remove it and stop its effects. Done for you after each test. */
|
|
38
|
-
unmount(): void;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const mounted = new Set<() => void>();
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Render a component into the document, the way a client-side navigation
|
|
45
|
-
* would: a fresh render, with effects live.
|
|
46
|
-
*
|
|
47
|
-
* Each call gets its own container, so one test can render several things.
|
|
48
|
-
*/
|
|
49
|
-
export function render<P extends object>(
|
|
50
|
-
Component: (props: P) => unknown,
|
|
51
|
-
props: P = {} as P,
|
|
52
|
-
): Rendered {
|
|
53
|
-
const container = document.createElement("div");
|
|
54
|
-
document.body.appendChild(container);
|
|
55
|
-
const stop = withEffectScope(() => {
|
|
56
|
-
insert(container, Component(props));
|
|
57
|
-
});
|
|
58
|
-
const unmount = () => {
|
|
59
|
-
stop();
|
|
60
|
-
container.remove();
|
|
61
|
-
mounted.delete(unmount);
|
|
62
|
-
};
|
|
63
|
-
mounted.add(unmount);
|
|
64
|
-
return {
|
|
65
|
-
container,
|
|
66
|
-
$: (selector) => container.querySelector<HTMLElement>(selector),
|
|
67
|
-
$$: (selector) =>
|
|
68
|
-
Array.from(container.querySelectorAll<HTMLElement>(selector)),
|
|
69
|
-
text: () => container.textContent ?? "",
|
|
70
|
-
html: () => container.innerHTML,
|
|
71
|
-
unmount,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Unmount everything rendered so far. The DOM preload runs this after each test. */
|
|
76
|
-
export function cleanup(): void {
|
|
77
|
-
for (const unmount of [...mounted]) unmount();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Click an element, with the bubbling a real click has. */
|
|
81
|
-
export function click(el: Element): void {
|
|
82
|
-
el.dispatchEvent(
|
|
83
|
-
new MouseEvent("click", { bubbles: true, cancelable: true }),
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Type into an input or textarea: set its value and fire `input`, as a user would. */
|
|
88
|
-
export function type(el: Element, value: string): void {
|
|
89
|
-
(el as HTMLInputElement).value = value;
|
|
90
|
-
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** Fire any event on an element. */
|
|
94
|
-
export function fire(el: Element, type: string, init: EventInit = {}): void {
|
|
95
|
-
el.dispatchEvent(
|
|
96
|
-
new Event(type, { bubbles: true, cancelable: true, ...init }),
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// ---------------------------------------------------------------------------
|
|
101
|
-
// the arguments of load functions and actions
|
|
102
|
-
// ---------------------------------------------------------------------------
|
|
103
|
-
|
|
104
|
-
export interface ContextOptions {
|
|
105
|
-
params?: Record<string, string>;
|
|
106
|
-
/** Path or URL. A path is resolved against `http://localhost`. */
|
|
107
|
-
url?: string;
|
|
108
|
-
method?: string;
|
|
109
|
-
headers?: Record<string, string>;
|
|
110
|
-
/** A form submission: fields are sent url-encoded. */
|
|
111
|
-
form?: Record<string, string>;
|
|
112
|
-
/** A JSON body. */
|
|
113
|
-
json?: unknown;
|
|
114
|
-
locals?: Record<string, unknown>;
|
|
115
|
-
/** Cookies the request carries. */
|
|
116
|
-
cookies?: Record<string, string>;
|
|
117
|
-
/** What `fetch` inside the function does. Defaults to the real one. */
|
|
118
|
-
fetch?: typeof fetch;
|
|
119
|
-
/** What `parent()` resolves to. */
|
|
120
|
-
parent?: Record<string, unknown>;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export type TestContext = ReturnType<typeof context>;
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* The argument a `load`, an action or a `+server.ts` handler receives, so the
|
|
127
|
-
* function can be called directly:
|
|
128
|
-
*
|
|
129
|
-
* ```ts
|
|
130
|
-
* const data = await load(context({ params: { id: "7" } }));
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* `cookies` is the real implementation; `cookies.toHeaders()` lists what the
|
|
134
|
-
* function set, for assertions.
|
|
135
|
-
*/
|
|
136
|
-
export function context(options: ContextOptions = {}) {
|
|
137
|
-
const url = new URL(options.url ?? "/", "http://localhost");
|
|
138
|
-
const headers = new Headers(options.headers);
|
|
139
|
-
if (options.cookies) {
|
|
140
|
-
headers.set(
|
|
141
|
-
"cookie",
|
|
142
|
-
Object.entries(options.cookies)
|
|
143
|
-
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
|
|
144
|
-
.join("; "),
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
let body: BodyInit | undefined;
|
|
148
|
-
if (options.form) {
|
|
149
|
-
body = new URLSearchParams(options.form);
|
|
150
|
-
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
151
|
-
} else if (options.json !== undefined) {
|
|
152
|
-
body = JSON.stringify(options.json);
|
|
153
|
-
headers.set("content-type", "application/json");
|
|
154
|
-
}
|
|
155
|
-
const method = options.method ?? (body ? "POST" : "GET");
|
|
156
|
-
const request = new Request(url, { method, headers, body });
|
|
157
|
-
const cookies: Cookies & { toHeaders(): string[] } = createCookies(
|
|
158
|
-
headers.get("cookie") ?? "",
|
|
159
|
-
);
|
|
160
|
-
return {
|
|
161
|
-
request,
|
|
162
|
-
url,
|
|
163
|
-
params: options.params ?? {},
|
|
164
|
-
locals: (options.locals ?? {}) as App.Locals,
|
|
165
|
-
cookies,
|
|
166
|
-
fetch: options.fetch ?? globalThis.fetch,
|
|
167
|
-
parent: async () => options.parent ?? {},
|
|
168
|
-
};
|
|
169
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers for components and for the functions a route is made of.
|
|
3
|
+
*
|
|
4
|
+
* ```tsx
|
|
5
|
+
* import { expect, test } from "bun:test";
|
|
6
|
+
* import { render, click, tick } from "@sigil-dev/grimoire/testing";
|
|
7
|
+
* import { Counter } from "./Counter.tsx";
|
|
8
|
+
*
|
|
9
|
+
* test("counts", async () => {
|
|
10
|
+
* const { $, text } = render(Counter, { start: 1 });
|
|
11
|
+
* click($("button")!);
|
|
12
|
+
* await tick();
|
|
13
|
+
* expect(text()).toContain("2");
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Run with `sigil test`. Component tests need the DOM preload that command
|
|
18
|
+
* adds; for routes see `@sigil-dev/grimoire/testing/server`.
|
|
19
|
+
*/
|
|
20
|
+
import { insert, tick, withEffectScope } from "@sigil-dev/runtime";
|
|
21
|
+
import { createCookies } from "../server/cookie-utils";
|
|
22
|
+
import type { Cookies } from "../server/hooks";
|
|
23
|
+
|
|
24
|
+
export { tick };
|
|
25
|
+
|
|
26
|
+
export interface Rendered {
|
|
27
|
+
/** The element the component was rendered into. */
|
|
28
|
+
container: HTMLElement;
|
|
29
|
+
/** First element matching a selector inside the render. */
|
|
30
|
+
$(selector: string): HTMLElement | null;
|
|
31
|
+
/** Every element matching a selector inside the render. */
|
|
32
|
+
$$(selector: string): HTMLElement[];
|
|
33
|
+
/** The rendered text. */
|
|
34
|
+
text(): string;
|
|
35
|
+
/** The rendered markup. */
|
|
36
|
+
html(): string;
|
|
37
|
+
/** Remove it and stop its effects. Done for you after each test. */
|
|
38
|
+
unmount(): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const mounted = new Set<() => void>();
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Render a component into the document, the way a client-side navigation
|
|
45
|
+
* would: a fresh render, with effects live.
|
|
46
|
+
*
|
|
47
|
+
* Each call gets its own container, so one test can render several things.
|
|
48
|
+
*/
|
|
49
|
+
export function render<P extends object>(
|
|
50
|
+
Component: (props: P) => unknown,
|
|
51
|
+
props: P = {} as P,
|
|
52
|
+
): Rendered {
|
|
53
|
+
const container = document.createElement("div");
|
|
54
|
+
document.body.appendChild(container);
|
|
55
|
+
const stop = withEffectScope(() => {
|
|
56
|
+
insert(container, Component(props));
|
|
57
|
+
});
|
|
58
|
+
const unmount = () => {
|
|
59
|
+
stop();
|
|
60
|
+
container.remove();
|
|
61
|
+
mounted.delete(unmount);
|
|
62
|
+
};
|
|
63
|
+
mounted.add(unmount);
|
|
64
|
+
return {
|
|
65
|
+
container,
|
|
66
|
+
$: (selector) => container.querySelector<HTMLElement>(selector),
|
|
67
|
+
$$: (selector) =>
|
|
68
|
+
Array.from(container.querySelectorAll<HTMLElement>(selector)),
|
|
69
|
+
text: () => container.textContent ?? "",
|
|
70
|
+
html: () => container.innerHTML,
|
|
71
|
+
unmount,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Unmount everything rendered so far. The DOM preload runs this after each test. */
|
|
76
|
+
export function cleanup(): void {
|
|
77
|
+
for (const unmount of [...mounted]) unmount();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Click an element, with the bubbling a real click has. */
|
|
81
|
+
export function click(el: Element): void {
|
|
82
|
+
el.dispatchEvent(
|
|
83
|
+
new MouseEvent("click", { bubbles: true, cancelable: true }),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Type into an input or textarea: set its value and fire `input`, as a user would. */
|
|
88
|
+
export function type(el: Element, value: string): void {
|
|
89
|
+
(el as HTMLInputElement).value = value;
|
|
90
|
+
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Fire any event on an element. */
|
|
94
|
+
export function fire(el: Element, type: string, init: EventInit = {}): void {
|
|
95
|
+
el.dispatchEvent(
|
|
96
|
+
new Event(type, { bubbles: true, cancelable: true, ...init }),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
// the arguments of load functions and actions
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
export interface ContextOptions {
|
|
105
|
+
params?: Record<string, string>;
|
|
106
|
+
/** Path or URL. A path is resolved against `http://localhost`. */
|
|
107
|
+
url?: string;
|
|
108
|
+
method?: string;
|
|
109
|
+
headers?: Record<string, string>;
|
|
110
|
+
/** A form submission: fields are sent url-encoded. */
|
|
111
|
+
form?: Record<string, string>;
|
|
112
|
+
/** A JSON body. */
|
|
113
|
+
json?: unknown;
|
|
114
|
+
locals?: Record<string, unknown>;
|
|
115
|
+
/** Cookies the request carries. */
|
|
116
|
+
cookies?: Record<string, string>;
|
|
117
|
+
/** What `fetch` inside the function does. Defaults to the real one. */
|
|
118
|
+
fetch?: typeof fetch;
|
|
119
|
+
/** What `parent()` resolves to. */
|
|
120
|
+
parent?: Record<string, unknown>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export type TestContext = ReturnType<typeof context>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The argument a `load`, an action or a `+server.ts` handler receives, so the
|
|
127
|
+
* function can be called directly:
|
|
128
|
+
*
|
|
129
|
+
* ```ts
|
|
130
|
+
* const data = await load(context({ params: { id: "7" } }));
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* `cookies` is the real implementation; `cookies.toHeaders()` lists what the
|
|
134
|
+
* function set, for assertions.
|
|
135
|
+
*/
|
|
136
|
+
export function context(options: ContextOptions = {}) {
|
|
137
|
+
const url = new URL(options.url ?? "/", "http://localhost");
|
|
138
|
+
const headers = new Headers(options.headers);
|
|
139
|
+
if (options.cookies) {
|
|
140
|
+
headers.set(
|
|
141
|
+
"cookie",
|
|
142
|
+
Object.entries(options.cookies)
|
|
143
|
+
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
|
|
144
|
+
.join("; "),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
let body: BodyInit | undefined;
|
|
148
|
+
if (options.form) {
|
|
149
|
+
body = new URLSearchParams(options.form);
|
|
150
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
151
|
+
} else if (options.json !== undefined) {
|
|
152
|
+
body = JSON.stringify(options.json);
|
|
153
|
+
headers.set("content-type", "application/json");
|
|
154
|
+
}
|
|
155
|
+
const method = options.method ?? (body ? "POST" : "GET");
|
|
156
|
+
const request = new Request(url, { method, headers, body });
|
|
157
|
+
const cookies: Cookies & { toHeaders(): string[] } = createCookies(
|
|
158
|
+
headers.get("cookie") ?? "",
|
|
159
|
+
);
|
|
160
|
+
return {
|
|
161
|
+
request,
|
|
162
|
+
url,
|
|
163
|
+
params: options.params ?? {},
|
|
164
|
+
locals: (options.locals ?? {}) as App.Locals,
|
|
165
|
+
cookies,
|
|
166
|
+
fetch: options.fetch ?? globalThis.fetch,
|
|
167
|
+
parent: async () => options.parent ?? {},
|
|
168
|
+
};
|
|
169
|
+
}
|
package/src/testing/server.ts
CHANGED
package/src/typegen/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
existsSync,
|
|
2
3
|
mkdirSync,
|
|
3
4
|
readdirSync,
|
|
4
5
|
readFileSync,
|
|
@@ -7,6 +8,7 @@ import {
|
|
|
7
8
|
} from "node:fs";
|
|
8
9
|
import { mkdir } from "node:fs/promises";
|
|
9
10
|
import { dirname, isAbsolute, join, relative } from "node:path";
|
|
11
|
+
import { generateRouteTypes } from "../routing/route-ids.ts";
|
|
10
12
|
import type { RouteFile, RouteTree } from "../routing/scanner.ts";
|
|
11
13
|
|
|
12
14
|
export interface TypegenConfig {
|
|
@@ -358,7 +360,7 @@ function generateAmbient(): string {
|
|
|
358
360
|
"// Auto-generated by @sigil-dev/grimoire — do not edit",
|
|
359
361
|
"",
|
|
360
362
|
"declare namespace App {",
|
|
361
|
-
" //
|
|
363
|
+
" // Plugins in sigil.config.ts fill this (locals.d.ts); src/app.d.ts can add more",
|
|
362
364
|
" interface Locals extends Record<string, unknown> {}",
|
|
363
365
|
"}",
|
|
364
366
|
"",
|
|
@@ -422,6 +424,71 @@ function generateAmbient(): string {
|
|
|
422
424
|
].join("\n");
|
|
423
425
|
}
|
|
424
426
|
|
|
427
|
+
/**
|
|
428
|
+
* `App.Locals` from the plugins in sigil.config.ts, so a plugin that declares
|
|
429
|
+
* what it puts on `locals` (auth's `user`) is typed in every route with no
|
|
430
|
+
* app.d.ts. Needs the config to use `defineConfig`, which keeps the plugins'
|
|
431
|
+
* types; a plain object gives nothing, and an app.d.ts still adds to it.
|
|
432
|
+
*/
|
|
433
|
+
export function generateLocals(outDir: string, configFile: string): string {
|
|
434
|
+
let path = toImportPath(outDir, configFile);
|
|
435
|
+
if (!path.startsWith(".")) path = `./${path}`;
|
|
436
|
+
return [
|
|
437
|
+
"// Auto-generated by @sigil-dev/grimoire — do not edit",
|
|
438
|
+
'import type { PluginLocals } from "@sigil-dev/grimoire";',
|
|
439
|
+
`import type config from "${path}";`,
|
|
440
|
+
"",
|
|
441
|
+
"declare global {",
|
|
442
|
+
" namespace App {",
|
|
443
|
+
" interface Locals extends PluginLocals<typeof config> {}",
|
|
444
|
+
" }",
|
|
445
|
+
"}",
|
|
446
|
+
"",
|
|
447
|
+
"export {};",
|
|
448
|
+
"",
|
|
449
|
+
].join("\n");
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Tome's relation types from the tables the plugins bring. Only written when
|
|
454
|
+
* tome is installed: an augmentation of a missing module is an error.
|
|
455
|
+
*/
|
|
456
|
+
export function generateTables(outDir: string, configFile: string): string {
|
|
457
|
+
let path = toImportPath(outDir, configFile);
|
|
458
|
+
if (!path.startsWith(".")) path = `./${path}`;
|
|
459
|
+
return [
|
|
460
|
+
"// Auto-generated by @sigil-dev/grimoire — do not edit",
|
|
461
|
+
'import type { PluginTables } from "@sigil-dev/grimoire";',
|
|
462
|
+
`import type config from "${path}";`,
|
|
463
|
+
"",
|
|
464
|
+
'declare module "@sigil-dev/tome" {',
|
|
465
|
+
" interface Register {",
|
|
466
|
+
" pluginTables: PluginTables<typeof config>;",
|
|
467
|
+
" }",
|
|
468
|
+
"}",
|
|
469
|
+
"",
|
|
470
|
+
"export {};",
|
|
471
|
+
"",
|
|
472
|
+
].join("\n");
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function hasTome(projectRoot: string): boolean {
|
|
476
|
+
try {
|
|
477
|
+
Bun.resolveSync("@sigil-dev/tome", projectRoot);
|
|
478
|
+
return true;
|
|
479
|
+
} catch {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function runtimeEntry(): string | undefined {
|
|
485
|
+
try {
|
|
486
|
+
return Bun.resolveSync("@sigil-dev/runtime", import.meta.dir);
|
|
487
|
+
} catch {
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
425
492
|
function generateTsConfig(
|
|
426
493
|
aliases: Record<string, string>,
|
|
427
494
|
grimoireDir: string,
|
|
@@ -429,10 +496,23 @@ function generateTsConfig(
|
|
|
429
496
|
// paths without a baseUrl resolve against this file's directory
|
|
430
497
|
const paths: Record<string, string[]> = {};
|
|
431
498
|
for (const [prefix, dir] of Object.entries(aliases)) {
|
|
432
|
-
|
|
499
|
+
let rel = relative(grimoireDir, dir).replace(/\\/g, "/");
|
|
500
|
+
// inside .grimoire ($db, $i18n) the path has no ../ in front, and TS
|
|
501
|
+
// only accepts a path without baseUrl when it starts with ./ or ../
|
|
502
|
+
if (!rel.startsWith(".")) rel = `./${rel}`;
|
|
433
503
|
paths[prefix] = [rel];
|
|
434
504
|
paths[`${prefix}/*`] = [`${rel}/*`];
|
|
435
505
|
}
|
|
506
|
+
// An app imports the runtime (createContext, Boundary) without depending
|
|
507
|
+
// on it: the server and the browser both run grimoire's copy, so the
|
|
508
|
+
// editor reads that copy's types too. Regenerated on every build, so a
|
|
509
|
+
// reinstall that moves it is picked up.
|
|
510
|
+
const runtime = runtimeEntry();
|
|
511
|
+
if (runtime) {
|
|
512
|
+
paths["@sigil-dev/runtime"] = [
|
|
513
|
+
relative(grimoireDir, runtime).replace(/\\/g, "/"),
|
|
514
|
+
];
|
|
515
|
+
}
|
|
436
516
|
return JSON.stringify(
|
|
437
517
|
{
|
|
438
518
|
compilerOptions: {
|
|
@@ -492,6 +572,21 @@ export async function generateTypes(
|
|
|
492
572
|
}
|
|
493
573
|
|
|
494
574
|
writeFileSync(join(outDir, "ambient.d.ts"), generateAmbient(), "utf-8");
|
|
575
|
+
writeFileSync(join(outDir, "routes.d.ts"), generateRouteTypes(tree), "utf-8");
|
|
576
|
+
|
|
577
|
+
const configFile = join(projectRoot, "sigil.config.ts");
|
|
578
|
+
const localsFile = join(outDir, "locals.d.ts");
|
|
579
|
+
if (existsSync(configFile)) {
|
|
580
|
+
writeFileSync(localsFile, generateLocals(outDir, configFile), "utf-8");
|
|
581
|
+
} else {
|
|
582
|
+
rmSync(localsFile, { force: true });
|
|
583
|
+
}
|
|
584
|
+
const tablesFile = join(outDir, "tables.d.ts");
|
|
585
|
+
if (existsSync(configFile) && hasTome(projectRoot)) {
|
|
586
|
+
writeFileSync(tablesFile, generateTables(outDir, configFile), "utf-8");
|
|
587
|
+
} else {
|
|
588
|
+
rmSync(tablesFile, { force: true });
|
|
589
|
+
}
|
|
495
590
|
|
|
496
591
|
const grimoireDir = dirname(outDir);
|
|
497
592
|
writeFileSync(
|