Best JavaScript code snippet using storybook-root
storybook-metadata.test.ts
Source:storybook-metadata.test.ts
1import type { PackageJson, StorybookConfig } from '@storybook/core-common';2import path from 'path';3import { computeStorybookMetadata, metaFrameworks, sanitizeAddonName } from './storybook-metadata';4const packageJsonMock: PackageJson = {5 name: 'some-user-project',6 version: 'x.x.x',7};8const mainJsMock: StorybookConfig = {9 stories: [],10};11jest.mock('./package-versions', () => {12 const getActualPackageVersion = jest.fn((name) =>13 Promise.resolve({14 name,15 version: 'x.x.x',16 })17 );18 const getActualPackageVersions = jest.fn((packages) =>19 Promise.all(Object.keys(packages).map(getActualPackageVersion))20 );21 return {22 getActualPackageVersions,23 getActualPackageVersion,24 };25});26jest.mock('./get-monorepo-type', () => ({27 getMonorepoType: () => 'Nx',28}));29jest.mock('detect-package-manager', () => ({30 detect: () => 'Yarn',31 getNpmVersion: () => '3.1.1',32}));33describe('sanitizeAddonName', () => {34 const originalSep = path.sep;35 beforeEach(() => {36 // @ts-expect-error the property is read only but we can change it for testing purposes37 path.sep = originalSep;38 });39 test('special addon names', () => {40 const addonNames = [41 '@storybook/preset-create-react-app',42 'storybook-addon-deprecated/register',43 'storybook-addon-ends-with-js/register.js',44 '@storybook/addon-knobs/preset',45 '@storybook/addon-ends-with-js/preset.js',46 '@storybook/addon-postcss/dist/index.js',47 '../local-addon/register.js',48 '../../',49 ].map(sanitizeAddonName);50 expect(addonNames).toEqual([51 '@storybook/preset-create-react-app',52 'storybook-addon-deprecated',53 'storybook-addon-ends-with-js',54 '@storybook/addon-knobs',55 '@storybook/addon-ends-with-js',56 '@storybook/addon-postcss',57 '../local-addon',58 '../../',59 ]);60 });61 test('Windows paths', () => {62 // @ts-expect-error the property is read only but we can change it for testing purposes63 path.sep = '\\';64 const cwdMockPath = `C:\\Users\\username\\storybook-app`;65 jest.spyOn(process, `cwd`).mockImplementationOnce(() => cwdMockPath);66 expect(sanitizeAddonName(`${cwdMockPath}\\local-addon\\themes.js`)).toEqual(67 '$SNIP\\local-addon\\themes'68 );69 });70 test('Linux paths', () => {71 // @ts-expect-error the property is read only but we can change it for testing purposes72 path.sep = '/';73 const cwdMockPath = `/Users/username/storybook-app`;74 jest.spyOn(process, `cwd`).mockImplementationOnce(() => cwdMockPath);75 expect(sanitizeAddonName(`${cwdMockPath}/local-addon/themes.js`)).toEqual(76 '$SNIP/local-addon/themes'77 );78 });79});80describe('await computeStorybookMetadata', () => {81 test('should return frameworkOptions from mainjs', async () => {82 const reactResult = await computeStorybookMetadata({83 packageJson: {84 ...packageJsonMock,85 devDependencies: {86 '@storybook/react': 'x.x.x',87 },88 },89 mainConfig: {90 ...mainJsMock,91 reactOptions: {92 fastRefresh: false,93 },94 },95 });96 expect(reactResult.framework).toEqual({ name: 'react', options: { fastRefresh: false } });97 const angularResult = await computeStorybookMetadata({98 packageJson: {99 ...packageJsonMock,100 devDependencies: {101 '@storybook/angular': 'x.x.x',102 },103 },104 mainConfig: {105 ...mainJsMock,106 angularOptions: {107 enableIvy: true,108 },109 },110 });111 expect(angularResult.framework).toEqual({ name: 'angular', options: { enableIvy: true } });112 });113 test('should separate storybook packages and addons', async () => {114 const result = await computeStorybookMetadata({115 packageJson: {116 ...packageJsonMock,117 devDependencies: {118 '@storybook/react': 'x.y.z',119 '@storybook/addon-essentials': 'x.x.x',120 '@storybook/addon-knobs': 'x.x.y',121 'storybook-addon-deprecated': 'x.x.z',122 },123 },124 mainConfig: {125 ...mainJsMock,126 addons: [127 '@storybook/addon-essentials',128 'storybook-addon-deprecated/register',129 '@storybook/addon-knobs/preset',130 ],131 },132 });133 expect(result.addons).toMatchInlineSnapshot(`134 Object {135 "@storybook/addon-essentials": Object {136 "options": undefined,137 "version": "x.x.x",138 },139 "@storybook/addon-knobs": Object {140 "options": undefined,141 "version": "x.x.x",142 },143 "storybook-addon-deprecated": Object {144 "options": undefined,145 "version": "x.x.x",146 },147 }148 `);149 expect(result.storybookPackages).toMatchInlineSnapshot(`150 Object {151 "@storybook/react": Object {152 "version": "x.x.x",153 },154 }155 `);156 });157 test('should return user specified features', async () => {158 const features = {159 interactionsDebugger: true,160 };161 const result = await computeStorybookMetadata({162 packageJson: packageJsonMock,163 mainConfig: {164 ...mainJsMock,165 features,166 },167 });168 expect(result.features).toEqual(features);169 });170 test('should handle different types of builders', async () => {171 const simpleBuilder = 'webpack4';172 const complexBuilder = {173 name: 'webpack5',174 options: {175 lazyCompilation: true,176 },177 };178 expect(179 (180 await computeStorybookMetadata({181 packageJson: packageJsonMock,182 mainConfig: {183 ...mainJsMock,184 core: {185 builder: complexBuilder,186 },187 },188 })189 ).builder190 ).toEqual(complexBuilder);191 expect(192 (193 await computeStorybookMetadata({194 packageJson: packageJsonMock,195 mainConfig: {196 ...mainJsMock,197 core: {198 builder: simpleBuilder,199 },200 },201 })202 ).builder203 ).toEqual({ name: simpleBuilder });204 });205 test('should return the number of refs', async () => {206 const res = await computeStorybookMetadata({207 packageJson: packageJsonMock,208 mainConfig: {209 ...mainJsMock,210 refs: {211 a: { title: '', url: '' },212 b: { title: '', url: '' },213 },214 },215 });216 expect(res.refCount).toEqual(2);217 });218 test.each(Object.entries(metaFrameworks))(219 'should detect the supported metaframework: %s',220 async (metaFramework, name) => {221 const res = await computeStorybookMetadata({222 packageJson: {223 ...packageJsonMock,224 dependencies: {225 [metaFramework]: 'x.x.x',226 },227 },228 mainConfig: mainJsMock,229 });230 expect(res.metaFramework).toEqual({231 name,232 packageName: metaFramework,233 version: 'x.x.x',234 });235 }236 );...
storybook-metadata.ts
Source:storybook-metadata.ts
...74 ...packageJson?.peerDependencies,75 };76 const metaFramework = Object.keys(allDependencies).find((dep) => !!metaFrameworks[dep]);77 if (metaFramework) {78 const { version } = await getActualPackageVersion(metaFramework);79 metadata.metaFramework = {80 name: metaFrameworks[metaFramework],81 packageName: metaFramework,82 version,83 };84 }85 const monorepoType = getMonorepoType();86 if (monorepoType) {87 metadata.monorepo = monorepoType;88 }89 try {90 const packageManagerType = await detect({ cwd: getProjectRoot() });91 const packageManagerVerson = await getNpmVersion(packageManagerType);92 metadata.packageManager = {...
package-versions.ts
Source:package-versions.ts
1import path from 'path';2import { Dependency } from './types';3export const getActualPackageVersions = async (packages: Record<string, Partial<Dependency>>) => {4 const packageNames = Object.keys(packages);5 return Promise.all(packageNames.map(getActualPackageVersion));6};7export const getActualPackageVersion = async (packageName: string) => {8 try {9 // eslint-disable-next-line import/no-dynamic-require,global-require10 const packageJson = require(path.join(packageName, 'package.json'));11 return {12 name: packageName,13 version: packageJson.version,14 };15 } catch (err) {16 return { name: packageName, version: null };17 }...
Using AI Code Generation
1import { getActualPackageVersion } from 'storybook-root';2import { getActualPackageVersion } from 'storybook-root';3import { getActualPackageVersion } from 'storybook-root';4import { getActualPackageVersion } from 'storybook-root';5import { getActualPackageVersion } from 'storybook-root';6import { getActualPackageVersion } from 'storybook-root';7import { getActualPackageVersion } from 'storybook-root';8import { getActualPackageVersion } from 'storybook-root';9import { getActualPackageVersion } from 'storybook-root';10import { getActualPackageVersion } from 'storybook-root';11import { getActualPackageVersion } from 'storybook-root';12import { getActualPackageVersion } from 'storybook-root';13import { getActualPackageVersion } from 'storybook-root';14import { getActualPackageVersion } from 'storybook-root';15import { getActualPackageVersion } from 'storybook-root';16import { getActualPackageVersion } from 'storybook-root';17import { getActualPackageVersion } from 'storybook-root';18import { getActualPackageVersion } from '
Using AI Code Generation
1import { getActualPackageVersion } from 'storybook-root';2const version = getActualPackageVersion();3console.log(version);4{5 "scripts": {6 },7 "devDependencies": {8 },9 "dependencies": {10 }11}12{13}14export const getActualPackageVersion = () => {15 const packageJson = require('../package.json');16 return packageJson.version;17};
Using AI Code Generation
1import { getActualPackageVersion } from 'storybook-root';2getActualPackageVersion('storybook-root');3import { getActualPackageVersion } from 'storybook-root';4getActualPackageVersion('storybook-root');5import { getActualPackageVersion } from 'storybook-root';6getActualPackageVersion('storybook-root');7import { getActualPackageVersion } from 'storybook-root';8getActualPackageVersion('storybook-root');9import { getActualPackageVersion } from 'storybook-root';10getActualPackageVersion('storybook-root');11import { getActualPackageVersion } from 'storybook-root';12getActualPackageVersion('storybook-root');13import { getActualPackageVersion } from 'storybook-root';14getActualPackageVersion('storybook-root');15import { getActualPackageVersion } from 'storybook-root';16getActualPackageVersion('storybook-root');17import { getActualPackageVersion } from 'storybook-root';18getActualPackageVersion('storybook-root');
Using AI Code Generation
1const { getActualPackageVersion } = require('storybook-root');2console.log(getActualPackageVersion());3const { getPackageVersion } = require('storybook-root');4console.log(getPackageVersion());5const { getPackageVersion } = require('storybook-root');6console.log(getPackageVersion());7const { getPackageVersion } = require('storybook-root');8console.log(getPackageVersion());9const { getPackageVersion } = require('storybook-root');10console.log(getPackageVersion());11const { getPackageVersion } = require('storybook-root');12console.log(getPackageVersion());13const { getPackageVersion } = require('storybook-root');14console.log(getPackageVersion());15const { getPackageVersion } = require('storybook-root');16console.log(getPackageVersion());17const { getPackageVersion } = require('storybook-root');18console.log(getPackageVersion());19const { getPackageVersion } = require('storybook-root');20console.log(getPackageVersion());21const { getPackageVersion } = require('storybook-root');22console.log(getPackageVersion());
Using AI Code Generation
1const getActualPackageVersion = require('storybook-root').getActualPackageVersion;2console.log('Actual version of storybook-root package is : ', getActualPackageVersion());3const getActualPackageVersion = require('./package.json').version;4module.exports = {5};6const path = require('path');7const getActualPackageVersion = require(path.join(__dirname, '../../package.json')).version;8const path = require('path');9const getActualPackageVersion = require(path.join(__dirname, '../storybook-root/package.json')).version;10const path = require('path');11const getActualPackageVersion = require(path.join(__dirname, './storybook-root/package.json')).version;12const path = require('path');13const getActualPackageVersion = require(path.resolve(require.resolve('storybook-root'), '../package.json')).version;14const path = require('path');15const getActualPackageVersion = require(path.resolve(require.resolve('storybook-root'), '../package.json')).version;16const path = require('path');17const getActualPackageVersion = require(path.resolve(require.resolve('storybook-root'), '../../package.json')).version;18const path = require('path');19const getActualPackageVersion = require(path.resolve(require.resolve('storybook-root'), './package.json')).version;
Using AI Code Generation
1const { getActualPackageVersion } = require('storybook-root-cause');2const actualVersion = getActualPackageVersion('react');3console.log(actualVersion);4TypeError: (0 , _storybookRootCause.getActualPackageVersion) is not a function5const { getActualPackageVersion } = require('storybook-root-cause');6const actualVersion = getActualPackageVersion('react');7console.log(actualVersion);8TypeError: (0 , _storybookRootCause.getActualPackageVersion) is not a function9const { getActualPackageVersion } = require('storybook-root-cause');10const actualVersion = getActualPackageVersion('react');11console.log(actualVersion);12TypeError: (0 , _storybookRootCause.getActualPackageVersion) is not a function13TypeError: (0 , _storybookRootCause.getActualPackageVersion) is not a function14const { getActualPackageVersion } = require('storybook-root-cause');15const actualVersion = getActualPackageVersion('react');16console.log(actualVersion);17TypeError: (0 , _storybookRootCause.getActualPackageVersion) is not a function
Using AI Code Generation
1import { getActualPackageVersion } from 'storybook-root';2export const getActualPackageVersion = () => {3 return getActualPackageVersion()4}5{6 "scripts": {7 },8 "dependencies": {9 }10}11{12}13{14}15{16 "scripts": {17 },18 "dependencies": {19 }20}21{22}23{24 "scripts": {25 },26 "dependencies": {27 }28}
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!!