Best JavaScript code snippet using storybook-root
run-e2e.ts
Source:run-e2e.ts
1/* eslint-disable no-irregular-whitespace */2import path from 'path';3import { remove, ensureDir, pathExists, writeFile, readJSON, writeJSON } from 'fs-extra';4import { prompt } from 'enquirer';5import pLimit from 'p-limit';6import shell from 'shelljs';7import program from 'commander';8import { serve } from './utils/serve';9import { exec } from './utils/command';10// @ts-ignore11import { listOfPackages } from './utils/list-packages';12import * as configs from './run-e2e-config';13const logger = console;14export interface Parameters {15 /** E2E configuration name */16 name: string;17 /** framework version */18 version: string;19 /** CLI to bootstrap the project */20 generator: string;21 /** Use storybook framework detection */22 autoDetect?: boolean;23 /** Pre-build hook */24 preBuildCommand?: string;25 /** When cli complains when folder already exists */26 ensureDir?: boolean;27 /** Dependencies to add before building Storybook */28 additionalDeps?: string[];29 /** Add typescript dependency and creates a tsconfig.json file */30 typescript?: boolean;31}32export interface Options extends Parameters {33 cwd?: string;34}35const rootDir = path.join(__dirname, '..');36const siblingDir = path.join(__dirname, '..', '..', 'storybook-e2e-testing');37const prepareDirectory = async ({38 cwd,39 ensureDir: ensureDirOption = true,40}: Options): Promise<boolean> => {41 const siblingExists = await pathExists(siblingDir);42 if (!siblingExists) {43 await ensureDir(siblingDir);44 await exec('git init', { cwd: siblingDir });45 await exec('npm init -y', { cwd: siblingDir });46 await writeFile(path.join(siblingDir, '.gitignore'), 'node_modules\n');47 }48 const cwdExists = await pathExists(cwd);49 if (cwdExists) {50 return true;51 }52 if (ensureDirOption) {53 await ensureDir(cwd);54 }55 return false;56};57const cleanDirectory = async ({ cwd }: Options): Promise<void> => {58 await remove(cwd);59 await remove(path.join(siblingDir, 'node_modules'));60 if (useYarn2) {61 await shell.rm('-rf', [path.join(siblingDir, '.yarn'), path.join(siblingDir, '.yarnrc.yml')]);62 }63};64const configureYarn2 = async ({ cwd }: Options) => {65 const command = [66 `yarn set version berry`,67 // â ï¸ Need to set registry because Yarn 2 is not using the conf of Yarn 168 `yarn config set npmScopes --json '{ "storybook": { "npmRegistryServer": "http://localhost:6000/" } }'`,69 // Some required magic to be able to fetch deps from local registry70 `yarn config set unsafeHttpWhitelist --json '["localhost"]'`,71 ].join(' && ');72 logger.info(`ðâConfiguring Yarn 2`);73 logger.debug(command);74 try {75 await exec(command, { cwd });76 } catch (e) {77 logger.error(`ð¨âConfiguring Yarn 2 failed`);78 throw e;79 }80};81const generate = async ({ cwd, name, version, generator }: Options) => {82 let command = generator.replace(/{{name}}/g, name).replace(/{{version}}/g, version);83 if (useYarn2) {84 command = command.replace(/npx/g, `yarn dlx`);85 }86 logger.info(`ðââBootstrapping ${name} project`);87 logger.debug(command);88 try {89 await exec(command, { cwd });90 } catch (e) {91 logger.error(`ð¨âBootstrapping ${name} failed`);92 throw e;93 }94};95const initStorybook = async ({ cwd, autoDetect = true, name }: Options) => {96 logger.info(`ð¨âInitializing Storybook with @storybook/cli`);97 try {98 const type = autoDetect ? '' : `--type ${name}`;99 const sbCLICommand = useLocalSbCli100 ? 'node ../../storybook/lib/cli/dist/generate'101 : 'npx -p @storybook/cli sb';102 await exec(`${sbCLICommand} init --yes ${type}`, { cwd });103 } catch (e) {104 logger.error(`ð¨âStorybook initialization failed`);105 throw e;106 }107};108// Verdaccio doesn't resolve *109// So we set resolutions manually in package.json110const setResolutions = async ({ cwd }: Options) => {111 logger.info(`ðâSetting yarn resolutions`);112 const packages = await listOfPackages();113 const packageJsonPath = path.resolve(cwd, 'package.json');114 const packageJson = await readJSON(packageJsonPath, { encoding: 'utf8' });115 packageJson.resolutions = {116 ...packageJson.resolutions,117 ...packages.reduce(118 (acc, { name, version }) => ({119 ...acc,120 [name]: version,121 }),122 {}123 ),124 };125 await writeJSON(packageJsonPath, packageJson, { encoding: 'utf8', spaces: 2 });126};127const addRequiredDeps = async ({ cwd, additionalDeps }: Options) => {128 logger.info(`ðâAdding needed deps & installing all deps`);129 try {130 if (additionalDeps && additionalDeps.length > 0) {131 await exec(`yarn add -D ${additionalDeps.join(' ')}`, {132 cwd,133 silent: true,134 });135 } else {136 await exec(`yarn install`, {137 cwd,138 silent: true,139 });140 }141 } catch (e) {142 logger.error(`ð¨âDependencies installation failed`);143 throw e;144 }145};146const addTypescript = async ({ cwd }: Options) => {147 logger.info(`ð®ð»âAdding typescript and tsconfig.json`);148 try {149 await exec(`yarn add -D typescript@latest`, { cwd });150 const tsConfig = {151 compilerOptions: {152 baseUrl: '.',153 esModuleInterop: true,154 jsx: 'preserve',155 skipLibCheck: true,156 strict: true,157 },158 include: ['src/*'],159 };160 const tsConfigJsonPath = path.resolve(cwd, 'tsconfig.json');161 await writeJSON(tsConfigJsonPath, tsConfig, { encoding: 'utf8', spaces: 2 });162 } catch (e) {163 logger.error(`ð¨âCreating tsconfig.json failed`);164 throw e;165 }166};167const buildStorybook = async ({ cwd, preBuildCommand }: Options) => {168 logger.info(`ð·âBuilding Storybook`);169 try {170 if (preBuildCommand) {171 await exec(preBuildCommand, { cwd });172 }173 await exec(`yarn build-storybook --quiet`, { cwd });174 } catch (e) {175 logger.error(`ð¨âStorybook build failed`);176 throw e;177 }178};179const serveStorybook = async ({ cwd }: Options, port: string) => {180 const staticDirectory = path.join(cwd, 'storybook-static');181 logger.info(`ðâServing ${staticDirectory} on http://localhost:${port}`);182 return serve(staticDirectory, port);183};184const runCypress = async ({ name, version }: Options, location: string, open: boolean) => {185 const cypressCommand = open ? 'open' : 'run';186 logger.info(`ð¤âRunning Cypress tests`);187 try {188 await exec(189 `yarn cypress ${cypressCommand} --config integrationFolder="cypress/generated" --env location="${location}"`,190 { cwd: rootDir }191 );192 logger.info(`â
âE2E tests success`);193 logger.info(`ðâStorybook is working great with ${name} ${version}!`);194 } catch (e) {195 logger.error(`ð¨âE2E tests fails`);196 logger.info(`ð¥ºâStorybook has some issues with ${name} ${version}!`);197 throw e;198 }199};200const runTests = async ({ name, version, ...rest }: Parameters) => {201 const options = {202 name,203 version,204 ...rest,205 cwd: path.join(siblingDir, `${name}-${version}`),206 };207 logger.info(`ðââï¸âStarting for ${name} ${version}`);208 logger.log();209 logger.debug(options);210 logger.log();211 if (!(await prepareDirectory(options))) {212 if (useYarn2) {213 await configureYarn2({ ...options, cwd: siblingDir });214 }215 await generate({ ...options, cwd: siblingDir });216 logger.log();217 await setResolutions(options);218 logger.log();219 if (options.typescript) {220 await addTypescript(options);221 logger.log();222 }223 await initStorybook(options);224 logger.log();225 await addRequiredDeps(options);226 logger.log();227 await buildStorybook(options);228 logger.log();229 }230 const server = await serveStorybook(options, '4000');231 logger.log();232 let open = false;233 if (!process.env.CI) {234 ({ open } = await prompt({235 type: 'confirm',236 name: 'open',237 message: 'Should open cypress?',238 }));239 }240 try {241 await runCypress(options, 'http://localhost:4000', open);242 logger.log();243 } finally {244 server.close();245 }246};247// Run tests!248const runE2E = (parameters: Parameters) =>249 runTests(parameters)250 .then(async () => {251 if (!process.env.CI) {252 const { name, version } = parameters;253 const cwd = path.join(siblingDir, `${name}-${version}`);254 const { cleanup } = await prompt({255 type: 'confirm',256 name: 'cleanup',257 message: 'Should perform cleanup?',258 });259 if (cleanup) {260 logger.log();261 logger.info(`ðââCleaning ${cwd}`);262 await cleanDirectory({ ...parameters, cwd });263 } else {264 logger.log();265 logger.info(`ð¯âNo cleanup happened: ${cwd}`);266 }267 }268 })269 .catch((e) => {270 logger.error(`ðâan error occurred:\n${e}`);271 logger.log();272 logger.error(e);273 logger.log();274 process.exitCode = 1;275 });276program.option('--use-yarn-2', 'Run tests using Yarn 2 instead of Yarn 1 + npx', false);277program.option(278 '--use-local-sb-cli',279 'Run tests using local @storybook/cli package (â ï¸ Be sure @storybook/cli is properly build as it will not be rebuild before running the tests)',280 false281);282program.parse(process.argv);283const { useYarn2, useLocalSbCli, args: frameworkArgs } = program;284const typedConfigs: { [key: string]: Parameters } = configs;285let e2eConfigs: { [key: string]: Parameters } = {};286if (frameworkArgs.length > 0) {287 // eslint-disable-next-line no-restricted-syntax288 for (const [framework, version = 'latest'] of frameworkArgs.map((arg) => arg.split('@'))) {289 e2eConfigs[`${framework}-${version}`] = Object.values(typedConfigs).find(290 (c) => c.name === framework && c.version === version291 );292 }293} else {294 e2eConfigs = typedConfigs;295 // FIXME: For now Yarn 2 E2E tests must be run by explicitly call `yarn test:e2e-framework yarn2Cra@latest`296 // Because it is telling Yarn to use version 2297 delete e2eConfigs.yarn_2_cra;298}299const perform = () => {300 const limit = pLimit(1);301 const narrowedConfigs = Object.values(e2eConfigs);302 const nodeIndex = +process.env.CIRCLE_NODE_INDEX || 0;303 const numberOfNodes = +process.env.CIRCLE_NODE_TOTAL || 1;304 const list = narrowedConfigs.filter((_, index) => {305 return index % numberOfNodes === nodeIndex;306 });307 logger.info(308 `ðâAssigning jobs ${list309 .map((c) => c.name)310 .join(', ')} to node ${nodeIndex} (on ${numberOfNodes})`311 );312 return Promise.all(list.map((config) => limit(() => runE2E(config))));313};314perform().then(() => {315 process.exit(process.exitCode || 0);...
Using AI Code Generation
1import { siblingDir } from 'storybook-root'2import siblingDir from 'storybook-root/siblingDir'3import { siblingDir } from 'storybook-root'4import siblingDir from 'storybook-root/siblingDir'5import { siblingDir } from 'storybook-root'6import siblingDir from 'storybook-root/siblingDir'7import { siblingDir } from 'storybook-root'8import siblingDir from 'storybook-root/siblingDir'9import { siblingDir } from 'storybook-root'10import siblingDir from 'storybook-root/siblingDir'11import { siblingDir } from 'storybook-root'12import siblingDir from 'storybook-root/siblingDir'13import { siblingDir } from 'storybook-root'14import siblingDir from 'storybook-root/siblingDir'15import { siblingDir } from 'storybook-root'16import siblingDir from 'storybook-root/siblingDir'17import { siblingDir } from 'storybook-root'18import siblingDir from 'storybook-root/siblingDir'19import { siblingDir } from 'storybook-root'20import siblingDir from 'storybook-root/siblingDir'
Using AI Code Generation
1const storybookRootDir = require('storybook-root-dir');2const path = require('path');3const storybookDir = storybookRootDir();4const myDir = path.join(storybookDir, 'myDir');5module.exports = {6 webpackFinal: async config => {7 config.module.rules.push({8 include: path.resolve(__dirname, '../'),9 });10 config.resolve.modules.push(myDir);11 return config;12 },13};
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const path = require('path');3const storybookRootDir = storybookRoot.getStorybookRootDir();4const siblingDir = storybookRoot.siblingDir();5const siblingPath = path.join(siblingDir, 'test.txt');6console.log(siblingPath);7const storybookRoot = require('storybook-root');8const path = require('path');9const storybookRootDir = storybookRoot.getStorybookRootDir();10const siblingDir = storybookRoot.siblingDir();11const siblingPath = path.join(siblingDir, 'test.js');12console.log(siblingPath);13const storybookRoot = require('storybook-root');14const path = require('path');15const storybookRootDir = storybookRoot.getStorybookRootDir();16const siblingDir = storybookRoot.siblingDir();17const siblingPath = path.join(siblingDir, 'test.txt');18console.log(siblingPath);19const storybookRoot = require('storybook-root');20const path = require('path');21const storybookRootDir = storybookRoot.getStorybookRootDir();22const siblingDir = storybookRoot.siblingDir();23const siblingPath = path.join(siblingDir, 'test.js');24console.log(siblingPath);25const storybookRoot = require('storybook-root');26const path = require('path');27const storybookRootDir = storybookRoot.getStorybookRootDir();28const siblingDir = storybookRoot.siblingDir();29const siblingPath = path.join(siblingDir, 'test.txt');30console.log(siblingPath);
Using AI Code Generation
1var storybookRoot = require('storybook-root');2var siblingDir = storybookRoot.siblingDir;3var path = siblingDir('test/test.js');4console.log(path);5var storybookRoot = require('storybook-root');6var siblingDir = storybookRoot.siblingDir;7var path = siblingDir('test/test.js');8console.log(path);9var storybookRoot = require('storybook-root');10var siblingDir = storybookRoot.siblingDir;11var path = siblingDir('test/test.js');12console.log(path);13var storybookRoot = require('storybook-root');14var siblingDir = storybookRoot.siblingDir;15var path = siblingDir('test/test.js');16console.log(path);17var storybookRoot = require('storybook-root');18var siblingDir = storybookRoot.siblingDir;19var path = siblingDir('test/test.js');20console.log(path);21var storybookRoot = require('storybook-root');22var siblingDir = storybookRoot.siblingDir;23var path = siblingDir('test/test.js');24console.log(path);25var storybookRoot = require('storybook-root');26var siblingDir = storybookRoot.siblingDir;27var path = siblingDir('test/test.js');28console.log(path);29var storybookRoot = require('storybook-root');30var siblingDir = storybookRoot.siblingDir;31var path = siblingDir('test/test.js');32console.log(path);33var storybookRoot = require('storybook-root');34var siblingDir = storybookRoot.siblingDir;35var path = siblingDir('test/test.js');36console.log(path);37var storybookRoot = require('storybook-root');
Using AI Code Generation
1const storybookDir = require('storybook-root').siblingDir;2const path = require('path');3const storiesDir = path.join(storybookDir, 'stories');4const fixturesDir = path.join(storybookDir, 'fixtures');5const storiesDir = storybookDir.siblingDir('stories');6const fixturesDir = storybookDir.siblingDir('fixtures');7const storiesDir = storybookDir.siblingDir('stories', 'fixtures');8const fixturesDir = storybookDir.siblingDir('fixtures', 'stories');9const storiesDir = storybookDir.siblingDir('stories', 'fixtures', 'src');10const fixturesDir = storybookDir.siblingDir('fixtures', 'stories', 'src');11const storiesDir = storybookDir.siblingDir('stories', 'fixtures', 'src', 'config');12const fixturesDir = storybookDir.siblingDir('fixtures', 'stories', 'src', 'config');13const storiesDir = storybookDir.siblingDir('stories', 'fixtures', 'src', 'config', 'storybook');14const fixturesDir = storybookDir.siblingDir('fixtures', 'stories', 'src', 'config', 'storybook');15const storiesDir = storybookDir.siblingDir('stories', 'fixtures', 'src', 'config', 'storybook', 'test');16const fixturesDir = storybookDir.siblingDir('fixtures', 'stories', 'src', 'config', 'storybook', 'test');17const storiesDir = storybookDir.siblingDir('stories', 'fixtures', 'src', 'config', 'storybook', 'test', 'test');18const fixturesDir = storybookDir.siblingDir('fixtures', 'stories', 'src', 'config', 'storybook', 'test', '
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const siblingDir = storybookRoot.siblingDir;3const path = siblingDir('test.js');4const storybookDir = storybookRoot.storybookDir;5const path = storybookDir('test.js');6MIT © [Rajesh Singh ](
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const path = require('path');3const siblingDir = storybookRoot.siblingDir;4const siblingPath = siblingDir('sibling');5console.log(siblingPath);6const storybookRoot = require('storybook-root');7const path = require('path');8const siblingDir = storybookRoot.siblingDir;9const siblingPath = siblingDir('sibling');10console.log(siblingPath);11const storybookRoot = require('storybook-root');12const path = require('path');13const siblingDir = storybookRoot.siblingDir;14const siblingPath = siblingDir('sibling');15console.log(siblingPath);16const storybookRoot = require('storybook-root');17const path = require('path');18const siblingDir = storybookRoot.siblingDir;19const siblingPath = siblingDir('sibling');20console.log(siblingPath);21const storybookRoot = require('storybook-root');22const path = require('path');23const siblingDir = storybookRoot.siblingDir;24const siblingPath = siblingDir('sibling');25console.log(siblingPath);
Using AI Code Generation
1import { siblingDir } from 'storybook-root-decorator'2const siblingDirPath = siblingDir('sibling-directory')3import { siblingDir } from 'storybook-root-decorator'4const siblingDirPath = siblingDir('../sibling-directory')5import { siblingDir } from 'storybook-root-decorator'6const siblingDirPath = siblingDir('sibling-directory')7import { siblingDir } from 'storybook-root-decorator'8const siblingDirPath = siblingDir('../sibling-directory')9import { siblingDir } from 'storybook-root-decorator'10const siblingDirPath = siblingDir('sibling-directory')
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!