@storybook/addon-storyshots
Version:
Take a code snapshot of every story automatically with Jest
61 lines (60 loc) • 2.36 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stories2SnapsConverter = void 0;
const path_1 = __importDefault(require("path"));
const ts_dedent_1 = require("ts-dedent");
const defaultOptions = {
snapshotsDirName: '__snapshots__',
snapshotExtension: '.storyshot',
storiesExtensions: ['.js', '.jsx', '.ts', '.tsx', '.mdx'],
};
class Stories2SnapsConverter {
constructor(options = {}) {
this.getSnapshotExtension = () => this.options.snapshotExtension;
this.options = {
...defaultOptions,
...options,
};
}
getStoryshotFile(fileName) {
const { dir, name } = path_1.default.parse(fileName);
const { snapshotsDirName, snapshotExtension } = this.options;
// Convert to absolute path, in case jest is not running in CWD,
// else it will create snapshots with the wrong path
const absDir = path_1.default.isAbsolute(dir) ? dir : path_1.default.resolve(dir);
return path_1.default.format({
dir: path_1.default.join(absDir, snapshotsDirName),
name,
ext: snapshotExtension,
});
}
getSnapshotFileName(context) {
const { fileName, kind } = context;
if (!fileName) {
// eslint-disable-next-line no-console
console.warn((0, ts_dedent_1.dedent) `
Storybook was unable to detect filename for stories of kind "${kind}".
To fix it, add following to your jest.config.js:
transform: {
// should be above any other js transform like babel-jest
'^.+\\.stories\\.js$': '@storybook/addon-storyshots/injectFileName',
}
`);
return undefined;
}
return this.getStoryshotFile(fileName);
}
getPossibleStoriesFiles(storyshotFile) {
const { dir, name } = path_1.default.parse(storyshotFile);
const { storiesExtensions } = this.options;
return storiesExtensions.map((ext) => path_1.default.format({
dir: path_1.default.dirname(dir),
name,
ext,
}));
}
}
exports.Stories2SnapsConverter = Stories2SnapsConverter;
;