Skip to content

Instantly share code, notes, and snippets.

@ajs139
Last active October 24, 2019 02:16
Show Gist options
  • Save ajs139/3ddc10e807ee9b94b581c80a762de587 to your computer and use it in GitHub Desktop.
Save ajs139/3ddc10e807ee9b94b581c80a762de587 to your computer and use it in GitHub Desktop.
require('expect-puppeteer');
const isCI = require('is-ci');
const path = require('path');
const { mkdirp, writeFile } = require('fs-extra');
const screenshotsPath = path.resolve(__dirname, '../reports/screenshots');
const toFilename = s => s.replace(/[^a-z0-9.-]+/gi, '_');
const saveScreenshot = async (screenshot, testName) => {
await mkdirp(screenshotsPath);
const fileName = toFilename(`${new Date().toISOString()}_${testName}_screenshot.png`);
const filePath = path.join(screenshotsPath, fileName);
await writeFile(filePath, screenshot);
return filePath;
};
let currentTest = '';
let currentScreenshot = null;
if (isCI) {
jasmine.getEnv().addReporter({
specStarted: ({ fullName }) => {
currentTest = fullName;
currentScreenshot = null;
},
specDone: async ({ status, fullName }) => {
if (status === 'failed') {
if(currentScreenshot) {
try {
const filePath = await saveScreenshot(currentScreenshot, currentTest);
console.error(`FAILED ${fullName}: screenshot @ ${filePath}`);
} catch (e) {
console.error(`FAILED ${fullName}: could not save screenshot.`, e);
}
} else {
console.error(`FAILED ${fullName}: sadly, no screenshot could be taken.`);
}
}
},
});
afterEach(async () => {
currentScreenshot = await page.screenshot();
});
} else {
jest.setTimeout(120e3);
}
@ajs139
Copy link
Author

ajs139 commented Oct 24, 2019

@chandana-b - haven't had the chance to experiment with Jest Circus yet, hope you managed to find a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment