@testplane/storybook
Version:
Testplane plugin that enables runtime screenshot storybook tests autogeneration
94 lines • 4.51 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStoryFile = void 0;
const config_1 = require("./config");
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const get_stories_1 = require("./storybook/get-stories");
const story_to_test_1 = require("./storybook/story-to-test");
const utils_2 = require("./utils");
const dev_server_1 = require("./storybook/dev-server");
__exportStar(require("./types"), exports);
var story_to_test_2 = require("./storybook/story-to-test");
Object.defineProperty(exports, "getStoryFile", { enumerable: true, get: function () { return story_to_test_2.getStoryFile; } });
exports.default = (testplane, opts) => {
const config = (0, config_1.parseConfig)(opts);
if (!config.enabled || !process.argv.includes("--storybook")) {
return;
}
if (testplane.isWorker()) {
onTestplaneWorker(testplane);
}
else {
onTestplaneMaster(testplane, config);
}
};
function onTestplaneWorker(testplane) {
testplane.on(testplane.events.NEW_BROWSER, async (browser) => {
const commandName = "assertView";
const withStoryDefaults = (opts = {}) => {
const executionContext = browser.executionContext;
return Object.assign(Object.assign({}, (executionContext["@testplane/storybook-assertView-opts"] || {})), opts);
};
browser.overwriteCommand(commandName, (baseAssertView, name, selector, opts = {}, ...rest) => {
return baseAssertView(name, selector, withStoryDefaults(opts), ...rest);
});
browser.overwriteCommand(commandName, (baseAssertView, name, opts = {}, ...rest) => {
return baseAssertView(name, withStoryDefaults(opts), ...rest);
}, true);
});
}
function onTestplaneMaster(testplane, config) {
const sharedData = { devServer: null };
testplane.on(testplane.events.CLI, commander => {
commander.option("--storybook", "run storybook tests");
});
testplane.on(testplane.events.INIT, async () => {
const isLocal = !config.remoteStorybookUrl;
if (isLocal) {
sharedData.devServer = await (0, dev_server_1.getStorybookDevServer)(testplane, config.localport, config.storybookConfigDir);
}
const storybookUrl = isLocal ? `http://127.0.0.1:${config.localport}` : config.remoteStorybookUrl;
const iframeUrl = (0, utils_1.getStorybookPathEndingWith)(storybookUrl, constants_1.STORYBOOK_IFRAME_PATH);
const stories = await (0, get_stories_1.getStories)(storybookUrl, config.waitStorybookJsonTimeout);
const storyTestFiles = await (0, story_to_test_1.buildStoryTestFiles)(stories, {
autoScreenshots: config.autoScreenshots,
autoscreenshotSelector: config.autoscreenshotSelector,
autoScreenshotStorybookGlobals: config.autoScreenshotStorybookGlobals,
});
(0, utils_2.patchTestplaneBaseUrl)(testplane.config, iframeUrl);
(0, utils_2.disableTestplaneIsolation)(testplane.config, config.browserIds);
(0, utils_2.patchSystemExtensions)(testplane.config);
(0, utils_2.patchTestplaneSets)(testplane.config, {
browserIds: config.browserIds,
files: storyTestFiles,
unsafeAllowOtherTests: config.unsafeAllowOtherTests,
});
});
testplane.on(testplane.events.AFTER_TESTS_READ, testCollection => {
testCollection.eachTest(test => {
if (test.skipReason === "Skipped by mocha interface") {
test.skipReason = "Skipped in storybook file";
}
});
});
process.on("exit", async () => {
var _a;
(_a = sharedData.devServer) === null || _a === void 0 ? void 0 : _a.kill("SIGKILL");
});
}
//# sourceMappingURL=index.js.map