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

Add ability to test docs pages via VIEW_MODE env var #79

Merged
merged 2 commits into from
Mar 13, 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
5 changes: 3 additions & 2 deletions .storybook/test-runner.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { toMatchImageSnapshot } from 'jest-image-snapshot';
import type { TestRunnerConfig } from '../dist/ts';

const customSnapshotsDir = `${process.cwd()}/__snapshots__`;
const snapshotsDir = process.env.SNAPSHOTS_DIR || '__snapshots__';
const customSnapshotsDir = `${process.cwd()}/${snapshotsDir}`;

const config: TestRunnerConfig = {
setup() {
expect.extend({ toMatchImageSnapshot });
},
async postRender(page, context) {
const image = await page.screenshot();
const image = await page.screenshot({ fullPage: true });
expect(image).toMatchImageSnapshot({
customSnapshotsDir,
customSnapshotIdentifier: context.id,
Expand Down
6 changes: 4 additions & 2 deletions src/setup-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const sanitizeURL = (url) => {
export const setupPage = async (page) => {
const start = new Date();
const targetURL = sanitizeURL(process.env.TARGET_URL || `http://localhost:6006`);
const viewMode = process.env.VIEW_MODE || 'story';
const renderedEvent = viewMode === 'docs' ? 'docsRendered' : 'storyRendered';

const referenceURL = process.env.REFERENCE_URL && sanitizeURL(process.env.REFERENCE_URL);

Expand Down Expand Up @@ -103,7 +105,7 @@ export const setupPage = async (page) => {
}

return new Promise((resolve, reject) => {
channel.on('storyRendered', () => resolve(document.getElementById('root')));
channel.on('${renderedEvent}', () => resolve(document.getElementById('root')));
channel.on('storyUnchanged', () => resolve(document.getElementById('root')));
channel.on('storyErrored', ({ description }) => reject(
new StorybookTestRunnerError(storyId, description))
Expand All @@ -115,7 +117,7 @@ export const setupPage = async (page) => {
new StorybookTestRunnerError(storyId, 'The story was missing when trying to access it.'))
);

channel.emit('setCurrentStory', { storyId });
channel.emit('setCurrentStory', { storyId, viewMode: '${viewMode}' });
});
};
`,
Expand Down