From 47c982a26409eb21ac0c8ea62276a1187a6eb05f Mon Sep 17 00:00:00 2001 From: Roman Kuznetsov Date: Tue, 23 Apr 2024 14:21:27 +0300 Subject: [PATCH] feat: add option to allow other tests execution --- src/config.ts | 2 ++ src/index.ts | 6 +++++- src/utils.test.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++-- src/utils.ts | 29 ++++++++++++++++++++-------- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/src/config.ts b/src/config.ts index e9d66ff..c3f63ce 100644 --- a/src/config.ts +++ b/src/config.ts @@ -55,6 +55,7 @@ export interface PluginConfig { localport: number; remoteStorybookUrl: string; browserIds: Array; + unsafeAllowOtherTests: boolean; } export type PluginPartialConfig = Partial; @@ -70,6 +71,7 @@ export function parseConfig(options: PluginPartialConfig): PluginConfig { localport: numberOption("localport", 6006), remoteStorybookUrl: stringOption("remoteStorybookUrl", ""), browserIds: stringAndRegExpArrayOption("browserIds", []), + unsafeAllowOtherTests: booleanOption("unsafeAllowOtherTests", false), }), { envPrefix: "testplane_storybook_", cliPrefix: "--storybook-" }, ); diff --git a/src/index.ts b/src/index.ts index 84bce10..c482866 100644 --- a/src/index.ts +++ b/src/index.ts @@ -81,8 +81,12 @@ function onTestplaneMaster(testplane: Testplane, config: PluginConfig): void { const storyTestFiles = await buildStoryTestFiles(stories, { autoScreenshots: config.autoScreenshots }); patchTestplaneBaseUrl(testplane.config, iframeUrl); - patchTestplaneSets(testplane.config, config.browserIds, storyTestFiles); disableTestplaneIsolation(testplane.config, config.browserIds); + patchTestplaneSets(testplane.config, { + browserIds: config.browserIds, + files: storyTestFiles, + unsafeAllowOtherTests: config.unsafeAllowOtherTests, + }); }); testplane.on(testplane.events.AFTER_TESTS_READ, testCollection => { diff --git a/src/utils.test.ts b/src/utils.test.ts index 6baa437..8d7fa41 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -64,7 +64,7 @@ describe("utils", () => { const browserIds = ["chrome"]; const files = ["file1", "file2"]; - patchTestplaneSets(config, browserIds, files); + patchTestplaneSets(config, { browserIds, files }); expect(config.sets).toEqual({ [STORYBOOK_SET_NAME]: { @@ -84,7 +84,7 @@ describe("utils", () => { }); const files = ["file1", "file2"]; - patchTestplaneSets(config, [], files); + patchTestplaneSets(config, { browserIds: [], files }); expect(config.sets).toEqual({ [STORYBOOK_SET_NAME]: { @@ -93,6 +93,50 @@ describe("utils", () => { }, }); }); + + it("should overwrite sets completely by default", () => { + const config = getConfig_({ + browsers: { + chrome: {}, + firefox: {}, + }, + sets: { "my-storybook-set": { files: ["my-files"], browsers: ["my-browsers"] } }, + }); + const files = ["file1", "file2"]; + + patchTestplaneSets(config, { browserIds: [], files }); + + expect(config.sets).toEqual({ + [STORYBOOK_SET_NAME]: { + browsers: ["chrome", "firefox"], + files: ["file1", "file2"], + }, + }); + }); + + it("should not overwrite sets completely with 'unsafeAllowOtherTests'", () => { + const config = getConfig_({ + browsers: { + chrome: {}, + firefox: {}, + }, + sets: { "my-storybook-set": { files: ["my-files"], browsers: ["my-browsers"] } }, + }); + const files = ["file1", "file2"]; + + patchTestplaneSets(config, { browserIds: [], files, unsafeAllowOtherTests: true }); + + expect(config.sets).toEqual({ + [STORYBOOK_SET_NAME]: { + browsers: ["chrome", "firefox"], + files: ["file1", "file2"], + }, + "my-storybook-set": { + browsers: ["my-browsers"], + files: ["my-files"], + }, + }); + }); }); describe("patchTestplaneBaseUrl", () => { diff --git a/src/utils.ts b/src/utils.ts index 7903420..ccd729a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -38,15 +38,28 @@ export const getStorybookPathEndingWith = (url: string, pathEnding: string): str return urlObj.toString(); }; -export const patchTestplaneSets = (config: Config, browserIds: Array, files: string[]): void => { +export const patchTestplaneSets = ( + config: Config, + { + browserIds, + files, + unsafeAllowOtherTests = false, + }: { browserIds: Array; files: string[]; unsafeAllowOtherTests?: boolean }, +): void => { const browsers = getFilteredBrowserIds(config, browserIds); - - config.sets = { - [STORYBOOK_SET_NAME]: { - browsers, - files, - }, - }; + const autoStorybookSet = { browsers, files }; + + if (unsafeAllowOtherTests) { + config.sets = config.sets || {}; + config.sets[STORYBOOK_SET_NAME] = autoStorybookSet; + } else { + config.sets = { + [STORYBOOK_SET_NAME]: { + browsers, + files, + }, + }; + } }; export const patchTestplaneBaseUrl = (config: Config, baseUrl: string): void => {