-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
075f911
commit 9fdc7a2
Showing
18 changed files
with
792 additions
and
46 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
docs/snippets/common/individual-snapshot-tests-portable-stories.jest.js.mdx
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,69 @@ | ||
```js | ||
// storybook.test.js | ||
import path from 'path'; | ||
import * as glob from 'glob'; | ||
|
||
//👇 Augment expect with jest-specific-snapshot | ||
import 'jest-specific-snapshot'; | ||
|
||
import { describe, test, expect } from '@jest/globals'; | ||
|
||
// Replace your-testing-library with one of the supported testing libraries (e.g., react, vue) | ||
import { render } from '@testing-library/your-testing-library'; | ||
|
||
// Adjust the import based on the supported framework or Storybook's testing libraries (e.g., react, vue3) | ||
import { composeStories } from '@storybook/your-framework'; | ||
|
||
const compose = (entry) => { | ||
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, 'stories/**/*.{stories,story}.{js,jsx,mjs,ts,tsx}'), | ||
); | ||
|
||
return storyFiles.map((filePath) => { | ||
const storyFile = require(filePath); | ||
const storyDir = path.dirname(filePath); | ||
const componentName = path.basename(filePath).replace(/\.(stories|story)\.[^/.]+$/, ''); | ||
|
||
return { filePath, storyFile, storyDir, componentName }; | ||
}); | ||
} | ||
|
||
describe('Stories Snapshots', () => { | ||
getAllStoryFiles().forEach(({ storyFile, componentName }) => { | ||
const meta = storyFile.default; | ||
const title = meta.title || componentName; | ||
|
||
describe(title, () => { | ||
const stories = Object.entries(compose(storyFile)).map(([name, story]) => ({ name, story })); | ||
|
||
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.`, | ||
); | ||
} | ||
|
||
stories.forEach(({ name, story }) => { | ||
test(name, async () => { | ||
const mounted = render(story()); | ||
// Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot. | ||
await new Promise((resolve) => setTimeout(resolve, 1)); | ||
// Defines the custom snapshot path location and file name | ||
const customSnaphotPath = `./__snapshots__/${componentName}.test.js.snap`; | ||
expect(mounted.container).toMatchSpecificSnapshot(customSnaphotPath); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
``` |
82 changes: 82 additions & 0 deletions
82
docs/snippets/common/individual-snapshot-tests-portable-stories.jest.ts.mdx
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,82 @@ | ||
```ts | ||
// storybook.test.ts | ||
// Replace your-framework with one of the supported Storybook frameworks (react, vue3) | ||
import type { Meta, StoryFn } from '@storybook/your-framework'; | ||
|
||
import path from "path"; | ||
import * as glob from "glob"; | ||
|
||
//👇 Augment expect with jest-specific-snapshot | ||
import "jest-specific-snapshot"; | ||
|
||
import { describe, test, expect } from "@jest/globals"; | ||
|
||
// Replace your-testing-library with one of the supported testing libraries (e.g., react, vue) | ||
import { render } from '@testing-library/your-testing-library'; | ||
|
||
// Adjust the import based on the supported framework or Storybook's testing libraries (e.g., react, vue3) | ||
import { composeStories } from '@storybook/your-framework'; | ||
|
||
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, 'stories/**/*.{stories,story}.{js,jsx,mjs,ts,tsx}'), | ||
); | ||
|
||
return storyFiles.map((filePath) => { | ||
const storyFile = require(filePath); | ||
const storyDir = path.dirname(filePath); | ||
const componentName = path | ||
.basename(filePath) | ||
.replace(/\.(stories|story)\.[^/.]+$/, ""); | ||
|
||
return { filePath, storyFile, storyDir, componentName }; | ||
}); | ||
} | ||
|
||
describe("Stories Snapshots", () => { | ||
getAllStoryFiles().forEach(({ storyFile, componentName }) => { | ||
const meta = storyFile.default; | ||
const title = meta.title || componentName; | ||
|
||
describe(title, () => { | ||
const stories = Object.entries(compose(storyFile)).map( | ||
([name, story]) => ({ name, story }) | ||
); | ||
|
||
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.` | ||
); | ||
} | ||
|
||
stories.forEach(({ name, story }) => { | ||
test(name, async () => { | ||
const mounted = render(story()); | ||
// Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot. | ||
await new Promise((resolve) => setTimeout(resolve, 1)); | ||
// Defines the custom snapshot path location and file name | ||
const customSnaphotPath = `./__snapshots__/${componentName}.test.ts.snap`; | ||
expect(mounted.container).toMatchSpecificSnapshot(customSnaphotPath); | ||
}); | ||
}); | ||
}); | ||
}); | ||
``` |
64 changes: 64 additions & 0 deletions
64
docs/snippets/common/individual-snapshot-tests-portable-stories.vitest.js.mdx
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,64 @@ | ||
```js | ||
// storybook.test.js | ||
// @vitest-environment jsdom | ||
|
||
import path from 'path'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
// Replace your-testing-library with one of the supported testing libraries (e.g., react, vue) | ||
import { render } from '@testing-library/your-testing-library'; | ||
|
||
// Adjust the import based on the supported framework or Storybook's testing libraries (e.g., react, vue3) | ||
import { composeStories } from '@storybook/your-framework'; | ||
|
||
const compose = (entry) => { | ||
try { | ||
return composeStories(entry); | ||
} catch (error) { | ||
throw new Error( | ||
`There was an issue composing stories for the module: ${JSON.stringify(entry)}, ${error}`, | ||
); | ||
} | ||
}; | ||
function getAllStoryFiles() { | ||
// Place the glob you want to match your story files | ||
const storyFiles = Object.entries( | ||
import.meta.glob('./stories/**/*.(stories|story).@(js|jsx|mjs|ts|tsx)', { | ||
eager: true, | ||
}), | ||
); | ||
|
||
return storyFiles.map(([filePath, storyFile]) => { | ||
const storyDir = path.dirname(filePath); | ||
const componentName = path.basename(filePath).replace(/\.(stories|story)\.[^/.]+$/, ''); | ||
return { filePath, storyFile, componentName, storyDir }; | ||
}); | ||
} | ||
describe('Stories Snapshots', () => { | ||
getAllStoryFiles().forEach(({ storyFile, componentName }) => { | ||
const meta = storyFile.default; | ||
const title = meta.title || componentName; | ||
|
||
describe(title, () => { | ||
const stories = Object.entries(compose(storyFile)).map(([name, story]) => ({ name, story })); | ||
|
||
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.`, | ||
); | ||
} | ||
|
||
stories.forEach(({ name, story }) => { | ||
test(name, async () => { | ||
const mounted = render(story()); | ||
// Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot. | ||
await new Promise((resolve) => setTimeout(resolve, 1)); | ||
// Defines the custom snapshot path location and file name | ||
const customSnaphotPath = `./__snapshots__/${componentName}.spec.js.snap`; | ||
expect(mounted.container).toMatchFileSnapshot(customSnaphotPath); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
``` |
74 changes: 74 additions & 0 deletions
74
docs/snippets/common/individual-snapshot-tests-portable-stories.vitest.ts.mdx
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,74 @@ | ||
```ts | ||
// storybook.test.ts | ||
// @vitest-environment jsdom | ||
|
||
// Replace your-framework with one of the supported Storybook frameworks (react, vue3) | ||
import type { Meta, StoryFn } from '@storybook/your-framework'; | ||
|
||
import path from 'path'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
// Replace your-testing-library with one of the supported testing libraries (e.g., react, vue) | ||
import { render } from '@testing-library/your-testing-library'; | ||
|
||
// Adjust the import based on the supported framework or Storybook's testing libraries (e.g., react, vue3) | ||
import { composeStories } from '@storybook/your-framework'; | ||
|
||
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 story files | ||
const storyFiles = Object.entries( | ||
import.meta.glob<StoryFile>('./stories/**/*.(stories|story).@(js|jsx|mjs|ts|tsx)', { | ||
eager: true, | ||
}), | ||
); | ||
|
||
return storyFiles.map(([filePath, storyFile]) => { | ||
const storyDir = path.dirname(filePath); | ||
const componentName = path.basename(filePath).replace(/\.(stories|story)\.[^/.]+$/, ''); | ||
return { filePath, storyFile, componentName, storyDir }; | ||
}); | ||
} | ||
|
||
describe('Stories Snapshots', () => { | ||
getAllStoryFiles().forEach(({ storyFile, componentName }) => { | ||
const meta = storyFile.default; | ||
const title = meta.title || componentName; | ||
|
||
describe(title, () => { | ||
const stories = Object.entries(compose(storyFile)).map(([name, story]) => ({ name, story })); | ||
|
||
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.`, | ||
); | ||
} | ||
|
||
stories.forEach(({ name, story }) => { | ||
test(name, async () => { | ||
const mounted = render(story()); | ||
// Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot. | ||
await new Promise((resolve) => setTimeout(resolve, 1)); | ||
// Defines the custom snapshot path location and file name | ||
const customSnaphotPath = `./__snapshots__/${componentName}.spec.ts.snap`; | ||
expect(mounted.container).toMatchFileSnapshot(customSnaphotPath); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
``` |
64 changes: 64 additions & 0 deletions
64
docs/snippets/common/snapshot-tests-portable-stories.jest.js.mdx
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,64 @@ | ||
```js | ||
// storybook.test.js | ||
import path from 'path'; | ||
import * as glob from 'glob'; | ||
|
||
import { describe, test, expect } from '@jest/globals'; | ||
|
||
// Replace your-testing-library with one of the supported testing libraries (e.g., react, vue) | ||
import { render } from '@testing-library/your-testing-library'; | ||
|
||
// Adjust the import based on the supported framework or Storybook's testing libraries (e.g., react, vue3) | ||
import { composeStories } from '@storybook/your-framework'; | ||
|
||
const compose = (entry) => { | ||
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, 'stories/**/*.{stories,story}.{js,jsx,mjs,ts,tsx}'), | ||
); | ||
|
||
return storyFiles.map((filePath) => { | ||
const storyFile = require(filePath); | ||
const storyDir = path.dirname(filePath); | ||
const componentName = path.basename(filePath).replace(/\.(stories|story)\.[^/.]+$/, ''); | ||
|
||
return { filePath, storyFile, storyDir, componentName }; | ||
}); | ||
} | ||
|
||
describe('Stories Snapshots', () => { | ||
getAllStoryFiles().forEach(({ storyFile, componentName }) => { | ||
const meta = storyFile.default; | ||
const title = meta.title || componentName; | ||
|
||
describe(title, () => { | ||
const stories = Object.entries(compose(storyFile)).map(([name, story]) => ({ name, story })); | ||
|
||
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.`, | ||
); | ||
} | ||
|
||
stories.forEach(({ name, story }) => { | ||
test(name, async () => { | ||
const mounted = render(story()); | ||
// Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot. | ||
await new Promise((resolve) => setTimeout(resolve, 1)); | ||
expect(mounted.container).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
``` |
Oops, something went wrong.