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
...132 version: undefined,133 };134 });135 }136 const addonVersions = await getActualPackageVersions(addons);137 addonVersions.forEach(({ name, version }) => {138 addons[name].version = version;139 });140 const addonNames = Object.keys(addons);141 // all Storybook deps minus the addons142 const storybookPackages = Object.keys(allDependencies)143 .filter((dep) => dep.includes('storybook') && !addonNames.includes(dep))144 .reduce((acc, dep) => {145 return {146 ...acc,147 [dep]: { version: undefined },148 };149 }, {}) as Record<string, Dependency>;150 const storybookPackageVersions = await getActualPackageVersions(storybookPackages);151 storybookPackageVersions.forEach(({ name, version }) => {152 storybookPackages[name].version = version;153 });154 const language = allDependencies.typescript ? 'typescript' : 'javascript';155 const hasStorybookEslint = !!allDependencies['eslint-plugin-storybook'];156 // FIXME: resolve framework/renderer split in 7.0157 // OR should be getting this from mainConfig instead?158 const storybookInfo = getStorybookInfo(packageJson);159 const storybookVersion =160 storybookPackages[storybookInfo.frameworkPackage]?.version || storybookInfo.version;161 return {162 ...metadata,163 storybookVersion,164 language,...
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
1const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;2const actualVersions = getActualPackageVersions();3console.log(actualVersions);4const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;5const actualVersions = getActualPackageVersions();6console.log(actualVersions);7const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;8const actualVersions = getActualPackageVersions();9console.log(actualVersions);10const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;11const actualVersions = getActualPackageVersions();12console.log(actualVersions);13const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;14const actualVersions = getActualPackageVersions();15console.log(actualVersions);16const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;17const actualVersions = getActualPackageVersions();18console.log(actualVersions);19const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;20const actualVersions = getActualPackageVersions();21console.log(actualVersions);22const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;23const actualVersions = getActualPackageVersions();24console.log(actualVersions);25const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;26const actualVersions = getActualPackageVersions();27console.log(actualVersions);28const getActualPackageVersions = require('storybook-root-deps').getActualPackageVersions;29const actualVersions = getActualPackageVersions();30console.log(actualVersions);
Using AI Code Generation
1const getActualPackageVersions = require('storybook-root').getActualPackageVersions;2const actualPackageVersions = getActualPackageVersions();3console.log(actualPackageVersions);4{5}6const getPackageJson = require('storybook-root').getPackageJson;7const packageJson = getPackageJson();8console.log(packageJson);9{10 "scripts": {11 },12 "repository": {
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const getActualPackageVersions = storybookRoot.getActualPackageVersions;3const actualPackageVersions = getActualPackageVersions();4const storybookRoot = require('storybook-root');5const getPackageVersion = storybookRoot.getPackageVersion;6const packageVersion = getPackageVersion('storybook-root');7const storybookRoot = require('storybook-root');8const getPackageVersions = storybookRoot.getPackageVersions;9const packageVersions = getPackageVersions('storybook-root');10const storybookRoot = require('storybook-root');11const getRoot = storybookRoot.getRoot;12const root = getRoot();13const storybookRoot = require('storybook-root');14const getRootPackage = storybookRoot.getRootPackage;15const rootPackage = getRootPackage();16const storybookRoot = require('storybook-root');17const getRootPackageVersion = storybookRoot.getRootPackageVersion;18const rootPackageVersion = getRootPackageVersion();19const storybookRoot = require('storybook-root');20const getRootPackageVersions = storybookRoot.getRootPackageVersions;21const rootPackageVersions = getRootPackageVersions();22const storybookRoot = require('storybook-root');23const isPackageInstalled = storybookRoot.isPackageInstalled;24const isInstalled = isPackageInstalled('storybook-root');25const storybookRoot = require('storybook-root');26const isPackageVersionInstalled = storybookRoot.isPackageVersionInstalled;
Using AI Code Generation
1const { getActualPackageVersions } = require('storybook-root');2const actualPackageVersions = getActualPackageVersions();3console.log(actualPackageVersions);4const { getActualPackageVersions } = require('storybook-root');5const actualPackageVersions = getActualPackageVersions();6console.log(actualPackageVersions);7const { getActualPackageVersions } = require('storybook-root');8const actualPackageVersions = getActualPackageVersions();9console.log(actualPackageVersions);10const { getActualPackageVersions } = require('storybook-root');11const actualPackageVersions = getActualPackageVersions();12console.log(actualPackageVersions);13const { getActualPackageVersions } = require('storybook-root');14const actualPackageVersions = getActualPackageVersions();15console.log(actualPackageVersions);16const { getActualPackageVersions } = require('storybook-root');17const actualPackageVersions = getActualPackageVersions();18console.log(actualPackageVersions);19const { getActualPackageVersions } = require('storybook-root');20const actualPackageVersions = getActualPackageVersions();21console.log(actualPackageVersions);
Using AI Code Generation
1const { getActualPackageVersions } = require('storybook-root');2console.log(getActualPackageVersions());3const { getActualPackageVersions } = require('storybook-root');4console.log(getActualPackageVersions());5const { getActualPackageVersions } = require('storybook-root');6console.log(getActualPackageVersions());7const { getActualPackageVersions } = require('storybook-root');8console.log(getActualPackageVersions());9const { getActualPackageVersions } = require('storybook-root');10console.log(getActualPackageVersions());11const { getActualPackageVersions } = require('storybook-root');12console.log(getActualPackageVersions());13const { getActualPackageVersions } = require('storybook-root');14console.log(getActualPackageVersions());15const { getActualPackageVersions } = require('storybook-root');16console.log(getActualPackageVersions());
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!!