/
vite-adapter-test.ts
125 lines (112 loc) · 3.47 KB
/
vite-adapter-test.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as fs from "node:fs";
import * as path from "node:path";
import { test, expect } from "@playwright/test";
import { normalizePath } from "vite";
import getPort from "get-port";
import {
createProject,
viteDev,
viteBuild,
VITE_CONFIG,
} from "./helpers/vite.js";
test.describe(async () => {
let port: number;
let cwd: string;
let stop: () => void;
function pathStartsWithCwd(pathname: string) {
return normalizePath(pathname).startsWith(normalizePath(cwd));
}
function relativeToCwd(pathname: string) {
return normalizePath(path.relative(cwd, pathname));
}
test.beforeAll(async () => {
port = await getPort();
cwd = await createProject({
"vite.config.ts": await VITE_CONFIG({
port,
pluginOptions: `
{
adapter: async ({ remixConfig }) => ({
serverBundles(...args) {
// This lets us assert that user options are passed to adapter options hook
return remixConfig.serverBundles?.(...args) + "--adapter-options";
},
async buildEnd(args) {
let fs = await import("node:fs/promises");
await fs.writeFile(
"BUILD_END_ARGS.json",
JSON.stringify(
args,
function replacer(key, value) {
return typeof value === "function"
? value.toString()
: value;
},
2,
),
"utf-8");
}
}),
serverBundles() {
return "user-options";
}
},
`,
}),
});
stop = await viteDev({ cwd, port });
});
test.afterAll(() => stop());
test("Vite / adapter / serverBundles and buildEnd hooks", async () => {
let { status } = viteBuild({ cwd });
expect(status).toBe(0);
let buildEndArgs: any = JSON.parse(
fs.readFileSync(path.join(cwd, "BUILD_END_ARGS.json"), "utf8")
);
let { remixConfig } = buildEndArgs;
// Before rewriting to relative paths, assert that paths are absolute within cwd
expect(pathStartsWithCwd(remixConfig.buildDirectory)).toBe(true);
// Rewrite path args to be relative and normalized for snapshot test
remixConfig.buildDirectory = relativeToCwd(remixConfig.buildDirectory);
expect(Object.keys(buildEndArgs)).toEqual(["buildManifest", "remixConfig"]);
// Smoke test the resolved config
expect(Object.keys(buildEndArgs.remixConfig)).toEqual([
"adapter",
"appDirectory",
"buildDirectory",
"future",
"manifest",
"publicPath",
"routes",
"serverBuildFile",
"serverBundles",
"serverModuleFormat",
"unstable_ssr",
]);
// Ensure we get a valid build manifest
expect(buildEndArgs.buildManifest).toEqual({
routeIdToServerBundleId: {
"routes/_index": "user-options--adapter-options",
},
routes: {
root: {
file: "app/root.tsx",
id: "root",
path: "",
},
"routes/_index": {
file: "app/routes/_index.tsx",
id: "routes/_index",
index: true,
parentId: "root",
},
},
serverBundles: {
"user-options--adapter-options": {
file: "build/server/user-options--adapter-options/index.js",
id: "user-options--adapter-options",
},
},
});
});
});