Skip to content

Commit ef775c1

Browse files
committed
fix conflicts and add various fixes
1 parent d58470d commit ef775c1

16 files changed

+88
-1213
lines changed

src/config/jest-playwright.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('getJestConfig', () => {
2020
'jest-playwright': {
2121
browsers: undefined,
2222
collectCoverage: false,
23+
exitOnPageError: false,
2324
},
2425
},
2526
watchPlugins: [

src/csf/__snapshots__/transformCsf.test.ts.snap

Lines changed: 28 additions & 1113 deletions
Large diffs are not rendered by default.

src/csf/transformCsf.test.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { prefixFunction, transformCsf } from './transformCsf';
1+
import { TestPrefixer, TransformOptions, transformCsf } from './transformCsf';
22
import { testPrefixer } from '../playwright/transformPlaywright';
33
import template from '@babel/template';
4-
import * as t from '@babel/types';
54

65
describe('transformCsf', () => {
76
it('inserts a no-op test if there are no stories', () => {
@@ -12,7 +11,7 @@ describe('transformCsf', () => {
1211
`;
1312
const expectedCode = `describe.skip('Button', () => { it('no-op', () => {}) });`;
1413

15-
const result = transformCsf(csfCode, { insertTestIfEmpty: true });
14+
const result = transformCsf(csfCode, { insertTestIfEmpty: true } as TransformOptions);
1615

1716
expect(result).toEqual(expectedCode);
1817
});
@@ -99,35 +98,12 @@ describe('transformCsf', () => {
9998
const beforeEachBlock = template.statement`beforeEach(() => { ${logStatement()} })`;
10099
return beforeEachBlock();
101100
};
102-
const result = transformCsf(code, { beforeEachPrefixer });
101+
const testPrefixer = template(`
102+
console.log({ id: %%id%%, title: %%title%%, name: %%name%%, storyExport: %%storyExport%% });
103+
async () => {}`) as unknown as TestPrefixer;
103104

104-
expect(result).toMatchSnapshot();
105-
});
106-
});
105+
const result = transformCsf(code, { beforeEachPrefixer, testPrefixer } as TransformOptions);
107106

108-
describe('prefixFunction', () => {
109-
it('returns input expression if testPrefixer is not provided', () => {
110-
const key = 'key';
111-
const title = 'title';
112-
const input = t.identifier('input');
113-
const result = prefixFunction(key, title, input);
114-
expect(result).toEqual(input);
115-
});
116-
117-
it('returns null literal if testPrefixer returns undefined', () => {
118-
const key = 'key';
119-
const title = 'title';
120-
const input = t.identifier('input');
121-
const result = prefixFunction(key, title, input, testPrefixer);
122107
expect(result).toMatchSnapshot();
123108
});
124-
125-
it('returns expression from testPrefixer if it returns a valid expression', () => {
126-
const key = 'key';
127-
const title = 'title';
128-
const input = t.identifier('input');
129-
const testPrefixer = () => t.expressionStatement(t.identifier('prefix'));
130-
const result = prefixFunction(key, title, input, testPrefixer);
131-
expect(result).toEqual(t.identifier('input'));
132-
});
133109
});

src/csf/transformCsf.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as t from '@babel/types';
44
import generate from '@babel/generator';
55
import { toId, storyNameFromExport } from '@storybook/csf';
66
import dedent from 'ts-dedent';
7+
78
import { getTagOptions } from '../util/getTagOptions';
89

910
export interface TestContext {
@@ -16,23 +17,18 @@ type TemplateResult = t.Statement | t.Statement[];
1617
type FilePrefixer = () => TemplateResult;
1718
export type TestPrefixer = (context: TestContext) => TemplateResult;
1819

19-
interface TransformOptions {
20+
export interface TransformOptions {
2021
clearBody?: boolean;
2122
beforeEachPrefixer?: FilePrefixer;
22-
testPrefixer?: TestPrefixer;
23+
testPrefixer: TestPrefixer;
2324
insertTestIfEmpty?: boolean;
2425
makeTitle?: (userTitle: string) => string;
2526
includeTags?: string[];
2627
excludeTags?: string[];
2728
skipTags?: string[];
2829
}
2930

30-
export const prefixFunction = (
31-
key: string,
32-
title: string,
33-
input: t.Expression,
34-
testPrefixer?: TestPrefixer
35-
) => {
31+
export const prefixFunction = (key: string, title: string, testPrefixer: TestPrefixer) => {
3632
const name = storyNameFromExport(key);
3733
const context: TestContext = {
3834
storyExport: t.identifier(key),
@@ -41,20 +37,9 @@ export const prefixFunction = (
4137
id: t.stringLiteral(toId(title, name)),
4238
};
4339

44-
let result = input;
45-
if (testPrefixer) {
46-
const prefixResult = makeArray(testPrefixer(context));
47-
const stmt = prefixResult[1] as t.ExpressionStatement;
48-
if (stmt) {
49-
result = stmt.expression;
50-
}
51-
}
52-
53-
if (!result) {
54-
result = t.nullLiteral();
55-
}
56-
57-
return result;
40+
const result = makeArray(testPrefixer(context));
41+
const stmt = result[1] as t.ExpressionStatement;
42+
return stmt.expression;
5843
};
5944

6045
const makePlayTest = ({
@@ -66,15 +51,15 @@ const makePlayTest = ({
6651
}: {
6752
key: string;
6853
title: string;
69-
metaOrStoryPlay?: t.Node;
70-
testPrefix?: TestPrefixer;
54+
metaOrStoryPlay?: boolean;
55+
testPrefix: TestPrefixer;
7156
shouldSkip?: boolean;
7257
}): t.Statement[] => {
7358
return [
7459
t.expressionStatement(
7560
t.callExpression(shouldSkip ? t.identifier('it.skip') : t.identifier('it'), [
7661
t.stringLiteral(!!metaOrStoryPlay ? 'play-test' : 'smoke-test'),
77-
prefixFunction(key, title, metaOrStoryPlay as t.Expression, testPrefix),
62+
prefixFunction(key, title, testPrefix),
7863
])
7964
),
8065
];
@@ -111,7 +96,7 @@ export const transformCsf = (
11196
beforeEachPrefixer,
11297
insertTestIfEmpty,
11398
makeTitle,
114-
}: TransformOptions = {}
99+
}: TransformOptions
115100
) => {
116101
const { includeTags, excludeTags, skipTags } = getTagOptions();
117102

@@ -153,7 +138,7 @@ export const transformCsf = (
153138
...makePlayTest({
154139
key,
155140
title,
156-
metaOrStoryPlay: storyAnnotations[key]?.play,
141+
metaOrStoryPlay: !!storyAnnotations[key]?.play,
157142
testPrefix: testPrefixer,
158143
shouldSkip,
159144
}),

src/playwright/hooks.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import { Page } from 'playwright-core';
22
import {
33
getStoryContext,
4-
setPreRender,
5-
setPostRender,
4+
setPreVisit,
5+
setPostVisit,
66
TestRunnerConfig,
77
waitForPageReady,
88
} from './hooks';
99

1010
type MockPage = Page & { evaluate: jest.Mock };
1111

1212
describe('test-runner', () => {
13-
describe('setPreRender', () => {
14-
it('sets the preRender function', () => {
15-
const preRender = jest.fn();
16-
setPreRender(preRender);
17-
expect(globalThis.__sbPreRender).toBe(preRender);
13+
describe('setPreVisit', () => {
14+
it('sets the preVisit function', () => {
15+
const preVisit = jest.fn();
16+
setPreVisit(preVisit);
17+
expect(globalThis.__sbPreVisit).toBe(preVisit);
1818
});
1919
});
2020

21-
describe('setPostRender', () => {
22-
it('sets the postRender function', () => {
23-
const postRender = jest.fn();
24-
setPostRender(postRender);
25-
expect(globalThis.__sbPostRender).toBe(postRender);
21+
describe('setPostVisit', () => {
22+
it('sets the postVisit function', () => {
23+
const postVisit = jest.fn();
24+
setPostVisit(postVisit);
25+
expect(globalThis.__sbPostVisit).toBe(postVisit);
2626
});
2727
});
2828

src/playwright/transformPlaywright.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const testPrefixer = template(
6969
{
7070
plugins: ['jsx'],
7171
}
72-
) as any as TestPrefixer;
72+
) as unknown as TestPrefixer;
7373

7474
const makeTitleFactory = (filename: string) => {
7575
const { workingDir, normalizedStoriesEntries } = getStorybookMetadata();

src/playwright/transformPlaywrightJson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const makeTest = ({
1414
shouldSkip: boolean;
1515
metaOrStoryPlay: boolean;
1616
}): t.Statement => {
17-
const result: any = testPrefixer({
17+
const result = testPrefixer({
1818
name: t.stringLiteral(entry.name),
1919
title: t.stringLiteral(entry.title),
2020
id: t.stringLiteral(entry.id),

src/setup-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ export const setupPage = async (page: Page, browserContext: BrowserContext) => {
4848
);
4949
}
5050

51-
const testRunnerConfig = getTestRunnerConfig();
51+
const testRunnerConfig = getTestRunnerConfig() || {};
5252
if (testRunnerConfig?.prepare) {
5353
await testRunnerConfig.prepare({ page, browserContext, testRunnerConfig });
5454
} else {
55-
if (testRunnerConfig) await defaultPrepare({ page, browserContext, testRunnerConfig });
55+
await defaultPrepare({ page, browserContext, testRunnerConfig });
5656
}
5757

5858
// if we ever want to log something from the browser to node

src/test-storybook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ async function executeJestPlaywright(args: JestOptions) {
178178
await jest.run(argv);
179179
}
180180

181-
async function checkStorybook(url: any) {
181+
async function checkStorybook(url: string) {
182182
try {
183183
const headers = await getHttpHeaders(url);
184184
const res = await fetch(url, { method: 'GET', headers });
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`getStorybookMain no stories should throw an error if no stories are defined 1`] = `
4-
"Could not find stories in main.js in .storybook.
4+
"Could not find stories in main.js in ".storybook".
55
If you are using a mono-repository, please run the test-runner only against your sub-package, which contains a .storybook folder with "stories" defined in main.js.
66
You can change the config directory by using --config-dir <path-to-dir>"
77
`;
88
99
exports[`getStorybookMain no stories should throw an error if stories list is empty 1`] = `
10-
"Could not find stories in main.js in .storybook.
10+
"Could not find stories in main.js in ".storybook".
1111
If you are using a mono-repository, please run the test-runner only against your sub-package, which contains a .storybook folder with "stories" defined in main.js.
1212
You can change the config directory by using --config-dir <path-to-dir>"
1313
`;
1414
15-
exports[`getStorybookMain should throw an error if no configuration is found 1`] = `"Could not load main.js in .storybook. Is the config directory correct? You can change it by using --config-dir <path-to-dir>"`;
15+
exports[`getStorybookMain should throw an error if no configuration is found 1`] = `"Could not load main.js in .storybook. Is the ".storybook" config directory correct? You can change it by using --config-dir <path-to-dir>"`;

src/util/getCliOptions.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ describe('getCliOptions', () => {
5656

5757
it('handles extra arguments correctly', () => {
5858
jest.spyOn(cliHelper, 'getParsedCliOptions').mockReturnValue({
59-
options: { version: true, cache: false, env: 'node' },
59+
options: { version: true, cache: false, env: 'node' } as any,
6060
extraArgs: ['--watch', '--coverage'],
6161
});
6262
const opts = getCliOptions();
6363
expect(opts.jestOptions).toEqual([
6464
'--version',
6565
'--no-cache',
66-
'--env="node"',
66+
'--env',
67+
'node',
6768
'--watch',
6869
'--coverage',
6970
]);

src/util/getCliOptions.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,19 @@ export const getCliOptions = (): CliOptions => {
5454
jestOptions: process.argv.splice(0, 2),
5555
};
5656

57-
const finalOptions = Object.keys(allOptions).reduce((acc: CliOptions, key: string) => {
58-
if (STORYBOOK_RUNNER_COMMANDS.includes(key as StorybookRunnerCommand)) {
59-
copyOption(
60-
acc.runnerOptions,
61-
key as StorybookRunnerCommand,
62-
allOptions[key as StorybookRunnerCommand]
63-
);
57+
const finalOptions = Object.keys(allOptions).reduce((acc: CliOptions, _key: string) => {
58+
let key = _key as StorybookRunnerCommand;
59+
let optionValue = allOptions[key];
60+
61+
if (STORYBOOK_RUNNER_COMMANDS.includes(key)) {
62+
copyOption(acc.runnerOptions, key, optionValue);
6463
} else {
65-
if (allOptions[key as StorybookRunnerCommand] === true) {
64+
if (optionValue === true) {
6665
acc.jestOptions.push(`--${key}`);
67-
} else if (allOptions[key as StorybookRunnerCommand] === false) {
66+
} else if (optionValue === false) {
6867
acc.jestOptions.push(`--no-${key}`);
6968
} else {
70-
acc.jestOptions.push(`--${key}="${allOptions[key as StorybookRunnerCommand]}"`);
69+
acc.jestOptions.push(`--${key}`, `${optionValue}`);
7170
}
7271
}
7372

src/util/getStorybookMain.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import dedent from 'ts-dedent';
55

66
let storybookMainConfig = new Map<string, StorybookConfig>();
77

8-
export const getStorybookMain = (configDir: string) => {
8+
export const getStorybookMain = (configDir = '.storybook') => {
99
if (storybookMainConfig.has(configDir)) {
10-
return storybookMainConfig.get(configDir);
10+
return storybookMainConfig.get(configDir) as StorybookConfig;
1111
} else {
1212
storybookMainConfig.set(configDir, serverRequire(join(resolve(configDir), 'main')));
1313
}
@@ -16,14 +16,14 @@ export const getStorybookMain = (configDir: string) => {
1616

1717
if (!mainConfig) {
1818
throw new Error(
19-
`Could not load main.js in ${configDir}. Is the config directory correct? You can change it by using --config-dir <path-to-dir>`
19+
`Could not load main.js in ${configDir}. Is the "${configDir}" config directory correct? You can change it by using --config-dir <path-to-dir>`
2020
);
2121
}
2222

2323
if (!mainConfig.stories || mainConfig.stories.length === 0) {
2424
throw new Error(
2525
dedent`
26-
Could not find stories in main.js in ${configDir}.
26+
Could not find stories in main.js in "${configDir}".
2727
If you are using a mono-repository, please run the test-runner only against your sub-package, which contains a .storybook folder with "stories" defined in main.js.
2828
You can change the config directory by using --config-dir <path-to-dir>
2929
`

src/util/getStorybookMetadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { StoriesEntry } from '@storybook/types';
55

66
export const getStorybookMetadata = () => {
77
const workingDir = getProjectRoot();
8-
const configDir = process.env.STORYBOOK_CONFIG_DIR || '';
8+
const configDir = process.env.STORYBOOK_CONFIG_DIR || '.storybook';
99

1010
const main = getStorybookMain(configDir);
11-
const normalizedStoriesEntries = normalizeStories(main?.stories as StoriesEntry[], {
11+
const normalizedStoriesEntries = normalizeStories(main.stories as StoriesEntry[], {
1212
configDir,
1313
workingDir,
1414
}).map((specifier) => ({
@@ -22,7 +22,7 @@ export const getStorybookMetadata = () => {
2222
.join(';');
2323

2424
// @ts-ignore -- this is added in @storybook/[email protected], which we don't depend on
25-
const lazyCompilation = !!main?.core?.builder?.options?.lazyCompilation;
25+
const lazyCompilation = !!main.core?.builder?.options?.lazyCompilation;
2626

2727
return {
2828
configDir,

src/util/getTestRunnerConfig.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { serverRequire } from '@storybook/core-common';
21
import { TestRunnerConfig } from '../playwright/hooks';
3-
import { getTestRunnerConfig, loaded } from './getTestRunnerConfig';
2+
import { getTestRunnerConfig } from './getTestRunnerConfig';
43
import { join, resolve } from 'path';
54

65
const testRunnerConfig: TestRunnerConfig = {
@@ -43,7 +42,6 @@ describe('getTestRunnerConfig', () => {
4342
);
4443

4544
const result = getTestRunnerConfig(configDir);
46-
console.log(result);
4745

4846
expect(result).toEqual(testRunnerConfig);
4947
expect(require('@storybook/core-common').serverRequire).toHaveBeenCalledWith(

src/util/getTestRunnerConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let testRunnerConfig: TestRunnerConfig;
66
let loaded = false;
77

88
export const getTestRunnerConfig = (
9-
configDir = process.env.STORYBOOK_CONFIG_DIR || ''
9+
configDir = process.env.STORYBOOK_CONFIG_DIR || '.storybook'
1010
): TestRunnerConfig | undefined => {
1111
// testRunnerConfig can be undefined
1212
if (loaded) {

0 commit comments

Comments
 (0)