|
| 1 | +/* eslint-disable local-rules/no-uncategorized-errors */ |
| 2 | +import { types as t } from 'storybook/internal/babel'; |
| 3 | +import { getStoryTitle } from 'storybook/internal/common'; |
| 4 | +import type { StoriesEntry } from 'storybook/internal/types'; |
| 5 | + |
| 6 | +import { dedent } from 'ts-dedent'; |
| 7 | + |
| 8 | +import { formatCsf, loadCsf } from '../CsfFile'; |
| 9 | + |
| 10 | +const logger = console; |
| 11 | + |
| 12 | +export async function testTransform({ |
| 13 | + code, |
| 14 | + fileName, |
| 15 | + configDir, |
| 16 | + stories, |
| 17 | +}: { |
| 18 | + code: string; |
| 19 | + fileName: string; |
| 20 | + configDir: string; |
| 21 | + stories: StoriesEntry[]; |
| 22 | +}): Promise<ReturnType<typeof formatCsf>> { |
| 23 | + const isStoryFile = /\.stor(y|ies)\./.test(fileName); |
| 24 | + if (!isStoryFile) { |
| 25 | + return code; |
| 26 | + } |
| 27 | + |
| 28 | + const parsed = loadCsf(code, { |
| 29 | + fileName, |
| 30 | + transformInlineMeta: true, |
| 31 | + makeTitle: (title) => { |
| 32 | + const result = |
| 33 | + getStoryTitle({ |
| 34 | + storyFilePath: fileName, |
| 35 | + configDir, |
| 36 | + stories, |
| 37 | + userTitle: title, |
| 38 | + }) || 'unknown'; |
| 39 | + |
| 40 | + if (result === 'unknown') { |
| 41 | + logger.warn( |
| 42 | + dedent` |
| 43 | + [Storybook]: Could not calculate story title for "${fileName}". |
| 44 | + Please make sure that this file matches the globs included in the "stories" field in your Storybook configuration at "${configDir}". |
| 45 | + ` |
| 46 | + ); |
| 47 | + } |
| 48 | + return result; |
| 49 | + }, |
| 50 | + }).parse(); |
| 51 | + |
| 52 | + const ast = parsed._ast; |
| 53 | + |
| 54 | + const metaNode = parsed._metaNode as t.ObjectExpression; |
| 55 | + |
| 56 | + const metaTitleProperty = metaNode.properties.find( |
| 57 | + (prop) => t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === 'title' |
| 58 | + ); |
| 59 | + |
| 60 | + const metaTitle = t.stringLiteral(parsed._meta?.title || 'unknown'); |
| 61 | + if (!metaTitleProperty) { |
| 62 | + metaNode.properties.push(t.objectProperty(t.identifier('title'), metaTitle)); |
| 63 | + } else if (t.isObjectProperty(metaTitleProperty)) { |
| 64 | + // If the title is present in meta, overwrite it because autotitle can still affect existing titles |
| 65 | + metaTitleProperty.value = metaTitle; |
| 66 | + } |
| 67 | + |
| 68 | + if (!metaNode || !parsed._meta) { |
| 69 | + throw new Error( |
| 70 | + 'Storybook could not detect the meta (default export) object in the story file. \n\nPlease make sure you have a default export with the meta object. If you are using a different export format that is not supported, please file an issue with details about your use case.' |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + // Generate new story exports from tests attached to stories |
| 75 | + const newExports: t.ExportNamedDeclaration[] = []; |
| 76 | + let testCounter = 1; |
| 77 | + |
| 78 | + // Track nodes to remove from the AST |
| 79 | + const nodesToRemove: t.Node[] = []; |
| 80 | + |
| 81 | + // Process each story to find attached tests |
| 82 | + Object.entries(parsed._stories).forEach(([storyExportName, storyInfo]) => { |
| 83 | + // Find all test calls on this story in the AST |
| 84 | + ast.program.body.forEach((node) => { |
| 85 | + if (!t.isExpressionStatement(node)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const { expression } = node; |
| 90 | + |
| 91 | + if (!t.isCallExpression(expression)) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const { callee, arguments: args } = expression; |
| 96 | + |
| 97 | + // Check if it's a call like StoryName.test() |
| 98 | + if ( |
| 99 | + t.isMemberExpression(callee) && |
| 100 | + t.isIdentifier(callee.object) && |
| 101 | + callee.object.name === storyExportName && |
| 102 | + t.isIdentifier(callee.property) && |
| 103 | + callee.property.name === 'test' && |
| 104 | + args.length >= 2 && |
| 105 | + t.isStringLiteral(args[0]) && |
| 106 | + (t.isFunctionExpression(args[1]) || t.isArrowFunctionExpression(args[1])) |
| 107 | + ) { |
| 108 | + // Get test name and body |
| 109 | + const testName = (args[0] as t.StringLiteral).value; |
| 110 | + const testFunction = args[1] as t.FunctionExpression | t.ArrowFunctionExpression; |
| 111 | + |
| 112 | + // Create unique export name for the test story |
| 113 | + const testExportName = `_test${testCounter > 1 ? testCounter : ''}`; |
| 114 | + testCounter++; |
| 115 | + |
| 116 | + // Create a new story object with the test function integrated as play function |
| 117 | + const newStoryObject = t.objectExpression([ |
| 118 | + t.spreadElement(t.identifier(storyExportName)), |
| 119 | + t.objectProperty( |
| 120 | + t.identifier('play'), |
| 121 | + t.arrowFunctionExpression( |
| 122 | + [t.identifier('context')], |
| 123 | + t.blockStatement([ |
| 124 | + // Add code to call the original story's play function if it exists |
| 125 | + t.expressionStatement( |
| 126 | + t.awaitExpression( |
| 127 | + t.callExpression( |
| 128 | + t.optionalMemberExpression( |
| 129 | + t.identifier(storyExportName), |
| 130 | + t.identifier('play'), |
| 131 | + false, |
| 132 | + true |
| 133 | + ), |
| 134 | + [] |
| 135 | + ) |
| 136 | + ) |
| 137 | + ), |
| 138 | + // Then add the test function body |
| 139 | + ...(t.isBlockStatement(testFunction.body) |
| 140 | + ? testFunction.body.body |
| 141 | + : [t.expressionStatement(testFunction.body)]), |
| 142 | + ]), |
| 143 | + true // async |
| 144 | + ) |
| 145 | + ), |
| 146 | + t.objectProperty( |
| 147 | + t.identifier('storyName'), |
| 148 | + t.stringLiteral(`${storyInfo.name || storyExportName}: ${testName}`) |
| 149 | + ), |
| 150 | + ]); |
| 151 | + |
| 152 | + // Create export statement |
| 153 | + const exportDeclaration = t.exportNamedDeclaration( |
| 154 | + t.variableDeclaration('const', [ |
| 155 | + t.variableDeclarator(t.identifier(testExportName), newStoryObject), |
| 156 | + ]), |
| 157 | + [] |
| 158 | + ); |
| 159 | + |
| 160 | + newExports.push(exportDeclaration); |
| 161 | + |
| 162 | + // Mark the original test call for removal |
| 163 | + nodesToRemove.push(node); |
| 164 | + } |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + // Remove the test calls from the AST |
| 169 | + ast.program.body = ast.program.body.filter((node) => !nodesToRemove.includes(node)); |
| 170 | + |
| 171 | + // Add new exports to the AST |
| 172 | + ast.program.body.push(...newExports); |
| 173 | + |
| 174 | + return formatCsf(parsed, { sourceMaps: true, sourceFileName: fileName }, code); |
| 175 | +} |
0 commit comments