-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Addon-docs: Jest MDX transform for storyshots #8189
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
76efa89
Addon-docs: Add jest-transform.js to convert .mdx => .js for storyshots
ndelangen 5d3e5da
misc: remove dependency on `deasyncPromise`
edelgado 245501b
misc: make filename more specific
edelgado 9d5bea0
chore: add instructions for MDX transform
edelgado e0595df
Merge branch 'next' into jest-transform
shilman 41039d0
Try adding an mdx story
Hypnosphi 606d1ce
Support custom babel-jest transforms
Hypnosphi bc92cf0
Add MDX snapshotting to HTML example (failed)
shilman 4797a17
Merge branch 'next' into jest-transform
shilman 2893357
Remove extra transform
shilman 510c39b
Merge branch 'next' into jest-transform
shilman 9512b9a
Storyshots: Add MDX to known stories extensions
shilman 3392ffd
Merge branch 'next' into jest-transform
shilman c0eafce
Exampes/Angular-cli: Fix angularshots for MDX
shilman 96c2c1a
Examples/Vue: Disable MDX storyshots temporarily
shilman 5950378
Update snapshots
shilman 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const path = require('path'); | ||
const mdx = require('@mdx-js/mdx'); | ||
const { ScriptTransformer } = require('@jest/transform'); | ||
const { dedent } = require('ts-dedent'); | ||
|
||
const createCompiler = require('./mdx-compiler-plugin'); | ||
|
||
const compilers = [createCompiler({})]; | ||
|
||
const getNextTransformer = (filename, config) => { | ||
const extension = path.extname(filename); | ||
const jsFileName = `${filename.slice(0, -extension.length)}.js`; | ||
const self = config.transform.find(([pattern]) => new RegExp(pattern).test(filename)); | ||
const jsTransforms = config.transform.filter(([pattern]) => new RegExp(pattern).test(jsFileName)); | ||
return new ScriptTransformer({ | ||
...config, | ||
transform: [ | ||
...config.transform.filter(entry => entry !== self), | ||
...jsTransforms.map(([pattern, ...rest]) => [self[0], ...rest]), | ||
], | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
process(src, filename, config, { instrument }) { | ||
const result = dedent` | ||
/* @jsx mdx */ | ||
import React from 'react' | ||
import { mdx } from '@mdx-js/react' | ||
${mdx.sync(src, { compilers, filepath: filename })} | ||
`; | ||
return getNextTransformer(filename, config).transformSource(filename, result, instrument); | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = ['@storybook/addon-docs/react/preset']; |
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 |
---|---|---|
|
@@ -178,6 +178,23 @@ StoryShots addon for Preact is dependent on [preact-render-to-json](https://gith | |
yarn add preact-render-to-json --dev | ||
``` | ||
|
||
### Configure Jest for MDX Docs Add-On Stories | ||
|
||
If using the [Docs add-on](../../docs/README.md) with | ||
[MDX stories](../../docs/docs/mdx.md) you will need | ||
to configure Jest to transform MDX stories into something Storyshots can understand: | ||
|
||
Add the following to your Jest configuration: | ||
|
||
```json | ||
{ | ||
"transform": { | ||
"^.+\\.[tj]sx?$": "babel-jest", | ||
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. Isn't the pipe symbol necessary? [t|j] |
||
"^.+\\.mdx?$": "@storybook/addon-docs/jest-transform-mdx" | ||
} | ||
} | ||
``` | ||
|
||
### <a name="deps-issue"></a>Why don't we install dependencies of each framework ? | ||
Storyshots addon is currently supporting React, Angular and Vue. Each framework needs its own packages to be integrated with Jest. We don't want people that use only React will need to bring other dependencies that do not make sense for them. | ||
|
||
|
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
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.
Isn't there a pipe symbol missing: [t|j] ? As here: https://github.com/facebook/jest/tree/master/packages/babel-jest#setup