Skip to content

Commit 3a94059

Browse files
feat: add ability to disable storybook globals with null
1 parent 1ba50d6 commit 3a94059

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
12
module.exports = {
23
clearMocks: true,
34
globals: {
45
"ts-jest": {
5-
"tsconfig": "tsconfig.spec.json",
6+
tsconfig: "tsconfig.spec.json",
7+
isolatedModules: true,
68
},
79
},
810
preset: "ts-jest",

src/storybook/story-test-runner/extend-stories.test.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("storybook/story-test-runner/extend-stories", () => {
3939
expect(extendedStories[0].browserIds).toBe(null);
4040
});
4141

42-
it("should overlay story configs / file confifs / default configs", () => {
42+
it("should overlay story configs over file configs", () => {
4343
const customTests = {};
4444
const defaultExport = {
4545
testplane: { browserIds: ["firefox"] },
@@ -59,7 +59,7 @@ describe("storybook/story-test-runner/extend-stories", () => {
5959

6060
const extendedStories = extendStoriesFromStoryFile(stories, { requireFn });
6161

62-
expect(extendedStories).toEqual([
62+
expect(extendedStories).toMatchObject([
6363
{
6464
name: "foo",
6565
absolutePath: "not/existing.js",
@@ -72,12 +72,39 @@ describe("storybook/story-test-runner/extend-stories", () => {
7272
ignoreDiffPixelCount: 10,
7373
},
7474
autoScreenshots: false,
75-
autoscreenshotSelector: null,
7675
autoScreenshotStorybookGlobals: { dark: { theme: "dark" } },
7776
},
7877
]);
7978
});
8079

80+
it("should overlay story configs over default configs", () => {
81+
const customTests = {};
82+
const fooExport = {
83+
testplane: customTests,
84+
testplaneConfig: { skip: false, autoScreenshots: false, assertViewOpts: { ignoreElements: ["foobar"] } },
85+
};
86+
const requireFn = mkRequireStub_().mockReturnValue({ default: {}, foo: fooExport });
87+
const stories = [{ name: "foo", absolutePath: "not/existing.js" }] as StorybookStory[];
88+
89+
const extendedStories = extendStoriesFromStoryFile(stories, { requireFn });
90+
91+
expect(extendedStories).toMatchObject([
92+
{
93+
name: "foo",
94+
absolutePath: "not/existing.js",
95+
extraTests: {},
96+
skip: false,
97+
browserIds: null,
98+
assertViewOpts: {
99+
ignoreElements: ["foobar"],
100+
},
101+
autoScreenshots: false,
102+
autoscreenshotSelector: null,
103+
autoScreenshotStorybookGlobals: {},
104+
},
105+
]);
106+
});
107+
81108
it("should fallback reading story configs from deprecated testplane property", () => {
82109
const stories = [{ name: "foo", absolutePath: "not/existing.js" }] as StorybookStory[];
83110
const requireFn = mkRequireStub_().mockReturnValue({ default: { testplane: { skip: true } }, foo: {} });

src/storybook/story-test-runner/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ function createTestplaneTests(
2727
story.autoScreenshotStorybookGlobals,
2828
autoScreenshotStorybookGlobals,
2929
);
30-
const screenshotGlobalSetNames = Object.keys(rawAutoScreenshotGlobalSets);
30+
31+
const screenshotGlobalSetNames = Object.keys(rawAutoScreenshotGlobalSets).filter(name =>
32+
Boolean(rawAutoScreenshotGlobalSets[name]),
33+
);
3134

3235
const autoScreenshotGlobalSets = screenshotGlobalSetNames.length
3336
? screenshotGlobalSetNames.map(name => ({ name, globals: rawAutoScreenshotGlobalSets[name] }))
@@ -41,7 +44,7 @@ function createTestplaneTests(
4144
async function (ctx: TestFunctionExtendedCtx) {
4245
ctx.expect = globalThis.expect;
4346

44-
const result = await openStoryStep(ctx.browser, story, globals);
47+
const result = await openStoryStep(ctx.browser, story, globals as Record<string, unknown>);
4548
const selector = story.autoscreenshotSelector || autoscreenshotSelector || result.rootSelector;
4649

4750
await autoScreenshotStep(ctx.browser, selector);

src/storybook/story-test-runner/inheritable-values.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22
export type Inheritable<T extends Record<any, any>> = T | ((baseValue: T) => T);
33

4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5+
const isFunc = (obj?: unknown): obj is (...args: any[]) => any => typeof obj === "function";
6+
47
// Acts like "Object.assign", but:
58
// - Does not mutate arguments
69
// - Supports functions (previousValue) => nextValue
@@ -22,13 +25,13 @@ export function inheritValue<T extends Record<any, any>>(
2225
resultValue = curValue;
2326
}
2427

25-
if (typeof prevValue !== "function" && typeof curValue !== "function") {
28+
if (!isFunc(prevValue) && !isFunc(curValue)) {
2629
resultValue = Object.assign({}, prevValue, curValue);
27-
} else if (typeof prevValue === "function" && typeof curValue === "function") {
30+
} else if (isFunc(prevValue) && isFunc(curValue)) {
2831
resultValue = (baseValue: T) => curValue(prevValue(structuredClone(baseValue)));
29-
} else if (typeof prevValue === "function" && typeof curValue !== "function") {
32+
} else if (isFunc(prevValue) && !isFunc(curValue)) {
3033
resultValue = (baseValue: T) => Object.assign({}, prevValue(structuredClone(baseValue)), curValue);
31-
} else if (typeof prevValue !== "function" && typeof curValue === "function") {
34+
} else if (!isFunc(prevValue) && isFunc(curValue)) {
3235
resultValue = (baseValue: T) => curValue(structuredClone(Object.assign({}, baseValue, prevValue)));
3336
}
3437
}

src/storybook/story-test-runner/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { TestplaneTestFunction } from "../../types";
33
import type { StorybookStoryExtended as StorybookStory } from "../get-stories";
44
import type { Inheritable } from "./inheritable-values";
55

6-
export type AutoScreenshotStorybookGlobals = Record<string, Record<string, unknown>>;
6+
export type AutoScreenshotStorybookGlobals = Record<string, null | Record<string, unknown>>;
77

88
export interface StorybookStoryExtraProperties {
99
skip: boolean;

0 commit comments

Comments
 (0)