Skip to content

Commit

Permalink
Feat: Throw Error if PERCY_RAISE_ERROR is True
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit3200 committed Dec 3, 2024
1 parent c0b6b9d commit 875481b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ module.exports.percyScreenshot = async function percyScreenshot(driver, name, op
// Handle errors
log.error(`Could not take Screenshot "${name}"`);
log.error(error.stack);
if (process.env.PERCY_RAISE_ERROR === 'true') {
throw error;
}
}
};

Expand Down
34 changes: 34 additions & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('percySnapshot', () => {
});

beforeEach(async () => {
delete process.env.PERCY_RAISE_ERROR;
await helpers.setupTest();
await driver.get(helpers.testSnapshotURL);
});
Expand Down Expand Up @@ -195,6 +196,22 @@ describe('percySnapshot', () => {
expect(setTimeout).toHaveBeenCalled();
delete process.env.RESPONSIVE_CAPTURE_SLEEP_TIME;
});

it('throw error in SDK if PERCY_RAISE_ERROR is true', async () => {
process.env.PERCY_RAISE_ERROR = 'true';
await helpers.test('error', '/percy/healthcheck');
let error = null;
try {
await percyScreenshot(driver, 'Snapshot 1');
} catch (e) {
error = e;
}

expect(helpers.logger.stderr).toEqual(jasmine.arrayContaining([
'[percy] Could not take Screenshot "Snapshot 1"'
]));
expect(error).toBeInstanceOf(Error);
});
});

describe('percyScreenshot', () => {
Expand All @@ -220,6 +237,7 @@ describe('percyScreenshot', () => {
});

beforeEach(async () => {
delete process.env.PERCY_RAISE_ERROR;
await helpers.setupTest();
spyOn(percySnapshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true));
utils.percy.type = 'automate';
Expand Down Expand Up @@ -324,6 +342,22 @@ describe('percyScreenshot', () => {
]));
});

it('throw error in SDK if PERCY_RAISE_ERROR is true', async () => {
process.env.PERCY_RAISE_ERROR = 'true';
await helpers.test('error', '/percy/automateScreenshot');
let error = null;
try {
await percyScreenshot(driver, 'Snapshot 1');
} catch (e) {
error = e;
}

expect(helpers.logger.stderr).toEqual(jasmine.arrayContaining([
'[percy] Could not take Screenshot "Snapshot 1"'
]));
expect(error).toBeInstanceOf(Error);
});

it('throws error for web session', async () => {
spyOn(percySnapshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true));
utils.percy.type = 'web';
Expand Down

0 comments on commit 875481b

Please sign in to comment.