Add new test for when yarn search does not come up with anything
ADD
bin-src/lib/getInstalledStorybookPackages.ts
+import execa from 'execa';
+import { viewLayers } from './viewLayers';
+import { supportedAddons } from './supportedAddons';
+
+export const getInstalledStorybookInfo = async () => {
+ const { all } = await execa.command('yarn list --pattern @storybook/ --json', {
+ env: {},
+ timeout: 10000,
+ all: true,
+ shell: true,
+ });
+
+ const { data } = JSON.parse(all as any);
+ const trees: { name: string; version: string }[] = data.trees.map((p: any) => {
+ const [, name, version] = p.name.match(/(.+)@(.+)/);
+ return { name, version };
+ });
+
+ const viewLayer = trees.find(({ name }) => viewLayers[name]);
+ const addons = trees.filter(({ name }) => supportedAddons[name]);
+
+ const result = {
+ viewLayer: viewLayers[viewLayer ? viewLayer.name : ''],
+ version: viewLayer ? viewLayer.version : undefined,
+ addons: addons.map((a) => ({
+ packageVersion: a.version,
+ packageName: a.name,
+ name: supportedAddons[a.name],
+ })),
+ };
+
+ return result;
+};
+
+export default getInstalledStorybookInfo;

MODIFY
bin-src/lib/getStorybookInfo.test.ts
bin-src/lib/getStorybookInfo.test.ts
import { Context } from '../types';
import getStorybookInfo from './getStorybookInfo';
+import { getInstalledStorybookInfo } from './getInstalledStorybookPackages';
jest.useFakeTimers();
+jest.mock('./getInstalledStorybookPackages', () => {
+ const original = jest.requireActual('./getInstalledStorybookPackages'); // Step 2.
+ return {
+ __esModule: true,
+ ...original,
+ getInstalledStorybookInfo: jest.fn(() => ({
+ addons: [
+ {
+ name: 'essentials',
+ packageName: '@storybook/addon-essentials',
+ packageVersion: '6.5.5',
+ },
+ ],
+ version: '6.5.5',
+ viewLayer: 'react',
+ })),
+ };
+});
+
const log = { info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn() };
const context: Context = { env: {}, log, options: {}, packageJson: {} } as any;
const getContext = (ctx: any): Context => ({ ...context, ...ctx });
@@ -52,9 +72,15 @@ describe('getStorybookInfo', () => {
it('throws on missing package', async () => {
const ctx = getContext({ packageJson: { dependencies: VUE } });
await expect(getStorybookInfo(ctx)).resolves.toEqual({
- addons: [],
- version: null,
- viewLayer: null,
+ addons: [
+ {
+ name: 'essentials',
+ packageName: '@storybook/addon-essentials',
+ packageVersion: '6.5.5',
+ },
+ ],
+ version: '6.5.5',
+ viewLayer: 'react',
});
});
@@ -106,18 +132,30 @@ describe('getStorybookInfo', () => {
it('throws on invalid value', async () => {
const ctx = getContext({ env: { CHROMATIC_STORYBOOK_VERSION: '3.2.1' } });
await expect(getStorybookInfo(ctx)).resolves.toEqual({
- addons: [],
- version: null,
- viewLayer: null,
+ addons: [
+ {
+ name: 'essentials',
+ packageName: '@storybook/addon-essentials',
+ packageVersion: '6.5.5',
+ },
+ ],
+ version: '6.5.5',
+ viewLayer: 'react',
});
});
it('throws on unsupported viewlayer', async () => {
const ctx = getContext({ env: { CHROMATIC_STORYBOOK_VERSION: '@storybook/native@3.2.1' } });
await expect(getStorybookInfo(ctx)).resolves.toEqual({
- addons: [],
- version: null,
- viewLayer: null,
+ addons: [
+ {
+ name: 'essentials',
+ packageName: '@storybook/addon-essentials',
+ packageVersion: '6.5.5',
+ },
+ ],
+ version: '6.5.5',
+ viewLayer: 'react',
});
});
});
@@ -129,8 +167,24 @@ describe('getStorybookInfo', () => {
packageJson: { dependencies: REACT },
});
await expect(getStorybookInfo(ctx)).resolves.toEqual(
- expect.objectContaining({ viewLayer: 'react', version: '1.2.3' })
+ expect.objectContaining({ viewLayer: 'react', version: '^1.2.3' })
);
});
});
+
+ it('cannot find packages via either method', async () => {
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ getInstalledStorybookInfo.mockImplementation(() => ({
+ viewLayer: '',
+ version: undefined,
+ addons: [],
+ }));
+ const ctx = getContext({ packageJson: { dependencies: VUE } });
+ await expect(getStorybookInfo(ctx)).resolves.toEqual({
+ addons: [],
+ version: null,
+ viewLayer: null,
+ });
+ });
});

MODIFY
bin-src/lib/getStorybookInfo.ts
bin-src/lib/getStorybookInfo.ts
@@ -2,43 +2,13 @@ import fs from 'fs-extra';
import meow from 'meow';
import { parseArgsStringToArgv } from 'string-argv';
import semver from 'semver';
-import execa from 'execa';
import noViewLayerPackage from '../ui/messages/errors/noViewLayerPackage';
import { viewLayers } from './viewLayers';
import { supportedAddons } from './supportedAddons';
import { timeout, raceFulfilled } from './promises';
import { Context } from '../types';
-
-export const getInstalledStorybookInfo = async () => {
- const { all } = await execa.command('yarn list --pattern @storybook/ --json', {
- env: {},
- timeout: 10000,
- all: true,
- shell: true,
- });
-
- const { data } = JSON.parse(all as any);
- const trees: { name: string; version: string }[] = data.trees.map((p: any) => {
- const [, name, version] = p.name.match(/(.+)@(.+)/);
- return { name, version };
- });
-
- const viewLayer = trees.find(({ name }) => viewLayers[name]);
- const addons = trees.filter(({ name }) => supportedAddons[name]);
-
- const result = {
- viewLayer: viewLayers[viewLayer ? viewLayer.name : ''],
- version: viewLayer ? viewLayer.version : undefined,
- addons: addons.map((a) => ({
- packageVersion: a.version,
- packageName: a.name,
- name: supportedAddons[a.name],
- })),
- };
-
- return result;
-};
+import { getInstalledStorybookInfo } from './getInstalledStorybookPackages';
const resolvePackageJson = (pkg: string) => {
try {

Add new test for when yarn search does not come up with anything