Skip to content

Commit

Permalink
Check that the viewLayer specified in package.json is installed, not …
Browse files Browse the repository at this point in the history
…another one
  • Loading branch information
ghengeveld committed Apr 23, 2021
1 parent 216e3a8 commit 4f994b1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
19 changes: 4 additions & 15 deletions bin/lib/getStorybookInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ const timeout = (count) =>
setTimeout(() => rej(new Error('The attempt to find the Storybook version timed out')), count);
});

const neverResolve = new Promise(() => {});
const disregard = () => neverResolve;

const findViewlayer = async ({ env, log, options, packageJson }) => {
// Allow setting Storybook version via CHROMATIC_STORYBOOK_VERSION='@storybook/react@4.0-alpha.8' for unusual cases
if (env.CHROMATIC_STORYBOOK_VERSION) {
Expand Down Expand Up @@ -114,19 +111,11 @@ const findViewlayer = async ({ env, log, options, packageJson }) => {
if (options.storybookBuildDir) return { viewLayer, version };

// Try to find the viewlayer package in node_modules so we know it's installed
const findings = Object.entries(viewLayers).map(([pk, name]) => [resolve(pk), name]);
const rejectedFindings = findings.map(([p]) => p.then(disregard, () => true));
const allFailed = Promise.all(rejectedFindings).then(() => {
throw new Error(noViewLayerPackage());
});

return Promise.race([
...findings.map(([promise, name]) =>
promise
.then(fs.readJson, disregard)
.then((pkgJson) => ({ viewLayer: name, version: pkgJson.version }))
),
allFailed,
resolve(pkg)
.then(fs.readJson)
.then((json) => ({ viewLayer, version: json.version }))
.catch(() => Promise.reject(new Error(noViewLayerPackage(pkg)))),
timeout(10000),
]);
};
Expand Down
10 changes: 7 additions & 3 deletions bin/lib/getStorybookInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const log = { warn: jest.fn() };
const context = { env: {}, log, options: {}, packageJson: {} };

const REACT = { '@storybook/react': '1.2.3' };
const VUE = { '@storybook/vue': '1.2.3' };

describe('getStorybookInfo', () => {
it('returns viewLayer and version', async () => {
Expand All @@ -15,9 +16,7 @@ describe('getStorybookInfo', () => {
});

it('throws on missing dependency', async () => {
await expect(getStorybookInfo(context)).rejects.toThrow(
'Could not find a supported Storybook viewlayer package in your package.json dependencies'
);
await expect(getStorybookInfo(context)).rejects.toThrow('Storybook dependency not found');
});

it('warns on duplicate devDependency', async () => {
Expand All @@ -36,6 +35,11 @@ describe('getStorybookInfo', () => {
);
});

it('throws on missing package', async () => {
const ctx = { ...context, packageJson: { dependencies: VUE } };
await expect(getStorybookInfo(ctx)).rejects.toThrow('Storybook package not installed');
});

describe('with CHROMATIC_STORYBOOK_VERSION', () => {
it('returns viewLayer and version from env', async () => {
const ctx = { ...context, env: { CHROMATIC_STORYBOOK_VERSION: '@storybook/react@3.2.1' } };
Expand Down
6 changes: 3 additions & 3 deletions bin/ui/messages/errors/noViewLayerPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import dedent from 'ts-dedent';

import { error } from '../../components/icons';

export default () =>
export default (pkg) =>
dedent(chalk`
${error} {bold Storybook package not found}
Could not find a supported Storybook viewlayer package in {bold node_modules}.
${error} {bold Storybook package not installed}
Could not find {bold ${pkg}} in {bold node_modules}.
Most likely you forgot to run {bold npm install} or {bold yarn} before running Chromatic.
`);
2 changes: 1 addition & 1 deletion bin/ui/messages/errors/noViewLayerPackage.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export default {
title: 'CLI/Messages/Errors',
};

export const NoViewLayerPackage = () => noViewLayerPackage();
export const NoViewLayerPackage = () => noViewLayerPackage('@storybook/vue');

0 comments on commit 4f994b1

Please sign in to comment.