Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadbstack committed Oct 13, 2023
1 parent cbd4847 commit ed6f949
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,15 @@ export async function* takeStorybookSnapshots(percy, callback, { baseUrl, flags
return lastCount > snapshots.length;
});
} catch (e) {
// if we get an exception while capturing a story
// - we want to print error
// - skip story
// - continue capturing stories on a new page to avoid weird page states
let { name } = snapshots[0];

log.error(`Failed to capture story: ${name}`);
log.error(e);

// ignore story
snapshots.shift();
if (process.env.PERCY_SKIP_STORY_ON_ERROR === 'true') {
let { name } = snapshots[0];
log.error(`Failed to capture story: ${name}`);
log.error(e);
// ignore story
snapshots.shift();
} else {
throw e;
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/storybook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,37 @@ describe('percy storybook', () => {
]);
});

describe('with PERCY_SKIP_STORY_ON_ERROR set to true', () => {
beforeAll(() => {
process.env.PERCY_SKIP_STORY_ON_ERROR = true;
});

afterAll(() => {
delete process.env.PERCY_SKIP_STORY_ON_ERROR;
});

it('skips the story and logs the error but does not break build', async () => {
server.reply('/iframe.html', () => [200, 'text/html', [
`<script>__STORYBOOK_PREVIEW__ = { async extract() {}, ${
'channel: { emit() {}, on: (a, c) => a === "storyErrored" && c(new Error("Story Error")) }'
} }</script>`,
`<script>__STORYBOOK_STORY_STORE__ = { raw: () => ${JSON.stringify([
{ id: '1', kind: 'foo', name: 'bar' }
])} }</script>`
].join('')]);

// does not reject
await storybook(['http://localhost:8000']);

// contains logs of story error
expect(logger.stderr).toEqual([
'[percy] Failed to capture story: foo: bar',
// error logs contain the client stack trace
jasmine.stringMatching(/^\[percy\] Error: Story Error\n.*\/iframe\.html.*$/s)
]);
});
});

it('uses the preview dom when javascript is enabled', async () => {
let previewDOM = '<p>This is the preview</p>';
let i = 0;
Expand Down

0 comments on commit ed6f949

Please sign in to comment.