@testplane/storybook
Version:
Testplane plugin that enables runtime screenshot storybook tests autogeneration
43 lines • 2.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildStoryTestFiles = exports.getStoryFile = void 0;
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = __importDefault(require("lodash"));
const write_tests_file_1 = require("./write-tests-file");
const constants_1 = require("../../constants");
const testplaneTestNameSuffix = ".testplane.js";
const getStorybookTestsTmpDir = () => path_1.default.join(os_1.default.tmpdir(), constants_1.STORYBOOK_TEST_DIRNAME);
const getEmptyStorybookTestsTmpDir = async () => {
const testsTmpDir = getStorybookTestsTmpDir();
await fs_extra_1.default.emptyDir(testsTmpDir);
return testsTmpDir;
};
const getStoryFile = (test) => {
const testFile = test.file;
const testsTmpDir = getStorybookTestsTmpDir();
if (!testFile.startsWith(testsTmpDir) || !testFile.endsWith(testplaneTestNameSuffix)) {
throw new Error(`Can't get storyfile of ${test.fullTitle()}, because it was not autogenerated by storybook-testplane`);
}
return path_1.default.relative(testsTmpDir, testFile).slice(0, -testplaneTestNameSuffix.length);
};
exports.getStoryFile = getStoryFile;
const getStoryTestPath = (testsDir, story) => path_1.default.resolve(testsDir, story.importPath + testplaneTestNameSuffix);
const buildStoryTestFiles = async (stories, opts) => {
const testsDir = await getEmptyStorybookTestsTmpDir();
const groupedByTestFileStories = lodash_1.default.groupBy(stories, story => getStoryTestPath(testsDir, story));
const storyTestFiles = Object.keys(groupedByTestFileStories);
const promises = storyTestFiles.map(testFile => (0, write_tests_file_1.writeStoryTestsFile)({
testFile,
opts,
stories: groupedByTestFileStories[testFile],
}));
await Promise.all(promises);
return storyTestFiles;
};
exports.buildStoryTestFiles = buildStoryTestFiles;
//# sourceMappingURL=index.js.map