-
Notifications
You must be signed in to change notification settings - Fork 32
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
add storybook snapshot #458
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e6b6691
chore: add test workspace .storybook directory
naporin0624 64acc81
chore: create storybook setup file
naporin0624 d906b87
feat: create storybook snapshot test
naporin0624 52b06f7
chore: icon disable parameter
naporin0624 1d837e7
chore: rename snapshot ext
naporin0624 fd99299
chore: change ext
naporin0624 57511a3
chore: update snapshot
naporin0624 4e19010
chore: remove storyshots modules
naporin0624 8c327e3
chore: add @types/jest-specific-snapshot
naporin0624 ef5961b
chore: refactor storybook.test.ts
naporin0624 4b2bb5a
chore: change snapshot ext
naporin0624 3c5c25c
chore: update snapshot
naporin0624 dfd979e
chore: add build to test job
naporin0624 8942bd7
chore: remove snap ext snapshot
naporin0624 234d218
Merge branch 'main' of ssh://github.com/pixiv/charcoal into feat/add-…
naporin0624 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,12 @@ | ||
import { config } from '../jest.config.mjs' | ||
|
||
export default { | ||
...config(), | ||
moduleNameMapper: { | ||
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': | ||
'<rootDir>/../__mocks__/file.ts', | ||
'\\.(styl|css|less|scss)$': '<rootDir>/../__mocks__/style.ts', | ||
'\\.(mdx)$': '<rootDir>/../__mocks__/mdx.ts', | ||
}, | ||
setupFilesAfterEnv: ['../jest.setup.ts', './jest.setup.ts'], | ||
} |
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 @@ | ||
import { setProjectAnnotations } from '@storybook/react' | ||
|
||
import * as projectAnnotations from './preview' | ||
|
||
setProjectAnnotations(projectAnnotations) | ||
|
||
globalThis.location = { | ||
...globalThis.location, | ||
search: '', | ||
} | ||
window.location = { | ||
...window.location, | ||
search: '', | ||
} | ||
|
||
global.IntersectionObserver = jest.fn().mockImplementation(() => ({ | ||
observe() { | ||
return null | ||
}, | ||
disconnect() { | ||
return null | ||
}, | ||
})) | ||
|
||
global.matchMedia = jest.fn().mockImplementation(() => ({ | ||
matches: true, | ||
media: '(max-width: 600px)', | ||
addEventListener() { | ||
// Do Nothing | ||
}, | ||
removeEventListener() { | ||
// Do Nothing | ||
}, | ||
})) |
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. |
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,97 @@ | ||
import path from 'node:path' | ||
import * as glob from 'glob' | ||
|
||
import { composeStories } from '@storybook/react' | ||
import renderer from 'react-test-renderer' | ||
import { addSerializer } from 'jest-specific-snapshot' | ||
import { styleSheetSerializer } from 'jest-styled-components' | ||
|
||
import type { Meta, StoryFn } from '@storybook/react' | ||
|
||
addSerializer(styleSheetSerializer) | ||
|
||
type StoryFile = { | ||
default: Meta | ||
[name: string]: StoryFn | Meta | ||
} | ||
|
||
const compose = ( | ||
entry: StoryFile | ||
): ReturnType<typeof composeStories<StoryFile>> => { | ||
try { | ||
return composeStories(entry) | ||
} catch (e) { | ||
throw new Error( | ||
`There was an issue composing stories for the module: ${JSON.stringify( | ||
entry | ||
)}, ${e}` | ||
) | ||
} | ||
} | ||
|
||
function getAllStoryFiles() { | ||
// Place the glob you want to match your stories files | ||
const storyFiles = glob.sync( | ||
path.join(__dirname, '../packages/**/*.{stories,story}.{js,jsx,mjs,ts,tsx}') | ||
) | ||
|
||
return storyFiles.map((filePath) => { | ||
const storyFile = require(filePath) | ||
return { filePath, storyFile } | ||
}) | ||
} | ||
|
||
// Recreate similar options to Storyshots. Place your configuration below | ||
const options = { | ||
suite: 'Storybook Tests', | ||
storyKindRegex: /^.*?DontTest$/, | ||
storyNameRegex: /UNSET/, | ||
snapshotsDirName: '__snapshots__', | ||
snapshotExtension: 'storyshot', | ||
} | ||
|
||
describe(options.suite, () => { | ||
getAllStoryFiles().forEach(({ storyFile, filePath }) => { | ||
const meta = storyFile.default | ||
const title = meta.title || filePath | ||
const dir = path.join(path.dirname(filePath), options.snapshotsDirName) | ||
const filename = [ | ||
path.basename(filePath, '.tsx'), | ||
options.snapshotExtension, | ||
].join('.') | ||
const snapshotPath = path.join(dir, filename) | ||
|
||
if ( | ||
options.storyKindRegex.test(title) || | ||
meta.parameters?.storyshots?.disable | ||
) { | ||
return | ||
} | ||
|
||
describe(title, () => { | ||
const stories = Object.entries(compose(storyFile)) | ||
.map(([name, story]) => ({ name, story })) | ||
.filter(({ name, story }) => { | ||
return ( | ||
!options.storyNameRegex.test(name) && | ||
!story.parameters.storyshots?.disable | ||
) | ||
}) | ||
|
||
if (stories.length <= 0) { | ||
throw new Error( | ||
`No stories found for this module: ${title}. Make sure there is at least one valid story for this module, without a disable parameter, or add parameters.storyshots.disable in the default export of this file.` | ||
) | ||
} | ||
|
||
for (const { name, story } of stories) { | ||
const testFn = story.parameters.storyshots?.skip ? test.skip : test | ||
|
||
testFn(name, async () => { | ||
const mounted = renderer.create(story()).toJSON() | ||
expect(mounted).toMatchSpecificSnapshot(snapshotPath) | ||
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. story があるディレクトリに snapshots を作成し個別に snapshot を生成する これしかなかったのでこれを使用したが他にいいものがあれたら変えたい |
||
}) | ||
} | ||
}) | ||
}) | ||
}) |
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
2 changes: 1 addition & 1 deletion
2
packages/react-sandbox/src/components/Carousel/__snapshots__/index.story.storyshot
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
2 changes: 1 addition & 1 deletion
2
packages/react-sandbox/src/components/HintText/__snapshots__/index.story.storyshot
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.
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.
https://storybook.js.org/docs/writing-tests/storyshots-migration-guide#import-project-level-annotations-from-storybook