Skip to content

Commit

Permalink
Storyshots: First-class CSF support (#8000)
Browse files Browse the repository at this point in the history
Storyshots: First-class CSF support
  • Loading branch information
shilman committed Oct 7, 2019
1 parent fd3b297 commit 39b8c80
Show file tree
Hide file tree
Showing 79 changed files with 2,539 additions and 1,849 deletions.
15 changes: 7 additions & 8 deletions addons/storyshots/storyshots-core/.storybook/config.js
@@ -1,10 +1,9 @@
import { configure } from '@storybook/react';

const req = require.context('../stories/required_with_context', true, /\.stories\.js$/);

function loadStories() {
req.keys().forEach(filename => req(filename));
require('../stories/directly_required');
}

configure(loadStories, module);
configure(
[
require.context('../stories/required_with_context', false, /\.stories\.js$/),
require.context('../stories/directly_required', false, /index\.js$/),
],
module
);
9 changes: 5 additions & 4 deletions addons/storyshots/storyshots-core/.storybook/configTest.js
@@ -1,11 +1,12 @@
import { configure } from '@storybook/react';

const req = require.context('../stories/required_with_context', true, /\.stories\.js$/);
const req = require.context('../stories/required_with_context', false, /\.stories\.js$/);

function loadStories() {
req.keys().forEach(filename => req(filename));
const loadStories = () => {
const result = req.keys().map(filename => req(filename));
// eslint-disable-next-line global-require
require('../stories/directly_required');
}
return result;
};

configure(loadStories, module);
10 changes: 10 additions & 0 deletions addons/storyshots/storyshots-core/README.md
Expand Up @@ -588,6 +588,16 @@ Like the default, but allows you to specify a set of options for the renderer, j
### `multiSnapshotWithOptions(options)`

Like `snapshotWithOptions`, but generate a separate snapshot file for each stories file rather than a single monolithic file (as is the convention in Jest). This makes it dramatically easier to review changes. If you'd like the benefit of separate snapshot files, but don't have custom options to pass, simply pass an empty object.
If you use [Component Story Format](https://storybook.js.org/docs/formats/component-story-format/), you may also need to add an additional Jest transform to automate detecting story file names:
```js
// jest.config.js
module.exports = {
transform: {
'^.+\\.stories\\.jsx?$': '@storybook/addon-storyshots/injectFileName',
'^.+\\.jsx?$': 'babel-jest',
},
};
```

#### integrityOptions

Expand Down
23 changes: 23 additions & 0 deletions addons/storyshots/storyshots-core/injectFileName.js
@@ -0,0 +1,23 @@
const { ScriptTransformer } = require('@jest/transform');

const getNextTransformer = (fileName, config) => {
const self = config.transform.find(([pattern]) => new RegExp(pattern).test(fileName));
return new ScriptTransformer({
...config,
transform: config.transform.filter(entry => entry !== self),
});
};

module.exports = {
process(src, fileName, config, { instrument }) {
const transformer = getNextTransformer(fileName, config);
const { code } = transformer.transformSource(fileName, src, instrument);

return `${code};
if(exports.default != null) {
exports.default.parameters = exports.default.parameters || {};
exports.default.parameters.fileName = '${fileName}';
}
`;
},
};
4 changes: 3 additions & 1 deletion addons/storyshots/storyshots-core/package.json
Expand Up @@ -29,13 +29,15 @@
"storybook": "start-storybook -p 6006"
},
"dependencies": {
"@jest/transform": "^24.9.0",
"@storybook/addons": "5.2.1",
"core-js": "^3.0.1",
"glob": "^7.1.3",
"global": "^4.3.2",
"jest-specific-snapshot": "^2.0.0",
"read-pkg-up": "^6.0.0",
"regenerator-runtime": "^0.12.1"
"regenerator-runtime": "^0.12.1",
"ts-dedent": "^1.1.0"
},
"devDependencies": {
"enzyme-to-json": "^3.3.5",
Expand Down
14 changes: 13 additions & 1 deletion addons/storyshots/storyshots-core/src/Stories2SnapsConverter.js
@@ -1,4 +1,5 @@
import path from 'path';
import dedent from 'ts-dedent';

const defaultOptions = {
snapshotsDirName: '__snapshots__',
Expand All @@ -24,9 +25,20 @@ class DefaultStories2SnapsConverter {
}

getSnapshotFileName(context) {
const { fileName } = context;
const { fileName, kind } = context;

if (!fileName) {
// eslint-disable-next-line no-console
console.warn(
dedent`
Storybook was unable to detect filename for stories of kind "${kind}".
To fix it, add following to your jest.config.js:
transform: {
// should be above any other js transform like babel-jest
'^.+\\\\.stories\\\\.js$': '@storybook/addon-storyshots/injectFileName',
}
`
);
return null;
}

Expand Down
Expand Up @@ -53,13 +53,13 @@ exports[`Storyshots Another Button with text 1`] = `
</Button>
`;

exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = `
exports[`Storyshots Async With Timeout 1`] = `
<AsyncTestComponent>
<h1 />
</AsyncTestComponent>
`;

exports[`Storyshots Button with some emoji 1`] = `
exports[`Storyshots Button With Some Emoji 1`] = `
<Button
onClick={[Function]}
>
Expand Down Expand Up @@ -88,7 +88,7 @@ exports[`Storyshots Button with some emoji 1`] = `
</Button>
`;

exports[`Storyshots Button with text 1`] = `
exports[`Storyshots Button With Text 1`] = `
<Button
onClick={[Function]}
>
Expand All @@ -112,7 +112,7 @@ exports[`Storyshots Button with text 1`] = `
</Button>
`;

exports[`Storyshots Welcome to Storybook 1`] = `
exports[`Storyshots Welcome To Storybook 1`] = `
<Welcome
showApp={[Function]}
>
Expand Down
Expand Up @@ -45,13 +45,13 @@ exports[`Storyshots Another Button with text 1`] = `
</button>
`;

exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = `
exports[`Storyshots Async With Timeout 1`] = `
<h1>
</h1>
`;

exports[`Storyshots Button with some emoji 1`] = `
exports[`Storyshots Button With Some Emoji 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -76,7 +76,7 @@ exports[`Storyshots Button with some emoji 1`] = `
</button>
`;

exports[`Storyshots Button with text 1`] = `
exports[`Storyshots Button With Text 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -96,7 +96,7 @@ exports[`Storyshots Button with text 1`] = `
</button>
`;

exports[`Storyshots Welcome to Storybook 1`] = `
exports[`Storyshots Welcome To Storybook 1`] = `
<Main>
<Title>
Welcome to storybook
Expand Down
Expand Up @@ -45,13 +45,13 @@ exports[`Storyshots Another Button with text 1`] = `
</button>
`;

exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = `
exports[`Storyshots Async With Timeout 1`] = `
<h1>
</h1>
`;

exports[`Storyshots Button with some emoji 1`] = `
exports[`Storyshots Button With Some Emoji 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -76,7 +76,7 @@ exports[`Storyshots Button with some emoji 1`] = `
</button>
`;

exports[`Storyshots Button with text 1`] = `
exports[`Storyshots Button With Text 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -96,7 +96,7 @@ exports[`Storyshots Button with text 1`] = `
</button>
`;

exports[`Storyshots Welcome to Storybook 1`] = `
exports[`Storyshots Welcome To Storybook 1`] = `
<Main>
<Title>
Welcome to storybook
Expand Down
Expand Up @@ -45,13 +45,13 @@ exports[`Storyshots Another Button with text 1`] = `
</button>
`;

exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = `
exports[`Storyshots Async With Timeout 1`] = `
<h1>
</h1>
`;

exports[`Storyshots Button with some emoji 1`] = `
exports[`Storyshots Button With Some Emoji 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -76,7 +76,7 @@ exports[`Storyshots Button with some emoji 1`] = `
</button>
`;

exports[`Storyshots Button with text 1`] = `
exports[`Storyshots Button With Text 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -96,7 +96,7 @@ exports[`Storyshots Button with text 1`] = `
</button>
`;

exports[`Storyshots Welcome to Storybook 1`] = `
exports[`Storyshots Welcome To Storybook 1`] = `
<article
style={
Object {
Expand Down
@@ -1,5 +1,4 @@
import React from 'react';
import { storiesOf } from '@storybook/react';

export const EXPECTED_VALUE = 'THIS IS SO DONE';
export const TIMEOUT = 5;
Expand All @@ -26,6 +25,12 @@ class AsyncTestComponent extends React.Component {
}
}

storiesOf('Async', module).add(`with ${TIMEOUT}ms timeout simulating async operation`, () => (
<AsyncTestComponent />
));
export default {
title: 'Async',
includeStories: ['withTimeout'],
};

export const withTimeout = () => <AsyncTestComponent />;
withTimeout.story = {
name: `with ${TIMEOUT}ms timeout simulating async operation`,
};
@@ -1,18 +1,26 @@
import React from 'react';

import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import { Button } from '@storybook/react/demo';

storiesOf('Button', module)
.addParameters({
export default {
title: 'Button',

parameters: {
component: Button,
})
.add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>)
.add('with some emoji', () => (
<Button onClick={action('clicked')}>
<span role="img" aria-label="so cool">
😀 😎 👍 💯
</span>
</Button>
));
},
};

export const withText = () => <Button onClick={action('clicked')}>Hello Button</Button>;

export const withSomeEmoji = () => (
<Button onClick={action('clicked')}>
<span role="img" aria-label="so cool">
😀 😎 👍 💯
</span>
</Button>
);

withSomeEmoji.story = {
name: 'with some emoji',
};
@@ -1,11 +1,18 @@
import React from 'react';

import { storiesOf } from '@storybook/react';
import { linkTo } from '@storybook/addon-links';
import { Welcome } from '@storybook/react/demo';

storiesOf('Welcome', module)
.addParameters({
export default {
title: 'Welcome',

parameters: {
component: Welcome,
})
.add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);
},
};

export const toStorybook = () => <Welcome showApp={linkTo('Button')} />;

toStorybook.story = {
name: 'to Storybook',
};
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = `
exports[`Storyshots Async With Timeout 1`] = `
<AsyncTestComponent>
<h1>
THIS IS SO DONE
Expand Down
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = `
exports[`Storyshots Async With Timeout 1`] = `
<h1>

</h1>
Expand Down
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Async with 5ms timeout simulating async operation 1`] = `
exports[`Storyshots Async With Timeout 1`] = `
<h1>

</h1>
Expand Down
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Button with some emoji 1`] = `
exports[`Storyshots Button With Some Emoji 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -25,7 +25,7 @@ exports[`Storyshots Button with some emoji 1`] = `
</button>
`;

exports[`Storyshots Button with text 1`] = `
exports[`Storyshots Button With Text 1`] = `
<button
onClick={[Function]}
style={
Expand Down
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Button with some emoji 1`] = `
exports[`Storyshots Button With Some Emoji 1`] = `
<button
onClick={[Function]}
style={
Expand All @@ -25,7 +25,7 @@ exports[`Storyshots Button with some emoji 1`] = `
</button>
`;

exports[`Storyshots Button with text 1`] = `
exports[`Storyshots Button With Text 1`] = `
<button
onClick={[Function]}
style={
Expand Down

0 comments on commit 39b8c80

Please sign in to comment.