Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make setup-page globally available for index.json mode compatibility #217

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"can-bind-to-host": "^1.1.1",
"commander": "^9.0.0",
"expect-playwright": "^0.8.0",
"global": "^4.4.0",
"jest": "^28.0.0",
"jest-environment-node":"^28.0.0",
"jest-circus":"^28.0.0",
Expand Down
7 changes: 5 additions & 2 deletions playwright/jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getTestRunnerConfig, setPreRender, setPostRender } = require('../dist/cjs');
const { getTestRunnerConfig, setPreRender, setPostRender, setupPage } = require('../dist/cjs');

const testRunnerConfig = getTestRunnerConfig(process.env.STORYBOOK_CONFIG_DIR);
if (testRunnerConfig) {
Expand All @@ -13,4 +13,7 @@ if (testRunnerConfig) {
}
}

global.__sbCollectCoverage = process.env.STORYBOOK_COLLECT_COVERAGE === 'true';
// If the transformed tests need a dependency, it has to be globally available
// in order to work both in defaul (file transformation) and stories/index.json mode.
globalThis.__sbSetupPage = setupPage;
globalThis.__sbCollectCoverage = process.env.STORYBOOK_COLLECT_COVERAGE === 'true';
9 changes: 0 additions & 9 deletions src/csf/transformCsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import generate from '@babel/generator';
import { toId, storyNameFromExport } from '@storybook/csf';
import dedent from 'ts-dedent';

const logger = console;

