Repository navigation
Expand file tree
/
Copy pathvitest.config.mjs
More file actions
142 lines (131 loc) · 4.4 KB
/
Copy pathvitest.config.mjs
File metadata and controls
142 lines (131 loc) · 4.4 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { playwright } from "@vitest/browser-playwright";
import { defineConfig } from "vitest/config";
function serveFiles(name, pattern, toPath, headers = {}) {
return {
name,
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
const match = req.url.match(pattern);
if (!match) {
return next();
}
try {
const body = await readFile(new URL(toPath(match[1]), import.meta.url));
for (const [header, value] of Object.entries(headers)) {
res.setHeader(header, value);
}
res.end(body);
} catch {
res.statusCode = 404;
res.end("not found");
}
});
},
};
}
// The suite requests fixtures by several relative paths (ajax/x, test/ajax/x).
const ajaxFixtures = serveFiles(
"ajax-fixtures",
/\/ajax\/([\w.-]+)/,
(file) => `test/ajax/${file}`
);
// Iframe documents are only parsed as XML when served with an XML content type.
const ajaxXmlFixtures = serveFiles(
"ajax-xml-fixtures",
/\/ajax\/([\w.-]+\.xml)$/,
(file) => `test/ajax/${file}`,
{ "Content-Type": "application/xml" }
);
// A real server labels HTML (iframe fixtures stay unlabeled so the browser sniffs them); without it jQuery 1.7.2 hands back an XMLDocument for the response.
const ajaxHtmlFixtures = serveFiles(
"ajax-html-fixtures",
/\/ajax\/((?!iframe-)[\w.-]+\.html)(\?.*)?$/,
(file) => `test/ajax/${file}`,
{ "Content-Type": "text/html" }
);
// Surface pages must load files as consumers would, without Vite rewriting bare imports.
const rawFiles = serveFiles(
"raw-files",
/^\/raw\/((?:dist|node_modules)\/[\w./@-]+\.m?js)$/,
(file) => file,
{ "Content-Type": "text/javascript" }
);
const jqueryEsm = new URL("./test/jquery-esm.js", import.meta.url);
// The matrix points JQUERY_FILE at another classic jQuery build (see scripts/fetch-jquery.sh).
const jqueryFile = fileURLToPath(
new URL(
process.env.JQUERY_FILE || "./node_modules/jquery/dist/jquery.js",
`file://${process.cwd()}/`
)
);
// jQuery builds are classic scripts that set window.jQuery. Appending an export gives the suite a
// default import without Vite's CommonJS interop, for every version in the matrix.
const jqueryAsModule = {
name: "jquery-as-module",
enforce: "pre",
transform(code, id) {
if (id.split("?")[0] === jqueryFile) {
return { code: `${code}\nexport default window.jQuery;\n`, map: null };
}
},
};
// The suite runs on Chromium and WebKit, as instances named suite-chromium and suite-webkit.
// suite-firefox is added only with JQUERY_FORM_FIREFOX=1 (scripts/test-docker.sh runs it in Docker):
// Playwright's Firefox build does not start on the maintainer's macOS, so npm test leaves it out.
// V8 coverage only exists in Chromium, so npm test runs suite-webkit in a second run.
const suiteBrowsers = ["chromium", "webkit"];
if (process.env.JQUERY_FORM_FIREFOX === "1") {
suiteBrowsers.push("firefox");
}
const browser = (names, project) => ({
enabled: true,
headless: true,
provider: playwright(),
instances: names.map((name) => ({ browser: name, name: project && `${project}-${name}` })),
});
export default defineConfig({
test: {
coverage: {
provider: "v8",
include: ["src/**/*.js"],
reporter: ["text-summary", "html"],
thresholds: {
100: true,
},
},
projects: [
{
plugins: [jqueryAsModule, ajaxXmlFixtures, ajaxHtmlFixtures, ajaxFixtures],
test: {
name: "suite",
// Keeps Vite's CommonJS interop wrapper out of the plugin's coverage.
alias: [
{ find: /^jquery$/, replacement: fileURLToPath(jqueryEsm) },
{ find: /^jquery\/dist\/jquery\.js$/, replacement: jqueryFile },
],
globals: true,
include: ["test/suite/**/*.test.js"],
setupFiles: ["test/setup.js"],
browser: browser(suiteBrowsers, "suite"),
},
},
{
plugins: [rawFiles],
test: {
name: "surfaces",
include: ["test/surfaces/browser.test.js"],
browser: browser(["chromium"]),
},
},
{
test: {
name: "commonjs",
include: ["test/surfaces/commonjs.test.js", "test/surfaces/manifests.test.js"],
environment: "node",
},
},
],
},
});