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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw error on unconfigured coverage when running with --coverage #166

Merged
merged 1 commit into from Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/setup-page.ts
@@ -1,5 +1,7 @@
//@ts-nocheck
const sanitizeURL = (url) => {
import type { Page } from 'playwright';
import dedent from 'ts-dedent';

const sanitizeURL = (url: string) => {
let finalURL = url;
// prepend URL protocol if not there
if (finalURL.indexOf('http://') === -1 && finalURL.indexOf('https://') === -1) {
Expand All @@ -20,9 +22,10 @@ const sanitizeURL = (url) => {
return finalURL;
};

export const setupPage = async (page) => {
export const setupPage = async (page: Page) => {
const targetURL = new URL('iframe.html', process.env.TARGET_URL).toString();
const viewMode = process.env.VIEW_MODE || 'story';
const isCoverageMode = process.env.STORYBOOK_COLLECT_COVERAGE === 'true';
const renderedEvent = viewMode === 'docs' ? 'docsRendered' : 'storyRendered';

const referenceURL = process.env.REFERENCE_URL && sanitizeURL(process.env.REFERENCE_URL);
Expand All @@ -43,7 +46,20 @@ export const setupPage = async (page) => {
}

throw err;
}); // FIXME: configure
});

if (isCoverageMode) {
const isCoverageSetupCorrectly = await page.evaluate(() => '__coverage__' in window);
if (!isCoverageSetupCorrectly) {
throw new Error(
dedent`
[Test runner] An error occurred when evaluating code coverage:
The code in Storybook is not instrumented, which means the coverage setup is not correct.
More info: https://github.com/storybookjs/test-runner#setting-up-code-coverage
`
);
}
}

// if we ever want to log something from the browser to node
await page.exposeBinding('logToPage', (_, message) => console.log(message));
Expand Down