export interface TestContext {
storyExport?: t.Identifier;
name: t.Literal;
Expand All @@ -19,7 +17,6 @@ export type TestPrefixer = (context: TestContext) => TemplateResult;

interface TransformOptions {
clearBody?: boolean;
filePrefixer?: FilePrefixer;
beforeEachPrefixer?: FilePrefixer;
testPrefixer?: TestPrefixer;
insertTestIfEmpty?: boolean;
Expand Down Expand Up @@ -87,7 +84,6 @@ const makeArray = (templateResult: TemplateResult) =>
export const transformCsf = (
code: string,
{
filePrefixer,
clearBody = false,
testPrefixer,
beforeEachPrefixer,
Expand Down Expand Up @@ -124,11 +120,6 @@ export const transformCsf = (

let result = '';

// FIXME: insert between imports
if (filePrefixer) {
const { code: prefixCode } = generate(t.program(makeArray(filePrefixer())), {});
result = `${prefixCode}\n`;
}
if (!clearBody) result = `${result}${code}\n`;
if (allTests.length) {
const describe = makeDescribe(
Expand Down
5 changes: 2 additions & 3 deletions src/playwright/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import global from 'global';
import type { Page } from 'playwright';
import type { StoryContext } from '@storybook/csf';

Expand All @@ -17,11 +16,11 @@ export interface TestRunnerConfig {
}

export const setPreRender = (preRender: TestHook) => {
global.__sbPreRender = preRender;
globalThis.__sbPreRender = preRender;
};

export const setPostRender = (postRender: TestHook) => {
global.__sbPostRender = postRender;
globalThis.__sbPostRender = postRender;
};

export const getStoryContext = async (page: Page, context: TestContext): Promise<StoryContext> => {
Expand Down
57 changes: 21 additions & 36 deletions src/playwright/transformPlaywright.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ describe('Playwright', () => {
filename
)
).toMatchInlineSnapshot(`
import global from 'global';

const {
setupPage
} = require('@storybook/test-runner');

if (!require.main) {
describe("Example/foo/bar", () => {
describe("A", () => {
Expand All @@ -73,8 +67,8 @@ describe('Playwright', () => {
});
});

if (global.__sbPreRender) {
await global.__sbPreRender(page, context);
if (globalThis.__sbPreRender) {
await globalThis.__sbPreRender(page, context);
}

const result = await page.evaluate(({
Expand All @@ -84,11 +78,11 @@ describe('Playwright', () => {
id: "example-foo-bar--a"
});

if (global.__sbPostRender) {
await global.__sbPostRender(page, context);
if (globalThis.__sbPostRender) {
await globalThis.__sbPostRender(page, context);
}

if (global.__sbCollectCoverage) {
if (globalThis.__sbCollectCoverage) {
const isCoverageSetupCorrectly = await page.evaluate(() => '__coverage__' in window);

if (!isCoverageSetupCorrectly) {
Expand All @@ -107,8 +101,9 @@ describe('Playwright', () => {
await testFn();
} catch (err) {
if (err.toString().includes('Execution context was destroyed')) {
console.log(\`An error occurred in the following story, most likely because of a navigation: "\${"Example/foo/bar"}/\${"A"}". Retrying...\`);
yannbf marked this conversation as resolved.
Show resolved Hide resolved
await jestPlaywright.resetPage();
await setupPage(global.page);
await globalThis.__sbSetupPage(globalThis.page);
await testFn();
} else {
throw err;
Expand All @@ -130,12 +125,6 @@ describe('Playwright', () => {
filename
)
).toMatchInlineSnapshot(`
import global from 'global';

const {
setupPage
} = require('@storybook/test-runner');

if (!require.main) {
describe("Example/foo/bar", () => {
describe("A", () => {
Expand All @@ -156,8 +145,8 @@ describe('Playwright', () => {
});
});

if (global.__sbPreRender) {
await global.__sbPreRender(page, context);
if (globalThis.__sbPreRender) {
await globalThis.__sbPreRender(page, context);
}

const result = await page.evaluate(({
Expand All @@ -167,11 +156,11 @@ describe('Playwright', () => {
id: "example-foo-bar--a"
});

if (global.__sbPostRender) {
await global.__sbPostRender(page, context);
if (globalThis.__sbPostRender) {
await globalThis.__sbPostRender(page, context);
}

if (global.__sbCollectCoverage) {
if (globalThis.__sbCollectCoverage) {
const isCoverageSetupCorrectly = await page.evaluate(() => '__coverage__' in window);

if (!isCoverageSetupCorrectly) {
Expand All @@ -190,8 +179,9 @@ describe('Playwright', () => {
await testFn();
} catch (err) {
if (err.toString().includes('Execution context was destroyed')) {
console.log(\`An error occurred in the following story, most likely because of a navigation: "\${"Example/foo/bar"}/\${"A"}". Retrying...\`);
await jestPlaywright.resetPage();
await setupPage(global.page);
await globalThis.__sbSetupPage(globalThis.page);
await testFn();
} else {
throw err;
Expand All @@ -214,12 +204,6 @@ describe('Playwright', () => {
filename
)
).toMatchInlineSnapshot(`
import global from 'global';

const {
setupPage
} = require('@storybook/test-runner');

if (!require.main) {
describe("Example/Header", () => {
describe("A", () => {
Expand All @@ -240,8 +224,8 @@ describe('Playwright', () => {
});
});

if (global.__sbPreRender) {
await global.__sbPreRender(page, context);
if (globalThis.__sbPreRender) {
await globalThis.__sbPreRender(page, context);
}

const result = await page.evaluate(({
Expand All @@ -251,11 +235,11 @@ describe('Playwright', () => {
id: "example-header--a"
});

if (global.__sbPostRender) {
await global.__sbPostRender(page, context);
if (globalThis.__sbPostRender) {
await globalThis.__sbPostRender(page, context);
}

if (global.__sbCollectCoverage) {
if (globalThis.__sbCollectCoverage) {
const isCoverageSetupCorrectly = await page.evaluate(() => '__coverage__' in window);

if (!isCoverageSetupCorrectly) {
Expand All @@ -274,8 +258,9 @@ describe('Playwright', () => {
await testFn();
} catch (err) {
if (err.toString().includes('Execution context was destroyed')) {
console.log(\`An error occurred in the following story, most likely because of a navigation: "\${"Example/Header"}/\${"A"}". Retrying...\`);
await jestPlaywright.resetPage();
await setupPage(global.page);
await globalThis.__sbSetupPage(globalThis.page);
await testFn();
} else {
throw err;
Expand Down
19 changes: 7 additions & 12 deletions src/playwright/transformPlaywright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import { transformCsf } from '../csf/transformCsf';
import type { TestPrefixer } from '../csf/transformCsf';
import dedent from 'ts-dedent';

const filePrefixer = template(`
import global from 'global';
const { setupPage } = require('@storybook/test-runner');
`);

const coverageErrorMessage = dedent`
[Test runner] An error occurred when evaluating code coverage:
The code in this story is not instrumented, which means the coverage setup is likely not correct.
Expand All @@ -29,19 +24,19 @@ export const testPrefixer = template(
page.evaluate(({ id, err }) => __throwError(id, err), { id: %%id%%, err: err.message });
});

if(global.__sbPreRender) {
await global.__sbPreRender(page, context);
if(globalThis.__sbPreRender) {
await globalThis.__sbPreRender(page, context);
}

const result = await page.evaluate(({ id, hasPlayFn }) => __test(id, hasPlayFn), {
id: %%id%%,
});

if(global.__sbPostRender) {
await global.__sbPostRender(page, context);
if(globalThis.__sbPostRender) {
await globalThis.__sbPostRender(page, context);
}

if(global.__sbCollectCoverage) {
if(globalThis.__sbCollectCoverage) {
const isCoverageSetupCorrectly = await page.evaluate(() => '__coverage__' in window);
if (!isCoverageSetupCorrectly) {
throw new Error(\`${coverageErrorMessage}\`);
Expand All @@ -57,8 +52,9 @@ export const testPrefixer = template(
await testFn();
} catch(err) {
if(err.toString().includes('Execution context was destroyed')) {
console.log(\`An error occurred in the following story, most likely because of a navigation: "\${%%title%%}/\${%%name%%}". Retrying...\`);
await jestPlaywright.resetPage();
await setupPage(global.page);
await globalThis.__sbSetupPage(globalThis.page);
await testFn();
} else {
throw err;
Expand All @@ -80,7 +76,6 @@ const makeTitleFactory = (filename: string) => {

export const transformPlaywright = (src: string, filename: string) => {
const result = transformCsf(src, {
filePrefixer,
testPrefixer,
insertTestIfEmpty: true,
clearBody: true,
Expand Down
Loading