diff --git a/index.js b/index.js index ab6ffb9..9eb781b 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,13 @@ async function currentURL(driver, options) { const percySnapshot = async function percySnapshot(driver, name, options) { if (!driver) throw new Error('An instance of the selenium driver object is required.'); if (!name) throw new Error('The `name` argument is required.'); - if (!(await module.exports.isPercyEnabled())) return; + if (!(await module.exports.isPercyEnabled())) { + if (process.env.PERCY_RAISE_ERROR === 'true') { + throw new Error('Percy is not running, disabling snapshots.'); + } else { + return; + } + } if (utils.percy?.type === 'automate') { throw new Error('Invalid function call - percySnapshot(). Please use percyScreenshot() function while using Percy with Automate. For more information on usage of percyScreenshot, refer https://www.browserstack.com/docs/percy/integrate/functional-and-visual'); } @@ -159,6 +165,9 @@ const percySnapshot = async function percySnapshot(driver, name, options) { // Handle errors log.error(`Could not take DOM snapshot "${name}"`); log.error(error); + if (process.env.PERCY_RAISE_ERROR === 'true') { + throw error; + } } }; @@ -187,7 +196,13 @@ module.exports.percyScreenshot = async function percyScreenshot(driver, name, op if (!driver) throw new Error('An instance of the selenium driver object is required.'); if (!name) throw new Error('The `name` argument is required.'); - if (!(await module.exports.isPercyEnabled())) return; + if (!(await module.exports.isPercyEnabled())) { + if (process.env.PERCY_RAISE_ERROR === 'true') { + throw new Error('Percy is not running, disabling snapshots.'); + } else { + return; + } + } if (utils.percy?.type !== 'automate') { throw new Error('Invalid function call - percyScreenshot(). Please use percySnapshot() function for taking screenshot. percyScreenshot() should be used only while using Percy with Automate. For more information on usage of PercySnapshot(), refer doc for your language https://www.browserstack.com/docs/percy/integrate/overview'); } @@ -226,6 +241,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; + } } }; diff --git a/test/index.test.mjs b/test/index.test.mjs index 8db141a..3d933e7 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -37,6 +37,7 @@ describe('percySnapshot', () => { }); beforeEach(async () => { + delete process.env.PERCY_RAISE_ERROR; await helpers.setupTest(); await driver.get(helpers.testSnapshotURL); }); @@ -195,6 +196,37 @@ 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 percySnapshot(driver, 'Snapshot 1'); + } catch (e) { + error = e; + } + + expect(helpers.logger.stdout).toEqual(jasmine.arrayContaining([ + '[percy] Percy is not running, disabling snapshots' + ])); + expect(error).toBeInstanceOf(Error); + }); + + it('handles snapshot failures if PERCY_RAISE_ERROR is true', async () => { + process.env.PERCY_RAISE_ERROR = 'true'; + await helpers.test('error', '/percy/snapshot'); + let error = null; + try { + await percySnapshot(driver, 'Snapshot 1'); + } catch (e) { + error = e; + } + expect(helpers.logger.stderr).toEqual(jasmine.arrayContaining([ + '[percy] Could not take DOM snapshot "Snapshot 1"' + ])); + expect(error).toBeInstanceOf(Error); + }); }); describe('percyScreenshot', () => { @@ -220,6 +252,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'; @@ -324,6 +357,37 @@ describe('percyScreenshot', () => { ])); }); + it('throw error in SDK if PERCY_RAISE_ERROR is true', async () => { + process.env.PERCY_RAISE_ERROR = 'true'; + spyOn(percySnapshot, 'isPercyEnabled').and.callThrough(); + await helpers.test('error', '/percy/healthcheck'); + let error = null; + try { + await percyScreenshot(driver, 'Snapshot 1'); + } catch (e) { + error = e; + } + expect(helpers.logger.stdout).toEqual(jasmine.arrayContaining([ + '[percy] Percy is not running, disabling snapshots' + ])); + expect(error).toBeInstanceOf(Error); + }); + + it('handles snapshot failures 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';