@cloudflare/vitest-pool-workers
Version:
Workers Vitest integration for writing Vitest unit and integration tests that run inside the Workers runtime
45 lines (44 loc) • 3.03 kB
JavaScript
//#region src/codemods/vitest-v3-to-v4.ts
function isNamedProp(j, prop, name) {
return (j.Property.check(prop) || j.ObjectProperty.check(prop)) && j.Identifier.check(prop.key) && prop.key.name === name;
}
function findNamedProp(j, properties, name) {
return properties.find((prop) => isNamedProp(j, prop, name));
}
function transform(fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);
const configImports = root.find(j.ImportDeclaration, { source: { value: "@cloudflare/vitest-pool-workers/config" } });
if (configImports.length === 0) return fileInfo.source;
configImports.forEach((path) => {
path.node.source.value = "@cloudflare/vitest-pool-workers";
path.node.specifiers = [j.importSpecifier(j.identifier("cloudflareTest")), ...path.node.specifiers?.filter((s) => !(s.type === "ImportSpecifier" && s.imported?.name === "defineWorkersProject")) ?? []];
});
const allImports = root.find(j.ImportDeclaration);
if (allImports.length > 0) allImports.at(-1).forEach((path) => {
path.insertAfter(j.importDeclaration([j.importSpecifier(j.identifier("defineConfig"))], j.stringLiteral("vitest/config")));
});
root.find(j.CallExpression, { callee: { name: "defineWorkersProject" } }).forEach((path) => {
path.node.callee.name = "defineConfig";
const config = path.node.arguments[0];
if (!j.ObjectExpression.check(config)) throw new Error("defineWorkersProject() is called with a function and not an object, and so is too complex to apply a codemod to. Please refer to the migration docs to perform the migration manually.");
const testProp = findNamedProp(j, config.properties, "test");
if (!testProp || !j.ObjectExpression.check(testProp.value)) throw new Error("Could not find `test` property in config");
const testObj = testProp.value;
const poolOptionsProp = findNamedProp(j, testObj.properties, "poolOptions");
if (!poolOptionsProp || !j.ObjectExpression.check(poolOptionsProp.value)) throw new Error("Could not find `test.poolOptions` property in config");
const poolOptionsObj = poolOptionsProp.value;
const workersProp = findNamedProp(j, poolOptionsObj.properties, "workers");
if (!workersProp || !(j.ObjectExpression.check(workersProp.value) || j.FunctionExpression.check(workersProp.value) || j.ArrowFunctionExpression.check(workersProp.value))) throw new Error("Could not find `test.poolOptions.workers` property in config");
const pluginCall = j.callExpression(j.identifier("cloudflareTest"), [workersProp.value]);
const pluginsProp = findNamedProp(j, config.properties, "plugins");
if (pluginsProp && j.ArrayExpression.check(pluginsProp.value)) pluginsProp.value.elements.unshift(pluginCall);
else config.properties.unshift(j.objectProperty(j.identifier("plugins"), j.arrayExpression([pluginCall])));
testObj.properties = testObj.properties.filter((prop) => !isNamedProp(j, prop, "poolOptions"));
});
return root.toSource();
}
const parser = "ts";
//#endregion
export { transform as default, parser };
//# sourceMappingURL=vitest-v3-to-v4.mjs.map