@storybook/addon-storyshots
Version:
Take a code snapshot of every story automatically with Jest
83 lines (82 loc) • 3.77 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const global_1 = require("@storybook/global");
const preview_api_1 = require("@storybook/preview-api");
const ensureOptionsDefaults_1 = __importDefault(require("./ensureOptionsDefaults"));
const snapshotsTestsTemplate_1 = __importDefault(require("./snapshotsTestsTemplate"));
const integrityTestTemplate_1 = __importDefault(require("./integrityTestTemplate"));
const frameworkLoader_1 = __importDefault(require("../frameworks/frameworkLoader"));
const { describe, window: globalWindow } = global_1.global;
const methods = ['beforeAll', 'beforeEach', 'afterEach', 'afterAll'];
function callTestMethodGlobals(testMethod) {
methods.forEach((method) => {
if (typeof testMethod[method] === 'function') {
// @ts-expect-error (ignore)
global_1.global[method](testMethod[method], testMethod[method].timeout);
}
});
}
const isDisabled = (parameter) => parameter === false || (parameter && parameter.disable === true);
function testStorySnapshots(options = {}) {
if (typeof describe !== 'function') {
throw new Error('testStorySnapshots is intended only to be used inside jest');
}
preview_api_1.addons.setChannel((0, preview_api_1.mockChannel)());
const { storybook, framework, renderTree, renderShallowTree } = (0, frameworkLoader_1.default)(options);
const { asyncJest, suite, storyNameRegex, storyKindRegex, stories2snapsConverter, testMethod, integrityOptions, snapshotSerializers, } = (0, ensureOptionsDefaults_1.default)(options);
const testMethodParams = {
renderTree,
renderShallowTree,
stories2snapsConverter,
};
// NOTE: as the store + preview's initialization process entirely uses
// `SychronousPromise`s in the v6 store case, the callback to the `then()` here
// will run *immediately* (in the same tick), and thus the `snapshotsTests`, and
// subsequent calls to `it()` etc will all happen within this tick, which is required
// by Jest (cannot add tests asynchronously)
globalWindow.__STORYBOOK_STORY_STORE__.initializationPromise.then(() => {
const data = storybook.raw()?.reduce((acc, item) => {
if (storyNameRegex && !item.name.match(storyNameRegex)) {
return acc;
}
if (storyKindRegex && !item.kind.match(storyKindRegex)) {
return acc;
}
const { kind, storyFn: render, parameters } = item;
const existing = acc.find((i) => i.kind === kind);
const { fileName } = item.parameters;
if (!isDisabled(parameters.storyshots)) {
if (existing) {
existing.children.push({ ...item, render, fileName });
}
else {
acc.push({
kind,
children: [{ ...item, render, fileName }],
});
}
}
return acc;
}, []);
if (data && data.length) {
callTestMethodGlobals(testMethod);
(0, snapshotsTestsTemplate_1.default)({
data,
asyncJest,
suite,
framework,
testMethod,
testMethodParams,
snapshotSerializers,
});
(0, integrityTestTemplate_1.default)(integrityOptions, stories2snapsConverter);
}
else {
throw new Error('storyshots found 0 stories');
}
});
}
exports.default = testStorySnapshots;
;