Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from chromaui/determine-viewlayer
Retrieve viewLayer and version from dependencies and support @web/dev-server-storybook
- Loading branch information
Showing
6 changed files
with
217 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import getStorybookInfo from './getStorybookInfo'; | ||
|
||
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 () => { | ||
const ctx = { ...context, packageJson: { dependencies: REACT } }; | ||
await expect(getStorybookInfo(ctx)).resolves.toEqual( | ||
// We're getting the result of tracing chromatic-cli's node_modules here. | ||
expect.objectContaining({ viewLayer: 'react', version: expect.any(String) }) | ||
); | ||
}); | ||
|
||
it('throws on missing dependency', async () => { | ||
await expect(getStorybookInfo(context)).rejects.toThrow('Storybook dependency not found'); | ||
}); | ||
|
||
it('warns on duplicate devDependency', async () => { | ||
const ctx = { ...context, packageJson: { dependencies: REACT, devDependencies: REACT } }; | ||
await getStorybookInfo(ctx); | ||
expect(log.warn).toHaveBeenCalledWith( | ||
expect.stringContaining('both "dependencies" and "devDependencies"') | ||
); | ||
}); | ||
|
||
it('warns on duplicate peerDependency', async () => { | ||
const ctx = { ...context, packageJson: { dependencies: REACT, peerDependencies: REACT } }; | ||
await getStorybookInfo(ctx); | ||
expect(log.warn).toHaveBeenCalledWith( | ||
expect.stringContaining('both "dependencies" and "peerDependencies"') | ||
); | ||
}); | ||
|
||
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' } }; | ||
await expect(getStorybookInfo(ctx)).resolves.toEqual( | ||
expect.objectContaining({ viewLayer: 'react', version: '3.2.1' }) | ||
); | ||
}); | ||
|
||
it('supports unscoped package name', async () => { | ||
const ctx = { ...context, env: { CHROMATIC_STORYBOOK_VERSION: 'react@3.2.1' } }; | ||
await expect(getStorybookInfo(ctx)).resolves.toEqual( | ||
expect.objectContaining({ viewLayer: 'react', version: '3.2.1' }) | ||
); | ||
}); | ||
|
||
it('throws on invalid value', async () => { | ||
const ctx = { ...context, env: { CHROMATIC_STORYBOOK_VERSION: '3.2.1' } }; | ||
await expect(getStorybookInfo(ctx)).rejects.toThrow('Invalid'); | ||
}); | ||
|
||
it('throws on unsupported viewlayer', async () => { | ||
const ctx = { ...context, env: { CHROMATIC_STORYBOOK_VERSION: '@storybook/native@3.2.1' } }; | ||
await expect(getStorybookInfo(ctx)).rejects.toThrow('Unsupported'); | ||
}); | ||
}); | ||
|
||
describe('with --storybook-build-dir', () => { | ||
it('returns viewLayer and version from packageJson', async () => { | ||
const ctx = { | ||
...context, | ||
options: { storybookBuildDir: 'storybook-static' }, | ||
packageJson: { dependencies: REACT }, | ||
}; | ||
await expect(getStorybookInfo(ctx)).resolves.toEqual( | ||
expect.objectContaining({ viewLayer: 'react', version: '1.2.3' }) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import chalk from 'chalk'; | ||
import dedent from 'ts-dedent'; | ||
|
||
import { error, info } from '../../components/icons'; | ||
import link from '../../components/link'; | ||
|
||
export default () => | ||
dedent(chalk` | ||
${error} {bold Storybook dependency not found} | ||
Could not find a supported Storybook viewlayer dependency in your {bold package.json}. | ||
Make sure you have setup Storybook and are running Chromatic from the same directory. | ||
${info} New to Storybook? Read ${link('https://www.chromatic.com/docs/storybook')} | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import noViewLayerDependency from './noViewLayerDependency'; | ||
|
||
export default { | ||
title: 'CLI/Messages/Errors', | ||
}; | ||
|
||
export const NoViewLayerDependency = () => noViewLayerDependency(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import chalk from 'chalk'; | ||
import dedent from 'ts-dedent'; | ||
|
||
import { error } from '../../components/icons'; | ||
|
||
export default (pkg) => | ||
dedent(chalk` | ||
${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. | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import noViewLayerPackage from './noViewLayerPackage'; | ||
|
||
export default { | ||
title: 'CLI/Messages/Errors', | ||
}; | ||
|
||
export const NoViewLayerPackage = () => noViewLayerPackage('@storybook/vue'); |