Skip to content

Commit 9442716

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

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
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/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)