Skip to content

Commit

Permalink
updating error message and fixing retry snapshot bug (#1072)
Browse files Browse the repository at this point in the history
* updating error message and fixing retry snapshot bug

* addressing comment

* addressing comments

* addressing comments

test fix

test fix

test fix

test fix

test fix

test fix

* addressig comments

* adding from key

* test fix

* test fix

* test fix

* comment fix

* typo fix
  • Loading branch information
prklm10 authored Nov 27, 2024
1 parent c3cc70f commit 051819a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ export async function* takeStorybookSnapshots(percy, callback, { baseUrl, flags

// gather storybook data in parallel
let [environmentInfo, stories] = yield* yieldAll([
withPage(percy, aboutUrl, p => p.eval(evalStorybookEnvironmentInfo)),
withPage(percy, previewUrl, p => p.eval(evalStorybookStorySnapshots))
withPage(percy, aboutUrl, p => p.eval(evalStorybookEnvironmentInfo), undefined, { from: 'about url' }),
withPage(percy, previewUrl, p => p.eval(evalStorybookStorySnapshots), undefined, { from: 'preview url' })
]);

// map stories to snapshot options
Expand Down
22 changes: 20 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export async function* withPage(percy, url, callback, retry, args) {
'}'
].join('\n')
), ...args);

try {
yield page.goto(url);
return yield* yieldTo(callback(page));
Expand All @@ -194,10 +193,29 @@ export async function* withPage(percy, url, callback, retry, args) {
} catch (error) {
attempt++;
let enableRetry = process.env.PERCY_RETRY_STORY_ON_ERROR || 'true';
const from = args?.from;
if (!(enableRetry === 'true') || attempt === retries) {
// Add snapshotName to the error message
const snapshotName = args?.snapshotName;
if (from) {
error.message = `${from}: \n${error.message}`;
}
if (snapshotName) {
error.message = `Snapshot Name: ${snapshotName}: \n${error.message}`;
}
throw error;
}
log.warn(`Retrying Story: ${args.snapshotName}`);

// throw warning message with snapshot name if it is present.
if (args?.snapshotName) {
log.warn(`Retrying Story: ${args.snapshotName}, attempt: ${attempt}`);
}
// throw warning message with from where it is called if from in present.
if (from) {
log.warn(
`Retrying because error occurred in: ${from}, attempt: ${attempt}`
);
}
}
}
}
Expand Down
22 changes: 15 additions & 7 deletions test/storybook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('percy storybook', () => {

expect(logger.stderr).toEqual([
'[percy] Build not created',
'[percy] Error: Storybook object not found on the window. ' +
'[percy] Error: preview url: \nStorybook object not found on the window. ' +
'Open Storybook and check the console for errors.'
]);
});
Expand Down Expand Up @@ -173,12 +173,15 @@ describe('percy storybook', () => {

await expectAsync(storybook(['http://localhost:8000']))
// message contains the client stack trace
.toBeRejectedWithError(/^Story Error\n.*\/iframe\.html.*$/s);
.toBeRejectedWithError(/Story Error\n.*\/iframe\.html.*$/s);

expect(logger.stderr).toEqual([
'[percy] Build not created',
// message contains the client stack trace
jasmine.stringMatching(/^\[percy\] Error: Story Error\n.*\/iframe\.html.*$/s)
jasmine.stringMatching(
/^\[percy\] Error: Snapshot Name:/s
)

]);
});

Expand Down Expand Up @@ -223,7 +226,9 @@ describe('percy storybook', () => {
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),
jasmine.stringMatching(
/^\[percy\] Error: Snapshot Name:/s
),
// does not create a build if all stories failed [ 1 in this case ]
'[percy] Build not created'
]);
Expand Down Expand Up @@ -269,11 +274,14 @@ describe('percy storybook', () => {

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

// does not create a build if all stories failed [ 1 in this case ]
'[percy] Build not created'
]);
Expand Down

0 comments on commit 051819a

Please sign in to comment.