Last active
December 9, 2022 05:13
-
-
Save interactivellama/6ddaf519469460f2bd890cb5ff68789a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* STORY-BASED SNAPSHOT TESTING | |
* | |
* This uses StoryShots to iterate over stories created in Storybook to | |
* automatically create DOM and image snapshot tests. | |
* | |
* For more information, please visit: | |
* https://github.com/storybooks/storybook/tree/master/addons/storyshots | |
*/ | |
import express from 'express'; | |
import initStoryshots, { imageSnapshot, getSnapshotFileName } from '@storybook/addon-storyshots'; | |
import path from 'path'; | |
import { shallow } from 'enzyme'; | |
import toJson from 'enzyme-to-json'; | |
// Express server setup. `npm run storyshots:build` must | |
// be run first. | |
const rootPath = path.resolve(__dirname, '../'); | |
const app = express(); | |
const port = process.env.PORT || 9001; | |
// Register static asset folders | |
app.use( | |
'/assets', | |
express.static( | |
`${rootPath}/node_modules/@salesforce-ux/design-system/assets/` | |
) | |
); | |
app.use(express.static(`${rootPath}/storybook-based-tests`)); | |
// Run SLDS validation tests on Storybook stories | |
initStoryshots({ | |
configPath: '.storybook-based-tests', | |
suite: 'SLDS validation', | |
test: ({ story, context }) => { | |
const snapshotFileName = getSnapshotFileName(context); | |
const storyElement = story.render(context); | |
const shallowTree = shallow(storyElement); // Enzyme | |
console.log(storyElement); // root DOM element (like React test util's render()) | |
console.log(shallowTree.html()); // Enzyme HTML markup | |
console.log(toJson(shallowTree)); // Enzyme JSON nested objects | |
if (snapshotFileName) { | |
expect([Boolean value]); | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment