-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storyshots - Replace require_context.js with babel-plugin-require-context-hook #3757
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2a992ba
Replace require_context.js with babel-plugin-require-context-hook
igor-dv a967f81
Extract ensureOptionsDefaults
igor-dv 251eb0d
Add docs to README.md and MIGRATION.md
igor-dv 9ccbb5d
Improve READMEs
igor-dv 0690120
Remove unneeded exports
igor-dv 34a4a08
Merge remote-tracking branch 'origin/master' into storyshots-remove-r…
igor-dv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -38,6 +38,53 @@ If you aren't familiar with Jest, here are some resources: | |
|
||
> Note: If you use React 16, you'll need to follow [these additional instructions](https://github.com/facebook/react/issues/9102#issuecomment-283873039). | ||
|
||
### Configure Jest to work with Webpack's [require.context()](https://webpack.js.org/guides/dependency-management/#require-context) | ||
|
||
Sometimes it's useful to configure Storybook with Webpack's require.context feature: | ||
|
||
```js | ||
import { configure } from '@storybook/react'; | ||
|
||
const req = require.context('../stories', true, /.stories.js$/); // <- import all the stories at once | ||
|
||
function loadStories() { | ||
req.keys().forEach(filename => req(filename)); | ||
} | ||
|
||
configure(loadStories, module); | ||
``` | ||
|
||
The problem here is that it will work only during the build with webpack, | ||
other tools may lack this feature. Since Storyshot is running under Jest, | ||
we need to polyfill this functionality to work with Jest. The easiest | ||
way is to integrate it to babel. One of the possible babel plugins to | ||
polyfill this functionality might be | ||
[babel-plugin-require-context-hook](https://github.com/smrq/babel-plugin-require-context-hook). | ||
|
||
To register it, add the following to your jest setup: | ||
|
||
```js | ||
import registerRequireContextHook from 'babel-plugin-require-context-hook/register'; | ||
registerRequireContextHook(); | ||
``` | ||
|
||
And after, add the plugin to `.babelrc`: | ||
|
||
```json | ||
{ | ||
"presets": ["..."], | ||
"plugins": ["..."], | ||
"env": { | ||
"test": { | ||
"plugins": ["require-context-hook"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Make sure that it is added under the needed environment, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Make sure not to include this babel plugin in the config environment that applies to webpack" -- might be a bit clearer? |
||
otherwise it may replace a real `require.context` functionality. | ||
|
||
### Configure Jest for React | ||
StoryShots addon for React is dependent on [react-test-renderer](https://github.com/facebook/react/tree/master/packages/react-test-renderer), but | ||
[doesn't](#deps-issue) install it, so you need to install it separately. | ||
|
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
46 changes: 46 additions & 0 deletions
46
addons/storyshots/storyshots-core/src/api/ensureOptionsDefaults.js
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,46 @@ | ||
import { snapshotWithOptions } from '../test-bodies'; | ||
import Stories2SnapsConverter from '../Stories2SnapsConverter'; | ||
|
||
const ignore = ['**/node_modules/**']; | ||
const defaultStories2SnapsConverter = new Stories2SnapsConverter(); | ||
|
||
function getIntegrityOptions({ integrityOptions }) { | ||
if (integrityOptions === false) { | ||
return false; | ||
} | ||
|
||
if (typeof integrityOptions !== 'object') { | ||
return false; | ||
} | ||
|
||
return { | ||
...integrityOptions, | ||
ignore: [...ignore, ...(integrityOptions.ignore || [])], | ||
absolute: true, | ||
}; | ||
} | ||
|
||
function ensureOptionsDefaults(options) { | ||
const { | ||
suite = 'Storyshots', | ||
storyNameRegex, | ||
storyKindRegex, | ||
renderer, | ||
serializer, | ||
stories2snapsConverter = defaultStories2SnapsConverter, | ||
test: testMethod = snapshotWithOptions({ renderer, serializer }), | ||
} = options; | ||
|
||
const integrityOptions = getIntegrityOptions(options); | ||
|
||
return { | ||
suite, | ||
storyNameRegex, | ||
storyKindRegex, | ||
stories2snapsConverter, | ||
testMethod, | ||
integrityOptions, | ||
}; | ||
} | ||
|
||
export default ensureOptionsDefaults; |
19 changes: 0 additions & 19 deletions
19
addons/storyshots/storyshots-core/src/api/getIntegrityOptions.js
This file was deleted.
Oops, something went wrong.
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
43 changes: 0 additions & 43 deletions
43
addons/storyshots/storyshots-core/src/frameworks/config-loader.js
This file was deleted.
Oops, something went wrong.
25 changes: 15 additions & 10 deletions
25
addons/storyshots/storyshots-core/src/frameworks/configure.js
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 |
---|---|---|
@@ -1,22 +1,27 @@ | ||
import loadConfig from './config-loader'; | ||
import runWithRequireContext from './require_context'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
function getConfigPathParts(configPath) { | ||
const resolvedConfigPath = path.resolve(configPath); | ||
|
||
if (fs.lstatSync(resolvedConfigPath).isDirectory()) { | ||
return path.join(resolvedConfigPath, 'config.js'); | ||
} | ||
|
||
return resolvedConfigPath; | ||
} | ||
|
||
function configure(options) { | ||
const { configPath = '.storybook', config, frameworkOptions, storybook } = options; | ||
const { configPath = '.storybook', config, storybook } = options; | ||
|
||
if (config && typeof config === 'function') { | ||
config(storybook); | ||
return; | ||
} | ||
|
||
const appOptions = require.requireActual(frameworkOptions).default; | ||
|
||
const { content, contextOpts } = loadConfig({ | ||
configPath, | ||
appOptions, | ||
}); | ||
const resolvedConfigPath = getConfigPathParts(configPath); | ||
|
||
runWithRequireContext(content, contextOpts); | ||
require.requireActual(resolvedConfigPath); | ||
} | ||
|
||
export default configure; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this advice for migrating users? Perhaps something more definite would be good here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I can link to the README explanation about this. WDYT?