Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | 12x 103x 12x 12x 12x 12x 12x 12x 12x 12x 12x 11x 11x 11x 11x 12x 10x 10x 10x 12x 9x 9x 9x 9x 12x 8x 8x 8x 12x 7x 7x 7x 12x 6x 6x 6x 12x 5x 5x 5x 5x 12x 4x 4x 4x 12x 3x 3x 3x 3x 12x 2x 2x 2x 2x | import * as ts from "typescript"; import { dedent } from "ts-dedent"; import * as DocsOnly from "./__mock__/DocsOnly"; import * as StandardCSFButAutomaticallyRenderTested from "./__mock__/StandardCSFButAutomaticallyRenderTested"; import csfCommonjsOutput from "!!raw-loader!./__mock__/csf-commonjs-output.jsx.raw"; import { transformIsolatedComponentFiles } from "./isolated-component-files"; const testTransformer = ({ transformerFns, file, fileName, commonjs, }: { transformerFns: any[]; file: string | null; fileName: string; commonjs?: boolean; }) => ts .transpileModule(file!, { compilerOptions: commonjs ? { module: ts.ModuleKind.CommonJS, esModuleInterop: true, moduleResollution: "node", } : { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022, }, fileName, transformers: { before: transformerFns, }, }) .outputText.replace(/\r/g, ""); // these could be in files and raw imported, but probably better documentation // to have them directly at hand here const csf = dedent` import type { ComponentStory, ComponentStoryObj } from "@storybook/react"; import { Comp, CompProps } from "components/Comp"; import * as React from "react"; export default { component: Comp, title: "Component", }; export const StoryFn: ComponentStory<typeof Comp> = (args) => <Comp {...args} />; export const StoryObj: ComponentStoryObj<typeof Comp> = { args: { label: "test" } }; `; const csfTransformed = dedent` import { executeCyTests } from "orphic-cypress"; import { Comp } from "components/Comp"; import * as React from "react"; export default { component: Comp, title: "Component", }; export const StoryFn = (args) => <Comp {...args}/>; export const StoryObj = { args: { label: "test" } }; executeCyTests({ default: { component: Comp, title: "Component", }, StoryFn, StoryObj })\n `; const csfNotTransformed = dedent` import { Comp } from "components/Comp"; import * as React from "react"; export default { component: Comp, title: "Component", }; export const StoryFn = (args) => <Comp {...args}/>; export const StoryObj = { args: { label: "test" } };\n `; describe("transformIsolatedComponentFiles", () => { it("should transform a csf file by adding the execute import and call on all exports", () => { const transformerFn = transformIsolatedComponentFiles(); const result = testTransformer({ file: csf, fileName: "Comp.stories.tsx", transformerFns: [transformerFn], }); expect(result).to.equal(csfTransformed); }); it("should skip non-story files based on story name match, even if the file is otherwise a csf", () => { const transformerFn = transformIsolatedComponentFiles(); const result = testTransformer({ file: csf, fileName: "Comp.tsx", transformerFns: [transformerFn], }); expect(result) .to.not.contain("executeCyTests") .and.not.contain("orphic-cypress"); expect(result).to.equal(csfNotTransformed); }); it("should accept alternate location for executeCyTests incase its altered and exported from elsewhere", () => { const transformerFn = transformIsolatedComponentFiles("../some/file/path"); const result = testTransformer({ file: csf, fileName: "Comp.stories.tsx", transformerFns: [transformerFn], }); expect(result.split("\n")[0]).to.equal( 'import { executeCyTests } from "../some/file/path";' ); }); it("should accept alternate story pattern regex for matching", () => { const transformerFn = transformIsolatedComponentFiles( undefined, /\.st.*book\./ ); expect( testTransformer({ file: csf, fileName: "Comp.storybook.tsx", transformerFns: [transformerFn], }) ).to.equal(csfTransformed); expect( testTransformer({ file: csf, fileName: "Comp.starrybook.tsx", transformerFns: [transformerFn], }) ).to.equal(csfTransformed); expect( testTransformer({ file: csf, fileName: "Comp.nopebook.tsx", transformerFns: [transformerFn], }) ).to.equal(csfNotTransformed); }); it("should accept alternate story pattern string for matching", () => { const transformerFn = transformIsolatedComponentFiles( undefined, ".storybook." ); expect( testTransformer({ file: csf, fileName: "Comp.storybook.tsx", transformerFns: [transformerFn], }) ).to.equal(csfTransformed); expect( testTransformer({ file: csf, fileName: "Comp.starrybook.tsx", transformerFns: [transformerFn], }) ).to.equal(csfNotTransformed); }); it("should not transform if no default export is found", () => { const transformerFn = transformIsolatedComponentFiles(); const file = dedent` import type { ComponentStory, ComponentStoryObj } from "@storybook/react"; import { Comp, CompProps } from "components/Comp"; import * as React from "react"; export const StoryFn: ComponentStory<typeof Comp> = (args) => <Comp {...args} />; export const StoryObj: ComponentStoryObj<typeof Comp> = { args: { label: "test" }; }; `; expect( testTransformer({ file, fileName: "Comp.stories.tsx", transformerFns: [transformerFn], }) ) .to.not.contain("executeCyTests") .and.not.contain("orphic-cypress"); }); it("should not transform if no named exports are found", () => { const transformerFn = transformIsolatedComponentFiles(); const file = dedent` import type { ComponentStory, ComponentStoryObj } from "@storybook/react"; import { Comp, CompProps } from "components/Comp"; import * as React from "react"; export default { component: Comp, title: "Component", }; `; expect( testTransformer({ file, fileName: "Comp.stories.tsx", transformerFns: [transformerFn], }) ) .to.not.contain("executeCyTests") .and.not.contain("orphic-cypress"); }); it("should transform a commonjs build config", () => { const transformerFn = transformIsolatedComponentFiles(); const result = testTransformer({ file: csf, fileName: "Comp.stories.tsx", transformerFns: [transformerFn], commonjs: true, }); expect(result).to.contain("executeCyTests"); expect(result).to.equal(csfCommonjsOutput); }); it("should handle an empty file", () => { const transformerFn = transformIsolatedComponentFiles(); const result = testTransformer({ file: "", fileName: "Comp.stories.tsx", transformerFns: [transformerFn], }); expect(result).to.equal(""); }); it("should transform a csf mdx file, post transform to jsx", () => { const transformerFn = transformIsolatedComponentFiles(); const result = testTransformer({ file: StandardCSFButAutomaticallyRenderTested.input, fileName: "StandardCSFButAutomaticallyRenderTested.stories.mdx", transformerFns: [transformerFn], }); expect(result).to.contain("executeCyTests"); expect(result).to.equal(StandardCSFButAutomaticallyRenderTested.output); }); it("should handle a docs only mdx file, post transform to jsx", () => { const transformerFn = transformIsolatedComponentFiles(); const result = testTransformer({ file: DocsOnly.input, fileName: "DocsOnly.stories.mdx", transformerFns: [transformerFn], }); expect(result).not.to.contain("executeCyTests"); expect(result).to.equal(DocsOnly.output); }); }); |