Fix tests with node 18 (#5528)

This commit is contained in:
Bjorn Lu 2022-12-06 22:11:43 +08:00 committed by GitHub
parent 0a62ecd894
commit e97fa9c01e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -70,14 +70,6 @@ export async function loadFixture(inlineConfig) {
if (!inlineConfig || !inlineConfig.root) if (!inlineConfig || !inlineConfig.root)
throw new Error("Must provide { root: './fixtures/...' }"); throw new Error("Must provide { root: './fixtures/...' }");
// Compatible with different Node versions (https://vitejs.dev/guide/migration.html#dev-server-changes)
// TODO: Remove this to test in Node >= 17 where the dns resolver is verbatim
if (!inlineConfig?.server) {
inlineConfig.server = {
host: '127.0.0.1',
};
}
// load config // load config
let cwd = inlineConfig.root; let cwd = inlineConfig.root;
delete inlineConfig.root; delete inlineConfig.root;
@ -118,7 +110,7 @@ export async function loadFixture(inlineConfig) {
}; };
const resolveUrl = (url) => const resolveUrl = (url) =>
`http://${'127.0.0.1'}:${config.server.port}${url.replace(/^\/?/, '/')}`; `http://${config.server.host}:${config.server.port}${url.replace(/^\/?/, '/')}`;
// A map of files that have been editted. // A map of files that have been editted.
let fileEdits = new Map(); let fileEdits = new Map();
@ -149,6 +141,7 @@ export async function loadFixture(inlineConfig) {
build: (opts = {}) => build(settings, { logging, telemetry, ...opts }), build: (opts = {}) => build(settings, { logging, telemetry, ...opts }),
startDevServer: async (opts = {}) => { startDevServer: async (opts = {}) => {
devServer = await dev(settings, { logging, telemetry, ...opts }); devServer = await dev(settings, { logging, telemetry, ...opts });
config.server.host = parseAddressToHost(devServer.address.address); // update host
config.server.port = devServer.address.port; // update port config.server.port = devServer.address.port; // update port
return devServer; return devServer;
}, },
@ -157,6 +150,8 @@ export async function loadFixture(inlineConfig) {
fetch: (url, init) => fetch(resolveUrl(url), init), fetch: (url, init) => fetch(resolveUrl(url), init),
preview: async (opts = {}) => { preview: async (opts = {}) => {
const previewServer = await preview(settings, { logging, telemetry, ...opts }); const previewServer = await preview(settings, { logging, telemetry, ...opts });
config.server.host = parseAddressToHost(previewServer.host); // update host
config.server.port = previewServer.port; // update port
return previewServer; return previewServer;
}, },
pathExists: (p) => fs.existsSync(new URL(p.replace(/^\//, ''), config.outDir)), pathExists: (p) => fs.existsSync(new URL(p.replace(/^\//, ''), config.outDir)),
@ -201,6 +196,16 @@ export async function loadFixture(inlineConfig) {
}; };
} }
/**
* @param {string} [address]
*/
function parseAddressToHost(address) {
if (address?.startsWith('::')) {
return `[${address}]`;
}
return address;
}
/** /**
* Basic object merge utility. Returns new copy of merged Object. * Basic object merge utility. Returns new copy of merged Object.
* @param {Object} a * @param {Object} a