Skip to content

Instantly share code, notes, and snippets.

@interactivellama
Last active December 9, 2022 05:13
Show Gist options
  • Save interactivellama/6ddaf519469460f2bd890cb5ff68789a to your computer and use it in GitHub Desktop.
Save interactivellama/6ddaf519469460f2bd890cb5ff68789a to your computer and use it in GitHub Desktop.
/*
* 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