-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest-puppeteer-custom-env.js
57 lines (47 loc) · 2.32 KB
/
jest-puppeteer-custom-env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// custom-environment.js
const JestPuppeteerEnvironment =
require("jest-environment-puppeteer").TestEnvironment;
const fs = require('fs');
/*
* Configurations
*/
const testDir = './tests'
// Used for "jest-image-shapshot"
const imageSnapshotsDir = testDir+'/__image_snapshots__';
const imageSnapshotsDiffOutputDir = imageSnapshotsDir+'/__diff_output__';
// Use for Testspace client publishing
const screenshotsDir = 'screenshots';
const screenshotsListFile = './screenshots-list.txt';
class CustomEnvironment extends JestPuppeteerEnvironment {
async handleTestEvent(event, state) { // eslint-disable-line no-unused-vars
switch (event.name) {
case "test_fn_failure":
/*
Check for browser launch with no goto URL yet. Empty page screenshoot not useful, no capture.
*/
if (this.global.page.url().includes("about:blank")) return; // NO IMAGE TO CAPTURE
var dirName = screenshotsDir + "/" + state.currentlyRunningTest.parent.name.replace(/[^\w]/g, '');
var fileName = dirName + "/" + state.currentlyRunningTest.name.replace(/[^\w]/g, '_') + ".png";
fs.mkdirSync(dirName, {recursive: true});
/*
Check for auto-generated Diff Image. If exist will be moved for publishing.
*/
if (fs.existsSync(imageSnapshotsDiffOutputDir)) {
var files = fs.readdirSync(imageSnapshotsDiffOutputDir);
if (files.length > 0) {
// Moving diff image to screenshots folder
fs.renameSync(imageSnapshotsDiffOutputDir+"/"+files[0],fileName);
fs.appendFileSync( screenshotsListFile, '"['+state.currentlyRunningTest.parent.name+']+'+fileName+'{screenshot diff}"' + "\n");
return; // IMAGE MOVED
}
}
/*
If page active, but no auto-generated "Diff" image, capture current screen
*/
await this.global.page.screenshot({ path: fileName });
fs.appendFileSync(screenshotsListFile, '"['+state.currentlyRunningTest.parent.name+']+'+fileName+'{screenshot}"' + "\n");
break;
}
}
}
module.exports = CustomEnvironment;