Skip to content
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 15 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ jobs:
cache: yarn

- run: yarn install --immutable --inline-builds
- run: |
yarn test
yarn storyshots
- run: yarn build
- run: yarn test
env:
CI: 'true'

Expand Down
12 changes: 12 additions & 0 deletions .storybook/jest.config.mjs
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'],
}
34 changes: 34 additions & 0 deletions .storybook/jest.setup.ts
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)
Comment on lines +1 to +5
Copy link
Member Author

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

If you need project-level annotations (e.g., decorators, args, styles) enabled in your ./storybook/preview.js|ts included in your tests, adjust your test set up file to import the annotations as follows:


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
},
}))
97 changes: 97 additions & 0 deletions .storybook/storybook.test.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

story があるディレクトリに snapshots を作成し個別に snapshot を生成する

これしかなかったのでこれを使用したが他にいいものがあれたら変えたい
https://github.com/igor-dv/jest-specific-snapshot

})
}
})
})
})
2 changes: 1 addition & 1 deletion jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type { import('@jest/types').Config.InitialOptions } */
const rootConfig = {
projects: ['packages/*'],
projects: ['packages/*', '.storybook'],
passWithNoTests: true,
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"build": "yarn workspaces foreach -vpt -j unlimited run build",
"clean": "yarn workspaces foreach -vp run clean",
"test": "jest",
"storyshots": "jest -c storyshots.config.mjs",
"test:strict": "USE_STRICT=1 jest",
"typecheck": "yarn workspaces foreach -vp run typecheck",
"typecheck:config": "tsc",
Expand Down Expand Up @@ -49,7 +48,6 @@
"@storybook/addon-interactions": "^7.4.1",
"@storybook/addon-links": "^7.4.1",
"@storybook/addon-onboarding": "^1.0.8",
"@storybook/addon-storyshots": "7.4.1",
"@storybook/addon-storysource": "^7.4.1",
"@storybook/addon-styling": "^1.3.7",
"@storybook/addon-styling-webpack": "^0.0.5",
Expand All @@ -64,6 +62,7 @@
"@stylelint/postcss-css-in-js": "^0.37.2",
"@types/eslint": "^8.4.1",
"@types/jest": "^27.4.0",
"@types/jest-specific-snapshot": "^0.5.9",
"@types/node": "^17.0.13",
"@types/prettier": "^2.4.3",
"@types/styled-components": "^5.1.21",
Expand All @@ -83,6 +82,7 @@
"glob": "^7.2.0",
"husky": "^4.3.8",
"jest": "^27.4.7",
"jest-specific-snapshot": "^8.0.0",
"jsdom": "^19.0.0",
"lerna": "^6.6.2",
"npm-run-all": "^4.1.5",
Expand Down
6 changes: 6 additions & 0 deletions packages/icons/src/PixivIcon.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default {
},
},
},

parameters: {
storyshots: {
disable: true,
},
},
}

const groupedIcons = KNOWN_ICON_FILES.reduce<Record<string, KnownIconFile[]>>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Sandbox/Carousel Carousel 1`] = `
exports[`Storybook Tests Sandbox/Carousel _Carousel 1`] = `
.c12 {
stroke: none;
fill: currentColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Sandbox/Carousel/CarouselButton Carousel Button 1`] = `
exports[`Storybook Tests Sandbox/Carousel/CarouselButton _CarouselButton 1`] = `
.c3 {
stroke: none;
fill: currentColor;
Expand Down Expand Up @@ -99,7 +99,7 @@ exports[`Storyshots Sandbox/Carousel/CarouselButton Carousel Button 1`] = `
</div>
`;

exports[`Storyshots Sandbox/Carousel/CarouselButton Scroll Hint Button 1`] = `
exports[`Storybook Tests Sandbox/Carousel/CarouselButton _ScrollHintButton 1`] = `
.c2 {
stroke: none;
fill: currentColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Sandbox/Filter Button 1`] = `
exports[`Storybook Tests Sandbox/Filter Button 1`] = `
.c0 {
display: block;
outline: none;
Expand Down Expand Up @@ -45,7 +45,7 @@ exports[`Storyshots Sandbox/Filter Button 1`] = `
</div>
`;

exports[`Storyshots Sandbox/Filter Default 1`] = `
exports[`Storybook Tests Sandbox/Filter Default 1`] = `
.c3 {
display: block;
outline: none;
Expand Down Expand Up @@ -193,7 +193,7 @@ exports[`Storyshots Sandbox/Filter Default 1`] = `
</div>
`;

exports[`Storyshots Sandbox/Filter Link 1`] = `
exports[`Storybook Tests Sandbox/Filter Link 1`] = `
.c0 {
display: block;
outline: none;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Sandbox/HintText Default 1`] = `
exports[`Storybook Tests Sandbox/HintText Default 1`] = `
.c2 {
stroke: none;
fill: currentColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Sandbox/Layout Basic 1`] = `
exports[`Storybook Tests Sandbox/Layout Basic 1`] = `
.c2 {
display: -webkit-box;
display: -webkit-flex;
Expand Down Expand Up @@ -441,7 +441,7 @@ exports[`Storyshots Sandbox/Layout Basic 1`] = `
</div>
`;

exports[`Storyshots Sandbox/Layout Center 1`] = `
exports[`Storybook Tests Sandbox/Layout Center 1`] = `
.c0 {
display: -webkit-box;
display: -webkit-flex;
Expand Down Expand Up @@ -650,7 +650,7 @@ exports[`Storyshots Sandbox/Layout Center 1`] = `
</div>
`;

exports[`Storyshots Sandbox/Layout No Menu 1`] = `
exports[`Storybook Tests Sandbox/Layout NoMenu 1`] = `
.c0 {
display: -webkit-box;
display: -webkit-flex;
Expand Down Expand Up @@ -989,7 +989,7 @@ exports[`Storyshots Sandbox/Layout No Menu 1`] = `
</div>
`;

exports[`Storyshots Sandbox/Layout Wide 1`] = `
exports[`Storybook Tests Sandbox/Layout Wide 1`] = `
.c2 {
display: -webkit-box;
display: -webkit-flex;
Expand Down