forked from filipinascimento/helios-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite-web-test-runner-plugin.mjs
55 lines (46 loc) · 2.05 KB
/
vite-web-test-runner-plugin.mjs
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
// vite-web-test-runner-plugin.js
import { createServer } from 'vite';
import { join, relative } from 'node:path';
import koaConnect from 'koa-connect';
import minimatch from 'minimatch';
/** @typedef { import('@web/test-runner').TestRunnerPlugin } RunnerPlugin */
/**
* @return { RunnerPlugin }
*/
export default function viteWebTestRunnerPlugin({ testMatch, testRoot = process.cwd(), viteConfigFile } = {}) {
let viteServer;
let viteRoot;
let relativeRoot;
return {
name: 'vite-wtr-plugin',
async serverStart({ app, fileWatcher }) {
viteServer = await createServer({
clearScreen: true,
server: { middlewareMode: true, hmr: false },
appType: 'custom',
configFile: viteConfigFile || join(testRoot, 'vite.config.ts')
});
viteRoot = viteServer.config.root;
// This path represents the diff beween the test root and the vite root
// viteRoot should always be a relative path from the root (ex. testRoot: /root/test vs viteRoot: /root/test/src/module)
relativeRoot = `/${relative(testRoot, viteRoot)}`;
// Allow vite to take over most requests
app.use(koaConnect(viteServer.middlewares));
// Vite is taking over the handling of URLs, hence we need to forward the watching to the runner
viteServer.watcher.on('change', (...args) => fileWatcher.emit('change', ...args));
},
async transformImport({ source }) {
const [absSource, ] = source.split('?'); // Remove the queryString otherwise vite will fail resolving
const relativeSource = absSource.at(0) === '/' ? absSource.substring(1) : absSource;
for (const match of testMatch) {
if (minimatch(relativeSource, match)) {
const newPath = absSource.replace(relativeRoot, '');
return newPath;
}
}
},
async serverStop() {
return viteServer.close();
},
}
}