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

Cleanup play function checks #37

Merged
merged 2 commits into from
Feb 3, 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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ jobs:
yarn build
STORY_STORE_V7=true yarn test-storybook:ci

- name: Run test runner (stories json mode)
run: |
yarn build
yarn test-storybook:ci-json
2 changes: 0 additions & 2 deletions bin/test-storybook.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ async function fetchStoriesJson(url) {
return tmpDir;
}

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const main = async () => {
const targetURL = sanitizeURL(process.env.TARGET_URL || `http://localhost:6006`);
await checkStorybook(targetURL);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"release": "yarn build && auto shipit",
"test-storybook": "node bin/test-storybook.js --no-cache",
"test-storybook:ci": "concurrently -k -s first -n \"SB,TEST\" -c \"magenta,blue\" \"yarn build-storybook --quiet && npx http-server storybook-static --port 6006 --silent\" \"wait-on tcp:6006 && yarn test-storybook\"",
"test-storybook:ci-json": "concurrently -k -s first -n \"SB,TEST\" -c \"magenta,blue\" \"yarn build-storybook --quiet && npx http-server storybook-static --port 6006 --silent\" \"wait-on tcp:6006 && yarn test-storybook --stories-json\"",
"test-storybook:jsdom": "jest --no-cache --config ./jsdom-jest.config.js -i",
"test-storybook:playwright-no-cache": "jest --no-cache --config ./test-runner-jest.config.js",
"test-storybook:playwright": "./bin/test-storybook.js",
Expand Down
15 changes: 7 additions & 8 deletions playwright/custom-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class CustomEnvironment extends PlaywrightEnvironment {
await page.addScriptTag({
content: `
class StorybookTestRunnerError extends Error {
constructor(storyId, hasPlayFn, errorMessage) {
constructor(storyId, errorMessage) {
super(errorMessage);
this.name = 'StorybookTestRunnerError';
const storyUrl = \`${referenceURL || targetURL}?path=/story/\${storyId}\`;
const finalStoryUrl = storyUrl + (hasPlayFn ? '&addonPanel=storybook/interactions/panel' : '');
const finalStoryUrl = \`\${storyUrl}&addonPanel=storybook/interactions/panel\`;

this.message = \`\nAn error occurred in the following story:\n\${finalStoryUrl}\n\nMessage:\n \${errorMessage}\`;
}
Expand Down Expand Up @@ -90,19 +90,18 @@ class CustomEnvironment extends PlaywrightEnvironment {
});
}

async function __test(storyId, hasPlayFn) {
async function __test(storyId) {
try {
await __waitForElement('#root');
} catch(err) {
const message = \`Timed out waiting for Storybook to load after 10 seconds. Are you sure the Storybook is running correctly in that URL? Is the Storybook private (e.g. under authentication layers)?\n\n\nHTML: \${document.body.innerHTML}\`;
throw new StorybookTestRunnerError(storyId, hasPlayFn, message);
throw new StorybookTestRunnerError(storyId, message);
}

const channel = window.__STORYBOOK_ADDONS_CHANNEL__;
if(!channel) {
throw new StorybookTestRunnerError(
storyId,
hasPlayFn,
'The test runner could not access the Storybook channel. Are you sure the Storybook is running correctly in that URL?'
);
}
Expand All @@ -111,13 +110,13 @@ class CustomEnvironment extends PlaywrightEnvironment {
channel.on('storyRendered', () => resolve(document.getElementById('root')));
channel.on('storyUnchanged', () => resolve(document.getElementById('root')));
channel.on('storyErrored', ({ description }) => reject(
new StorybookTestRunnerError(storyId, hasPlayFn, description))
new StorybookTestRunnerError(storyId, description))
);
channel.on('storyThrewException', (error) => reject(
new StorybookTestRunnerError(storyId, hasPlayFn, error.message))
new StorybookTestRunnerError(storyId, error.message))
);
channel.on('storyMissing', (id) => id === storyId && reject(
new StorybookTestRunnerError(storyId, hasPlayFn, 'The story was missing when trying to access it.'))
new StorybookTestRunnerError(storyId, 'The story was missing when trying to access it.'))
);

channel.emit('setCurrentStory', { storyId });
Expand Down
4 changes: 1 addition & 3 deletions src/csf/transformCsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface TestContext {
name: t.Literal;
title: t.Literal;
id: t.Literal;
hasPlayFn?: t.BooleanLiteral;
}
type FilePrefixer = () => t.Statement[];
type TestPrefixer = (context: TestContext) => t.Statement[];
Expand All @@ -35,9 +34,8 @@ const prefixFunction = (
const context: TestContext = {
storyExport: t.identifier(key),
name: t.stringLiteral(name), // FIXME .name annotation
title: t.stringLiteral(title), // FIXME: auto-title
title: t.stringLiteral(title),
id: t.stringLiteral(toId(title, name)),
hasPlayFn: t.booleanLiteral(!!input),
};

// instead, let's just make the prefixer return the function
Expand Down
2 changes: 1 addition & 1 deletion src/jsdom/transformJsdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const filePrefixer = template(`

const testPrefixer = template(
`
console.log({ id: %%id%%, title: %%title%%, name: %%name%%, storyExport: %%storyExport%%, hasPlayFn: %%hasPlayFn%%});
console.log({ id: %%id%%, title: %%title%%, name: %%name%%, storyExport: %%storyExport%% });
async () => {
const Composed = await composeStory(%%storyExport%%, exports.default);
const { container } = render(<Composed />);
Expand Down
24 changes: 9 additions & 15 deletions src/playwright/transformPlaywright.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ describe('Playwright', () => {
});
});
return page.evaluate(({
id,
hasPlayFn
}) => __test(id, hasPlayFn), {
id: "foo-bar--a",
hasPlayFn: true
id
}) => __test(id), {
id: "foo-bar--a"
});
});
});
Expand Down Expand Up @@ -96,11 +94,9 @@ describe('Playwright', () => {
});
});
return page.evaluate(({
id,
hasPlayFn
}) => __test(id, hasPlayFn), {
id: "foo-bar--a",
hasPlayFn: false
id
}) => __test(id), {
id: "foo-bar--a"
});
});
});
Expand Down Expand Up @@ -133,11 +129,9 @@ describe('Playwright', () => {
});
});
return page.evaluate(({
id,
hasPlayFn
}) => __test(id, hasPlayFn), {
id: "example-header--a",
hasPlayFn: false
id
}) => __test(id), {
id: "example-header--a"
});
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/playwright/transformPlaywright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ export const testPrefixer = template(
page.evaluate(({ id, err }) => __throwError(id, err), { id: %%id%%, err: err.message });
});

return page.evaluate(({ id, hasPlayFn }) => __test(id, hasPlayFn), {
id: %%id%%,
hasPlayFn: %%hasPlayFn%%,
return page.evaluate(({ id }) => __test(id), {
id: %%id%%
});
}
`,
Expand Down
32 changes: 12 additions & 20 deletions src/playwright/transformPlaywrightJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ Object {
});
});
return page.evaluate(({
id,
hasPlayFn
}) => __test(id, hasPlayFn), {
id: \\"example-header--logged-in\\",
hasPlayFn: false
id
}) => __test(id), {
id: \\"example-header--logged-in\\"
});
});
});
Expand All @@ -82,11 +80,9 @@ Object {
});
});
return page.evaluate(({
id,
hasPlayFn
}) => __test(id, hasPlayFn), {
id: \\"example-header--logged-out\\",
hasPlayFn: false
id
}) => __test(id), {
id: \\"example-header--logged-out\\"
});
});
});
Expand All @@ -104,11 +100,9 @@ Object {
});
});
return page.evaluate(({
id,
hasPlayFn
}) => __test(id, hasPlayFn), {
id: \\"example-page--logged-in\\",
hasPlayFn: false
id
}) => __test(id), {
id: \\"example-page--logged-in\\"
});
});
});
Expand Down Expand Up @@ -164,11 +158,9 @@ Object {
});
});
return page.evaluate(({
id,
hasPlayFn
}) => __test(id, hasPlayFn), {
id: \\"example-page--logged-in\\",
hasPlayFn: false
id
}) => __test(id), {
id: \\"example-page--logged-in\\"
});
});
});
Expand Down
1 change: 0 additions & 1 deletion src/playwright/transformPlaywrightJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const makeTest = (story: Story): t.Statement => {
id: t.stringLiteral(story.id),
// FIXME
storyExport: t.identifier(story.id),
hasPlayFn: t.booleanLiteral(false),
});
const stmt = result[1] as t.ExpressionStatement;
return t.expressionStatement(
Expand Down