UNPKG

@cloudflare/vitest-pool-workers

Version:

Workers Vitest integration for writing Vitest unit and integration tests that run inside the Workers runtime

1 lines 10.5 kB
{"version":3,"file":"vitest-v3-to-v4.mjs","names":[],"sources":["../../src/codemods/vitest-v3-to-v4.ts"],"sourcesContent":["/**\n * jscodeshift codemod: migrate @cloudflare/vitest-pool-workers config from v3 to v4.\n *\n * Transforms:\n * import { defineWorkersProject } from \"@cloudflare/vitest-pool-workers/config\";\n * export default defineWorkersProject({ test: { poolOptions: { workers: { ... } } } });\n *\n * Into:\n * import { cloudflareTest } from \"@cloudflare/vitest-pool-workers\";\n * import { defineConfig } from \"vitest/config\";\n * export default defineConfig({ plugins: [cloudflareTest({ ... })], test: { ... } });\n *\n * Usage:\n * npx jscodeshift -t node_modules/@cloudflare/vitest-pool-workers/dist/codemods/vitest-v3-to-v4.mjs vitest.config.ts\n */\n\n// Minimal jscodeshift types — avoids requiring @types/jscodeshift as a dependency.\ninterface FileInfo {\n\tpath: string;\n\tsource: string;\n}\ninterface JSCodeshift {\n\t(source: string): Collection;\n\tImportDeclaration: ASTType;\n\tCallExpression: ASTType;\n\tObjectExpression: { check(node: unknown): node is ObjectExpressionNode };\n\tObjectProperty: { check(node: unknown): node is PropertyNode };\n\tProperty: { check(node: unknown): node is PropertyNode };\n\tIdentifier: { check(node: unknown): node is IdentifierNode };\n\tFunctionExpression: { check(node: unknown): boolean };\n\tArrowFunctionExpression: { check(node: unknown): boolean };\n\tArrayExpression: { check(node: unknown): node is ArrayExpressionNode };\n\timportDeclaration(\n\t\tspecifiers: ImportSpecifierNode[],\n\t\tsource: LiteralNode\n\t): ImportDeclarationNode;\n\timportSpecifier(\n\t\timported: IdentifierNode,\n\t\tlocal?: IdentifierNode\n\t): ImportSpecifierNode;\n\tidentifier(name: string): IdentifierNode;\n\tstringLiteral(value: string): LiteralNode;\n\tcallExpression(\n\t\tcallee: IdentifierNode,\n\t\targs: ExpressionNode[]\n\t): CallExpressionNode;\n\tarrayExpression(elements: ExpressionNode[]): ArrayExpressionNode;\n\tobjectProperty(key: IdentifierNode, value: ExpressionNode): PropertyNode;\n}\n\ninterface ASTType {\n\tname: string;\n}\n\ninterface ASTNode {\n\ttype: string;\n}\n\ninterface IdentifierNode extends ASTNode {\n\ttype: \"Identifier\";\n\tname: string;\n}\n\ninterface LiteralNode extends ASTNode {\n\tvalue: string;\n}\n\ninterface ImportSpecifierNode extends ASTNode {\n\ttype: \"ImportSpecifier\";\n\timported: IdentifierNode;\n\tlocal?: IdentifierNode;\n}\n\ninterface ImportDeclarationNode extends ASTNode {\n\ttype: \"ImportDeclaration\";\n\tsource: LiteralNode;\n\tspecifiers: ImportSpecifierNode[];\n}\n\ninterface PropertyNode extends ASTNode {\n\tkey: ASTNode;\n\tvalue: ASTNode;\n}\n\ninterface ObjectExpressionNode extends ASTNode {\n\ttype: \"ObjectExpression\";\n\tproperties: PropertyNode[];\n}\n\ninterface ArrayExpressionNode extends ASTNode {\n\ttype: \"ArrayExpression\";\n\telements: ExpressionNode[];\n}\n\ninterface CallExpressionNode extends ASTNode {\n\ttype: \"CallExpression\";\n\tcallee: ASTNode;\n\targuments: ASTNode[];\n}\n\ntype ExpressionNode = ASTNode;\n\ninterface NodePath<T = ASTNode> {\n\tnode: T;\n\tparent: NodePath;\n\tinsertAfter(node: ASTNode): void;\n}\n\ninterface Collection {\n\tfind(type: ASTType, filter?: Record<string, unknown>): Collection;\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any -- jscodeshift Collection.forEach accepts callbacks with narrowed NodePath types\n\tforEach(callback: (path: NodePath<any>) => void): Collection;\n\tat(index: number): Collection;\n\tpaths(): NodePath[];\n\tlength: number;\n\ttoSource(): string;\n}\n\ninterface API {\n\tjscodeshift: JSCodeshift;\n}\n\nfunction isNamedProp(\n\tj: JSCodeshift,\n\tprop: ASTNode,\n\tname: string\n): prop is PropertyNode {\n\treturn (\n\t\t(j.Property.check(prop) || j.ObjectProperty.check(prop)) &&\n\t\tj.Identifier.check(prop.key) &&\n\t\t(prop.key as IdentifierNode).name === name\n\t);\n}\n\nfunction findNamedProp(\n\tj: JSCodeshift,\n\tproperties: PropertyNode[],\n\tname: string\n): PropertyNode | undefined {\n\treturn properties.find((prop) => isNamedProp(j, prop, name));\n}\n\nexport default function transform(fileInfo: FileInfo, api: API): string {\n\tconst j = api.jscodeshift;\n\tconst root = j(fileInfo.source);\n\n\t// 1. Find the import of @cloudflare/vitest-pool-workers/config with defineWorkersProject\n\tconst configImports = root.find(j.ImportDeclaration, {\n\t\tsource: { value: \"@cloudflare/vitest-pool-workers/config\" },\n\t});\n\n\tif (configImports.length === 0) {\n\t\t// Nothing to transform\n\t\treturn fileInfo.source;\n\t}\n\n\t// Update the import: change source and swap defineWorkersProject → cloudflareTest\n\tconfigImports.forEach((path: NodePath<ImportDeclarationNode>) => {\n\t\tpath.node.source.value = \"@cloudflare/vitest-pool-workers\";\n\t\tpath.node.specifiers = [\n\t\t\tj.importSpecifier(j.identifier(\"cloudflareTest\")),\n\t\t\t...(path.node.specifiers?.filter(\n\t\t\t\t(s) =>\n\t\t\t\t\t!(\n\t\t\t\t\t\ts.type === \"ImportSpecifier\" &&\n\t\t\t\t\t\ts.imported?.name === \"defineWorkersProject\"\n\t\t\t\t\t)\n\t\t\t) ?? []),\n\t\t];\n\t});\n\n\t// 2. Add `import { defineConfig } from \"vitest/config\"` after the last import\n\tconst allImports = root.find(j.ImportDeclaration);\n\tif (allImports.length > 0) {\n\t\tconst lastImport = allImports.at(-1);\n\t\tlastImport.forEach((path: NodePath) => {\n\t\t\tpath.insertAfter(\n\t\t\t\tj.importDeclaration(\n\t\t\t\t\t[j.importSpecifier(j.identifier(\"defineConfig\"))],\n\t\t\t\t\tj.stringLiteral(\"vitest/config\")\n\t\t\t\t)\n\t\t\t);\n\t\t});\n\t}\n\n\t// 3. Transform defineWorkersProject() → defineConfig() with plugins\n\troot\n\t\t.find(j.CallExpression, {\n\t\t\tcallee: { name: \"defineWorkersProject\" },\n\t\t})\n\t\t.forEach((path: NodePath<CallExpressionNode>) => {\n\t\t\t// Rename callee\n\t\t\t(path.node.callee as IdentifierNode).name = \"defineConfig\";\n\n\t\t\tconst config = path.node.arguments[0];\n\t\t\tif (!j.ObjectExpression.check(config)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"defineWorkersProject() is called with a function and not an object, \" +\n\t\t\t\t\t\t\"and so is too complex to apply a codemod to. \" +\n\t\t\t\t\t\t\"Please refer to the migration docs to perform the migration manually.\"\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Find test.poolOptions.workers\n\t\t\tconst testProp = findNamedProp(j, config.properties, \"test\");\n\t\t\tif (!testProp || !j.ObjectExpression.check(testProp.value)) {\n\t\t\t\tthrow new Error(\"Could not find `test` property in config\");\n\t\t\t}\n\t\t\tconst testObj = testProp.value as ObjectExpressionNode;\n\n\t\t\tconst poolOptionsProp = findNamedProp(\n\t\t\t\tj,\n\t\t\t\ttestObj.properties,\n\t\t\t\t\"poolOptions\"\n\t\t\t);\n\t\t\tif (\n\t\t\t\t!poolOptionsProp ||\n\t\t\t\t!j.ObjectExpression.check(poolOptionsProp.value)\n\t\t\t) {\n\t\t\t\tthrow new Error(\"Could not find `test.poolOptions` property in config\");\n\t\t\t}\n\t\t\tconst poolOptionsObj = poolOptionsProp.value as ObjectExpressionNode;\n\n\t\t\tconst workersProp = findNamedProp(\n\t\t\t\tj,\n\t\t\t\tpoolOptionsObj.properties,\n\t\t\t\t\"workers\"\n\t\t\t);\n\t\t\tif (\n\t\t\t\t!workersProp ||\n\t\t\t\t!(\n\t\t\t\t\tj.ObjectExpression.check(workersProp.value) ||\n\t\t\t\t\tj.FunctionExpression.check(workersProp.value) ||\n\t\t\t\t\tj.ArrowFunctionExpression.check(workersProp.value)\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Could not find `test.poolOptions.workers` property in config\"\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Create plugins: [cloudflareTest(<workers value>)]\n\t\t\tconst pluginCall = j.callExpression(j.identifier(\"cloudflareTest\"), [\n\t\t\t\tworkersProp.value as ExpressionNode,\n\t\t\t]);\n\n\t\t\tconst pluginsProp = findNamedProp(j, config.properties, \"plugins\");\n\t\t\tif (pluginsProp && j.ArrayExpression.check(pluginsProp.value)) {\n\t\t\t\t// Prepend to existing plugins array\n\t\t\t\t(pluginsProp.value as ArrayExpressionNode).elements.unshift(pluginCall);\n\t\t\t} else {\n\t\t\t\t// Create new plugins property at the start\n\t\t\t\tconfig.properties.unshift(\n\t\t\t\t\tj.objectProperty(\n\t\t\t\t\t\tj.identifier(\"plugins\"),\n\t\t\t\t\t\tj.arrayExpression([pluginCall])\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Remove poolOptions from test\n\t\t\ttestObj.properties = testObj.properties.filter(\n\t\t\t\t(prop) => !isNamedProp(j, prop, \"poolOptions\")\n\t\t\t);\n\t\t});\n\n\treturn root.toSource();\n}\n\n// Tell jscodeshift to use the TypeScript parser\nexport const parser = \"ts\";\n"],"mappings":";AA0HA,SAAS,YACR,GACA,MACA,MACuB;AACvB,SACE,EAAE,SAAS,MAAM,KAAK,IAAI,EAAE,eAAe,MAAM,KAAK,KACvD,EAAE,WAAW,MAAM,KAAK,IAAI,IAC3B,KAAK,IAAuB,SAAS;;AAIxC,SAAS,cACR,GACA,YACA,MAC2B;AAC3B,QAAO,WAAW,MAAM,SAAS,YAAY,GAAG,MAAM,KAAK,CAAC;;AAG7D,SAAwB,UAAU,UAAoB,KAAkB;CACvE,MAAM,IAAI,IAAI;CACd,MAAM,OAAO,EAAE,SAAS,OAAO;CAG/B,MAAM,gBAAgB,KAAK,KAAK,EAAE,mBAAmB,EACpD,QAAQ,EAAE,OAAO,0CAA0C,EAC3D,CAAC;AAEF,KAAI,cAAc,WAAW,EAE5B,QAAO,SAAS;AAIjB,eAAc,SAAS,SAA0C;AAChE,OAAK,KAAK,OAAO,QAAQ;AACzB,OAAK,K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