Best JavaScript code snippet using storybook-root
update.js
Source:update.js
1const fs = require('fs');2const path = require('path');3const spawn = require('./src/spawn');4const dependencies = require('./benchmarks.packages.json');5updatePackageJson('npm');6spawn('rm', ['-rf', './package-lock.json'], path.resolve('npm'));7spawn('npm5', ['install'], path.resolve('npm'));8spawn('rm', ['-rf', './node_cache'], path.resolve('npm'));9updatePackageJson('npm-cached');10spawn('rm', ['-rf', './node_cache'], path.resolve('npm-cached'));11spawn('rm', ['-rf', './package-lock.json'], path.resolve('npm-cached'));12spawn('npm5', ['install'], path.resolve('npm-cached'));13spawn('rm', ['-rf', './node_modules'], path.resolve('npm-cached'));14spawn('npm', ['install'], path.resolve('npm-cached'));15updatePackageJson('pnpm');16spawn('rm', ['-rf', './shrinkwrap.yaml'], path.resolve('pnpm'));17spawn('pnpm', ['install'], path.resolve('pnpm'));18spawn('rm', ['-rf', '.pnpm-registry'], path.resolve('pnpm'));19spawn('rm', ['-rf', '.pnpm-store'], path.resolve('pnpm'));20spawn('rm', ['-rf', './node_modules'], path.resolve('pnpm'));21updatePackageJson('pnpm-cached');22spawn('rm', ['-rf', './shrinkwrap.yaml'], path.resolve('pnpm-cached'));23spawn('rm', ['-rf', '.pnpm-registry'], path.resolve('pnpm'));24spawn('rm', ['-rf', '.pnpm-store'], path.resolve('pnpm'));25spawn('pnpm', ['install'], path.resolve('pnpm-cached'));26spawn('rm', ['-rf', './node_modules'], path.resolve('pnpm-cached'));27updatePnpmStorePath('pnpm-cached', '.pnpm-store');28updatePackageJson('pnpm-offline');29spawn('rm', ['-rf', './.pnpm-registry-offline'], path.resolve('pnpm-offline'));30spawn('rm', ['-rf', './.pnpm-store-offline'], path.resolve('pnpm-offline'));31spawn('rm', ['-rf', './shrinkwrap.yaml'], path.resolve('pnpm-offline'));32spawn('mkdir', ['./.pnpm-registry-offline'], path.resolve('pnpm-offline'));33spawn('mkdir', ['./.pnpm-store-offline'], path.resolve('pnpm-offline'));34spawn('pnpm', ['install', '--offline', 'false'], path.resolve('pnpm-offline'));35spawn('rm', ['-rf', './node_modules'], path.resolve('pnpm-offline'));36updatePnpmStorePath('pnpm-offline', '.pnpm-store-offline');37updatePackageJson('shrinkpack');38spawn('rm', ['-rf', './npm-shrinkwrap.json'], path.resolve('shrinkpack'));39spawn('npm', ['install'], path.resolve('shrinkpack'));40spawn('npm', ['shrinkwrap'], path.resolve('shrinkpack'));41spawn('shrinkpack', [], path.resolve('shrinkpack'));42spawn('rm', ['-rf', './node_cache'], path.resolve('shrinkpack'));43updatePackageJson('shrinkpack-compressed');44spawn('rm', ['-rf', './npm-shrinkwrap.json'], path.resolve('shrinkpack-compressed'));45spawn('npm', ['install'], path.resolve('shrinkpack-compressed'));46spawn('npm', ['shrinkwrap'], path.resolve('shrinkpack-compressed'));47spawn('shrinkpack', ['--compress'], path.resolve('shrinkpack-compressed'));48spawn('rm', ['-rf', './node_cache'], path.resolve('shrinkpack-compressed'));49updatePackageJson('yarn');50spawn('rm', ['-rf', './yarn.lock'], path.resolve('yarn'));51spawn('yarn', ['install'], path.resolve('yarn'));52spawn('rm', ['-rf', './npm-packages-offline-cache'], path.resolve('yarn'));53updatePackageJson('yarn-offline');54spawn('rm', ['-rf', './npm-packages-offline-cache'], path.resolve('yarn-offline'));55spawn('rm', ['-rf', './yarn.lock'], path.resolve('yarn-offline'));56spawn('yarn', ['install'], path.resolve('yarn-offline'));57function updatePackageJson(directory) {58 const file = path.resolve(directory, 'package.json');59 const contents = prettyJson({60 name: directory,61 version: '0.0.0',62 dependencies: dependencies63 });64 fs.writeFileSync(file, contents);65}66function updatePnpmStorePath(directory, storeName) {67 const file = path.resolve(directory, storeName, '1', 'store.yaml');68 const contents = fs.readFileSync(file, { encoding: 'utf8' });69 fs.writeFileSync(file, contents.split(path.resolve(directory, '..')).join('.'));70}71function prettyJson(data) {...
index.test.js
Source:index.test.js
1const { updatePackageJSON, writePackageJSON, writeBabelRC, writeMochaOpts } = require('../src/');2jest.mock('write-json-file', () => jest.fn().mockResolvedValue({}));3jest.mock('write', () => jest.fn().mockResolvedValue({}));4const writeJsonFile = require('write-json-file');5const writeFile = require('write');6let logSpy;7let warnSpy;8beforeAll(() => {9 logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});10 warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});11});12afterAll(() => {13 logSpy.mockReset();14 warnSpy.mockReset();15});16test('does not write when --write is not passed', async () => {17 await writePackageJSON({ write: false });18 expect(writeJsonFile).not.toBeCalled();19 writeJsonFile.mockReset();20 await writeBabelRC("./fixtures/babelrc.json5", { write: false });21 expect(writeJsonFile).not.toBeCalled();22 writeJsonFile.mockReset();23 await writeMochaOpts("./fixtures/scripts-mocha.json", { write: false });24 expect(writeFile).not.toBeCalled();25 writeFile.mockReset();26});27test('writes when --write is passed', async () => {28 await writePackageJSON({ write: true });29 expect(writeJsonFile).toBeCalled();30 writeJsonFile.mockReset();31 await writeBabelRC("./fixtures/babelrc.json5", { write: true });32 expect(writeJsonFile).toBeCalled();33 writeJsonFile.mockReset();34 await writeMochaOpts("./fixtures/scripts-mocha.json", { write: true });35 expect(writeFile).toBeCalled();36 writeFile.mockReset();37});38test('do not downgrade dependencies', async () => {39 let pkg = await updatePackageJSON({40 dependencies: {41 'babel-loader': '^9.0.0',42 'rollup-plugin-babel': '^5.0.1',43 'babel-eslint': '^10.0.0',44 }45 });46 expect(pkg).toMatchSnapshot();47});48describe('flow preset', () => {49 test('adds flow preset if user was using v6 preset-react', async () => {50 let pkg = await updatePackageJSON({51 dependencies: {},52 devDependencies: {53 "babel-preset-react": "6.0.0"54 }55 }, { hasFlow: true });56 expect(pkg).toMatchSnapshot();57 });58 test('does not add flow preset if user was using v6 preset-react but flow not detected', async () => {59 let pkg = await updatePackageJSON({60 dependencies: {},61 devDependencies: {62 "babel-preset-react": "6.0.0"63 }64 }, { hasFlow: false });65 expect(pkg).toMatchSnapshot();66 });67 test('adds flow preset if user is upgrading from previous v7', async () => {68 let pkg = await updatePackageJSON({69 dependencies: {},70 devDependencies: {71 "@babel/preset-react": "7.0.0-alpha.0"72 }73 }, { hasFlow: true });74 expect(pkg).toMatchSnapshot();75 });76 test('does not add flow preset if user is upgrading from previous v7 but flow not detected', async () => {77 let pkg = await updatePackageJSON({78 dependencies: {},79 devDependencies: {80 "@babel/preset-react": "7.0.0-alpha.0",81 }82 }, { hasFlow: false });83 expect(pkg).toMatchSnapshot();84 });85 test('handles flow preset if user had entry and is upgrading from previous v7', async () => {86 let pkg = await updatePackageJSON({87 dependencies: {},88 devDependencies: {89 "@babel/preset-flow": "7.0.0-alpha.0",90 "@babel/preset-react": "7.0.0-alpha.0",91 }92 }, { hasFlow: true });93 expect(pkg).toMatchSnapshot();94 });95 test('handles flow preset if user had entry and is upgrading from previous v7 and flow not detected', async () => {96 let pkg = await updatePackageJSON({97 dependencies: {},98 devDependencies: {99 "@babel/preset-flow": "7.0.0-alpha.0",100 "@babel/preset-react": "7.0.0-alpha.0",101 }102 }, { hasFlow: false });103 expect(pkg).toMatchSnapshot();104 });...
packageData.test.js
Source:packageData.test.js
1const { updatePackageJSON } = require('../src/');2const upgradeDeps = require('../src/upgradeDeps');3const babelCoreFixture = require('../fixtures/babel-core');4const avaFixture = require('../fixtures/ava');5const jestFixture = require('../fixtures/jest');6const jestCliFixture = require('../fixtures/jest-cli');7const jest24Fixture = require('../fixtures/jest-v24');8const depsFixture = require('../fixtures/deps');9const webpackV1Fixture = require('../fixtures/webpack-v1');10const depsFixtureEarlierBeta = require('../fixtures/deps-earlier-beta.json');11const scriptsMochaFixture = require('../fixtures/scripts-mocha');12const scriptsBabelNodeFixture = require('../fixtures/scripts-babel-node');13const babelJestFixture = require('../fixtures/babel-jest');14const VERSION = "7.0.0-beta.39";15describe('upgradeDeps', () => {16 test('upgrades from v6', () => {17 expect(upgradeDeps(depsFixture, VERSION)).toMatchSnapshot();18 });19 test('upgrades from earlier v7 version', () => {20 expect(upgradeDeps(depsFixtureEarlierBeta, VERSION)).toMatchSnapshot();21 });22 test('splits runtime into runtime and runtime-corejs2', () => {23 expect(upgradeDeps({24 "@babel/runtime": "7.0.0-alpha.0",25 }, VERSION)).toMatchSnapshot();26 });27 test("doesn't add runtime-corejs2 if it is already there", () => {28 expect(upgradeDeps({29 "@babel/runtime": "7.0.0-alpha.0",30 "@babel/runtime-corejs2": "7.0.0-alpha.0",31 }, VERSION)).toMatchSnapshot();32 });33});34test('scripts', async () => {35 expect(await updatePackageJSON(scriptsMochaFixture)).toMatchSnapshot();36 expect(await updatePackageJSON(scriptsBabelNodeFixture)).toMatchSnapshot();37});38test('@babel/core peerDep', async () => {39 expect(await updatePackageJSON(babelCoreFixture)).toMatchSnapshot();40});41test('jest babel-core bridge', async () => {42 expect(await updatePackageJSON(jestFixture)).toMatchSnapshot();43});44test('jest-cli babel-core bridge', async () => {45 expect(await updatePackageJSON(jestCliFixture)).toMatchSnapshot();46});47test("doesn't downgrade jest >= 24 ", async () => {48 expect(await updatePackageJSON(jest24Fixture)).toMatchSnapshot();49});50test('webpack v1 compatibility', async () => {51 expect(await updatePackageJSON(webpackV1Fixture)).toMatchSnapshot();52});53test('replaces stage presets', () => {54 expect(upgradeDeps({55 "@babel/preset-stage-2": "7.0.0-alpha.0"56 }, VERSION)).toMatchSnapshot();57});58test('replaces transform-decorators-legacy plugin', () => {59 expect(upgradeDeps({60 "babel-plugin-transform-decorators-legacy": "1.0.0"61 }, VERSION)).toMatchSnapshot();62});63test('add babel-jest', async () => {64 expect(await updatePackageJSON(babelJestFixture)).toMatchSnapshot();65});66test('upgrades packages in ava', async () => {67 expect(await updatePackageJSON(avaFixture)).toMatchSnapshot();...
util.js
Source:util.js
1const fs = require('fs');2const path = require('path');3const util = repositoryRootPath => {4 const packageJsonFilePath = path.join(repositoryRootPath, 'package.json');5 const packageJSON = require(packageJsonFilePath);6 return {7 updatePackageJson: async function({8 moduleName,9 installed,10 latest,11 isCorePackage = false,12 packageJson = ''13 }) {14 console.log(`Bumping ${moduleName} from ${installed} to ${latest}`);15 const updatePackageJson = JSON.parse(JSON.stringify(packageJSON));16 if (updatePackageJson.dependencies[moduleName]) {17 let searchString = installed;18 // gets the exact version installed in package json for native packages19 if (!isCorePackage) {20 if (/\^|~/.test(packageJson)) {21 searchString = new RegExp(`\\${packageJson}`);22 } else {23 searchString = packageJson;24 }25 }26 updatePackageJson.dependencies[27 moduleName28 ] = updatePackageJson.dependencies[moduleName].replace(29 searchString,30 latest31 );32 }33 if (updatePackageJson.packageDependencies[moduleName]) {34 updatePackageJson.packageDependencies[35 moduleName36 ] = updatePackageJson.packageDependencies[moduleName].replace(37 new RegExp(installed),38 latest39 );40 }41 return new Promise((resolve, reject) => {42 fs.writeFile(43 packageJsonFilePath,44 JSON.stringify(updatePackageJson, null, 2),45 function(err) {46 if (err) {47 return reject(err);48 }49 console.log(`Bumped ${moduleName} from ${installed} to ${latest}`);50 return resolve();51 }52 );53 });54 },55 sleep: ms => new Promise(resolve => setTimeout(resolve, ms))56 };57};...
publish.js
Source:publish.js
...3const { join } = require('path');4function publish(dir, tag) {5 execSync(`npm publish ${dir} --access public --tag ${tag}`);6}7function updatePackageJson(packageJsonPath, version) {8 const packageJson = JSON.parse(readFileSync(packageJsonPath).toString());9 packageJson.version = version;10 writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));11}12function getProject(project) {13 const workspaceJson = JSON.parse(readFileSync('workspace.json').toString());14 return workspaceJson.projects[project];15}16const [_, _2, project] = process.argv;17const version = process.env.VERSION18const tag = process.env.TAG || 'next'19if (!project) {20 throw new Error('Need the project');21}22if (!version) {23 throw new Error('Need the version');24}25const projectMeta = getProject(project);26const outputPath = projectMeta.architect.build.options.outputPath;27if (!existsSync(outputPath)) {28 throw new Error('Must build the project first');29}30const root = projectMeta.root;31updatePackageJson(join(root, 'package.json'), version);32updatePackageJson(join(outputPath, 'package.json'), version);...
init.js
Source:init.js
1import updatePackageJson from './updatePackageJson';2import copyProjectDirectory from './copyProjectDirectory';3import openingQuestion from './openingQuestion';4import openingAnswer from './openingAnswer';5import checkReactApp from './checkReactApp';6const init = () => {7 console.log('Hey, are you ready to googlify your react App?');8 checkReactApp()9 .then(openingQuestion)10 .then(openingAnswer)11 .then(copyProjectDirectory)12 .then(updatePackageJson)13 .then(() => console.log('done'))14 .catch(console.error);15 // ask if they wish to add sample hello world app16};...
dockerize.js
Source:dockerize.js
1(function () {2 const fs = require('fs-extra');3 const path = require('path');4 updatePackageJson();5 /**6 * Update the package.json with the latest version number7 */8 function updatePackageJson() {9 const version = fs.readJSONSync('package.json').version;10 const packageJson = fs.readJSONSync(path.join('docker', 'package.json'));11 packageJson.version = version;12 packageJson.dependencies['ng-apimock'] = version;13 fs.writeJsonSync(path.join('docker', 'package.json'), packageJson);14 }...
index.js
Source:index.js
1const { updatePackageJson } = require('./updatePackageJson');23module.exports = {4 updatePackageJson,
...
Using AI Code Generation
1const path = require('path');2const { updatePackageJson } = require('storybook-root-alias');3const packageJsonPath = path.join(__dirname, 'package.json');4updatePackageJson(packageJsonPath, {5 root: path.join(__dirname, 'src'),6 alias: {7 }8});9{10 "scripts": {11 },12 "dependencies": {13 },14 "devDependencies": {},
Using AI Code Generation
1import { updatePackageJson } from 'storybook-root-alias';2import path from 'path';3updatePackageJson({4 rootPath: path.resolve(__dirname, '..', '..'),5 storybookConfigPath: path.resolve(__dirname, '..', '..', 'storybook'),6 storybookStaticDir: path.resolve(__dirname, '..', '..', 'storybook-static'),7 storybookBuildDir: path.resolve(__dirname, '..', '..', 'build-storybook'),8 storybookPublicDir: path.resolve(__dirname, '..', '..', 'public'),9 storybookPublicDir: path.resolve(__dirname, '..', '..', 'public'),10 storybookPreviewPath: path.resolve(__dirname, '..', '..', 'storybook', 'preview.js'),11 storybookTsConfigPath: path.resolve(__dirname, '..', '..', 'tsconfig.json'),12});13import { updateWebpackConfig } from 'storybook-root-alias';14import path from 'path';15const config = {16 entry: path.resolve(__dirname, '..', '..', 'src', 'index.tsx'),17 output: {18 path: path.resolve(__dirname, '..', '..', 'build'),19 },20 resolve: {21 alias: {},22 },23 module: {24 {25 test: /\.(ts|tsx)$/,26 },27 },28};29const updatedConfig = updateWebpackConfig(config, {30 rootPath: path.resolve(__dirname, '..', '..'),31 storybookConfigPath: path.resolve(__dirname, '..', '..', 'storybook'),32 storybookStaticDir: path.resolve(__dirname, '..', '..', 'storybook-static'),33 storybookBuildDir: path.resolve(__dirname, '..', '..', 'build-storybook'),34 storybookPublicDir: path.resolve(__dirname, '..', '..', 'public'),35 storybookPublicDir: path.resolve(__dirname, '..', '..', 'public'),36 storybookPreviewPath: path.resolve(__dirname, '..', '..', 'storybook', 'preview.js'),37 storybookTsConfigPath: path.resolve(__dirname, '..', '..', 'tsconfig.json'),38});39export default updatedConfig;40MIT © [Rajat Saini](
Using AI Code Generation
1import React from 'react';2import { storiesOf } from '@storybook/react';3import { action } from '@storybook/addon-actions';4import { linkTo } from '@storybook/addon-links';5import { Button, Welcome } from '@storybook/react/demo';6storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);7storiesOf('Button', module)8 .add('with text', () => (9 <Button onClick={action('clicked')}>Hello Button</Button>10 .add('with some emoji', () => (11 <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>12 ));13import React from 'react';14import { shallow } from 'enzyme';15import { Welcome } from '@storybook/react/demo';16describe('Welcome', () => {17 it('should render welcome message', () => {18 const wrapper = shallow(<Welcome showApp={jest.fn()} />);19 expect(wrapper.find('h1').text()).toMatch('Welcome to Storybook');20 });21});22import React from 'react';23import { storiesOf } from '@storybook/react';24import { action } from '@storybook/addon-actions';25import { linkTo } from '@storybook/addon-links';26import { Button, Welcome } from '@storybook/react/demo';27storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);28storiesOf('Button', module)29 .add('with text', () => (30 <Button onClick={action('clicked')}>Hello Button</Button>31 .add('with some emoji', () => (32 <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>33 ));34import React from 'react';35import { shallow } from 'enzyme';36import { Welcome } from '@storybook/react/demo';37describe('Welcome', () => {38 it('should render welcome message', () => {39 const wrapper = shallow(<Welcome showApp={jest.fn()} />);
Using AI Code Generation
1import { updatePackageJson } from 'storybook-root-decorator';2updatePackageJson({ 3 dependencies: { 4 }5});6import { storybookRootDecorator } from 'storybook-root-decorator';7storybookRootDecorator();8import { storybookRootDecorator } from 'storybook-root-decorator';9storybookRootDecorator();10import { storybookRootDecorator } from 'storybook-root-decorator';11storybookRootDecorator();12import { storybookRootDecorator } from 'storybook-root-decorator';13storybookRootDecorator();14import { storybookRootDecorator } from 'storybook-root-decorator';15storybookRootDecorator();16import { storybookRootDecorator } from 'storybook-root-decorator';17storybookRootDecorator();18import { storybookRootDecorator } from 'storybook-root-decorator';19storybookRootDecorator();20import { storybookRootDecorator } from 'storybook-root-decorator';21storybookRootDecorator();22import { storybookRootDecorator } from 'storybook-root-decorator';23storybookRootDecorator();24import { storybookRootDecorator } from 'storybook-root-decorator';25storybookRootDecorator();26import { storybookRootDecorator } from 'storybook-root-decorator';27storybookRootDecorator();28import { storybookRootDecorator } from 'storybook-root-decorator';29storybookRootDecorator();30import { storybookRootDecorator } from 'storybook-root-decorator';31storybookRootDecorator();
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!!