How to use transformCsf method in storybook-test-runner

Best JavaScript code snippet using storybook-test-runner

transformCsf.ts

Source:transformCsf.ts Github

copy

Full Screen

1/* eslint-disable no-underscore-dangle */2import { loadCsf } from '@storybook/csf-tools';3import * as t from '@babel/types';4import generate from '@babel/generator';5import { toId, storyNameFromExport } from '@storybook/csf';6import dedent from 'ts-dedent';7const logger = console;8export interface TestContext {9 storyExport?: t.Identifier;10 name: t.Literal;11 title: t.Literal;12 id: t.Literal;13}14type TemplateResult = t.Statement | t.Statement[];15type FilePrefixer = () => TemplateResult;16type TestPrefixer = (context: TestContext) => TemplateResult;17interface TransformOptions {18 clearBody?: boolean;19 filePrefixer?: FilePrefixer;20 beforeEachPrefixer?: FilePrefixer;21 testPrefixer?: TestPrefixer;22 insertTestIfEmpty?: boolean;23 makeTitle?: (userTitle: string) => string;24}25const prefixFunction = (26 key: string,27 title: string,28 input: t.Expression,29 testPrefixer?: TestPrefixer30) => {31 const name = storyNameFromExport(key);32 const context: TestContext = {33 storyExport: t.identifier(key),34 name: t.stringLiteral(name), // FIXME .name annotation35 title: t.stringLiteral(title),36 id: t.stringLiteral(toId(title, name)),37 };38 const result = makeArray(testPrefixer(context));39 const stmt = result[1] as t.ExpressionStatement;40 return stmt.expression;41};42const makePlayTest = (43 key: string,44 title: string,45 metaOrStoryPlay: t.Node,46 testPrefix?: TestPrefixer47): t.Statement[] => {48 return [49 t.expressionStatement(50 t.callExpression(t.identifier('it'), [51 t.stringLiteral(!!metaOrStoryPlay ? 'play-test' : 'smoke-test'),52 prefixFunction(key, title, metaOrStoryPlay as t.Expression, testPrefix),53 ])54 ),55 ];56};57const makeDescribe = (58 key: string,59 tests: t.Statement[],60 beforeEachBlock?: t.ExpressionStatement61): t.Statement | null => {62 const blockStatements = beforeEachBlock ? [beforeEachBlock, ...tests] : tests;63 return t.expressionStatement(64 t.callExpression(t.identifier('describe'), [65 t.stringLiteral(key),66 t.arrowFunctionExpression([], t.blockStatement(blockStatements)),67 ])68 );69};70const makeBeforeEach = (beforeEachPrefixer: FilePrefixer) => {71 const stmt = beforeEachPrefixer() as t.ExpressionStatement;72 return t.expressionStatement(t.callExpression(t.identifier('beforeEach'), [stmt.expression]));73};74const makeArray = (templateResult: TemplateResult) =>75 Array.isArray(templateResult) ? templateResult : [templateResult];76export const transformCsf = (77 code: string,78 {79 filePrefixer,80 clearBody = false,81 testPrefixer,82 beforeEachPrefixer,83 insertTestIfEmpty,84 makeTitle,85 }: TransformOptions = {}86) => {87 const csf = loadCsf(code, { makeTitle });88 csf.parse();89 const storyExports = Object.keys(csf._stories);90 const title = csf.meta.title;91 const storyPlays = storyExports.reduce((acc, key) => {92 const annotations = csf._storyAnnotations[key];93 if (annotations?.play) {94 acc[key] = annotations.play;95 }96 return acc;97 }, {} as Record<string, t.Node>);98 const playTests = storyExports99 .map((key: string) => {100 let tests: t.Statement[] = [];101 tests = [...tests, ...makePlayTest(key, title, storyPlays[key], testPrefixer)];102 if (tests.length) {103 return makeDescribe(key, tests);104 }105 return null;106 })107 .filter(Boolean);108 const allTests = playTests;109 let result = '';110 // FIXME: insert between imports111 if (filePrefixer) {112 const { code: prefixCode } = generate(t.program(makeArray(filePrefixer())), {});113 result = `${prefixCode}\n`;114 }115 if (!clearBody) result = `${result}${code}\n`;116 if (allTests.length) {117 const describe = makeDescribe(118 csf.meta.title,119 allTests,120 beforeEachPrefixer ? makeBeforeEach(beforeEachPrefixer) : undefined121 );122 const { code: describeCode } = generate(describe, {});123 result = dedent`124 ${result}125 if (!require.main) {126 ${describeCode}127 }128 `;129 } else if (insertTestIfEmpty) {130 result = `describe('${csf.meta.title}', () => { it('no-op', () => {}) });`;131 }132 return result;...

Full Screen

Full Screen

transformPlaywright.ts

Source:transformPlaywright.ts Github

copy

Full Screen

...63 const filePath = './' + relative(workingDir, filename);64 return (userTitle: string) => userOrAutoTitle(filePath, normalizedStoriesEntries, userTitle);65};66export const transformPlaywright = (src: string, filename: string) => {67 const result = transformCsf(src, {68 filePrefixer,69 // @ts-ignore70 testPrefixer,71 insertTestIfEmpty: true,72 clearBody: true,73 makeTitle: makeTitleFactory(filename),74 });75 return result;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { transformCsf } = require('storybook-test-runner');2const { transformCsf } = require('storybook-test-runner');3const { transformCsf } = require('storybook-test-runner');4const { transformCsf } = require('storybook-test-runner');5const { transformCsf } = require('storybook-test-runner');6const { transformCsf } = require('storybook-test-runner');7const { transformCsf } = require('storybook-test-runner');8const { transformCsf } = require('storybook-test-runner');9const { transformCsf } = require('storybook-test-runner');10const { transformCsf } = require('storybook-test-runner');11const { transformCsf } = require('storybook-test-runner');12const { transformCsf } = require('storybook-test-runner');13const { transformCsf } = require('storybook-test-runner');14const { transformCsf } = require('storybook-test-runner');15const { transformCsf } = require('storybook-test-runner');16const { transformCsf } = require('storybook-test-runner');17const { transformCsf } = require('storybook-test-runner');18const { transformCsf } = require('storybook-test-runner');19const { transformCsf } = require('storybook-test-runner');

Full Screen

Using AI Code Generation

copy

Full Screen

1import { transformCsf } from 'storybook-test-runner';2import { storiesOf } from '@storybook/react';3import MyComponent from './MyComponent';4const stories = storiesOf('MyComponent', module);5stories.add('default', () => <MyComponent />);6const transformedStories = transformCsf(stories);7export default transformedStories;8import transformedStories from './test.js';9MIT © [davidnguyen179](

Full Screen

Using AI Code Generation

copy

Full Screen

1import { transformCsf } from 'storybook-test-runner';2import { storiesOf } from '@storybook/react';3import Button from './Button';4const stories = storiesOf('Button', module);5stories.add('simple', () => <Button>Click me</Button>);6stories.add('with background', () => <Button background="red">Click me</Button>);7const transformed = transformCsf(storiesOf);8import { transformCsf } from 'storybook-test-runner';9import stories from './test';10describe('Button', () => {11 it('renders correctly', () => {12 const { simple } = stories;13 const { container } = render(simple());14 expect(container).toMatchSnapshot();15 });16});17import { transformCsf } from 'storybook-test-runner';18import stories from './test';19describe('Button', () => {20 it('renders correctly', () => {21 const { simple } = stories;22 const { container } = render(simple());23 expect(container).toMatchSnapshot();24 });25});26import { transformCsf } from 'storybook-test-runner';27import stories from './test';28describe('Button', () => {29 it('renders correctly', () => {30 const { simple } = stories;31 const { container } = render(simple());32 expect(container).toMatchSnapshot();33 });34});35import { transformCsf } from 'storybook-test-runner';36import stories from './test';37describe('Button', () => {38 it('renders correctly', () => {39 const { simple } = stories;40 const { container } = render(simple());41 expect(container).toMatchSnapshot();42 });43});44import { transformCsf } from 'storybook-test-runner';45import stories from './test';46describe('Button', () => {47 it('renders correctly', () => {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { transformCsf } from 'storybook-test-runner';2transformCsf({3});4import { transformCsf } from 'storybook-test-runner';5transformCsf({6});7import { transformCsf } from 'storybook-test-runner';8transformCsf({9});10import { transformCsf } from 'storybook-test-runner';11transformCsf({

Full Screen

Using AI Code Generation

copy

Full Screen

1import { transformCsf } from 'storybook-test-runner';2import { storiesOf } from '@storybook/html';3export default {4};5export const Basic = () => {6 const div = document.createElement('div');7 div.innerHTML = 'Hello World';8 return div;9};10const Test = () => {11 const div = document.createElement('div');12 div.innerHTML = 'Hello World';13 return div;14};15storiesOf('Test', module).add('Basic', Test);16import { transformCsf } from 'storybook-test-runner';17import { storiesOf } from '@storybook/html';18describe('test', () => {19 it('should work', () => {20 const stories = transformCsf(require('./test.js'));21 expect(stories).toEqual([22 {23 storyFn: expect.any(Function),24 parameters: { fileName: './test.js' },25 },26 ]);27 });28});29import { transformCsfToJest } from 'storybook-test-runner';30import { storiesOf } from '@storybook/html';31export default {32};33export const Basic = () => {34 const div = document.createElement('div');35 div.innerHTML = 'Hello World';36 return div;37};38const Test = () => {39 const div = document.createElement('div');40 div.innerHTML = 'Hello World';41 return div;42};43storiesOf('Test', module).add('Basic', Test);44import { transformCsfToJest } from 'storybook-test-runner';45import { storiesOf } from '@storybook/html';46describe('test', () => {47 it('should work', () => {48 const stories = transformCsfToJest(require('./test.js'));49 expect(stories).toEqual(50 `import { transformCsf } from 'storybook-test-runner';51import { storiesOf } from '@storybook/html';52describe('test', () => {53 it('should work

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-test-runner automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful