-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
109 lines (93 loc) · 3.36 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
const assert = require('assert');
const http = require('http');
const { remote } = require('webdriverio');
const { hashElement } = require('folder-hash');
const { existsSync, readFileSync, writeFileSync, readdirSync, mkdirSync } = require('fs');
const path = require('path');
const IS_CI = process.env.NODE_ENV === 'CI';
const URL_PREFIX = IS_CI ? 'https://debug.tips/canicatch/fixtures_generated/' : 'http://127.0.0.1:3000/';
const REPORT_SCRIPTS = `function() { var span = document.createElement('span'); span.id = '__CAPTURED'; document.body.appendChild(span); }`;
const INJECT_CAPTURE_SCRIPTS = [{
type: 'onerror',
script: `window.onerror = ${REPORT_SCRIPTS};`,
}, {
type: 'error',
script: `document.addEventListener('error', ${REPORT_SCRIPTS}, true);`,
}, {
type: 'promise',
script: `window.addEventListener('unhandledrejection', ${REPORT_SCRIPTS});`,
}];
// Start a local server if running on local machine
if (!IS_CI) {
let app = null;
before(() => {
// Start http server
app = http.createServer((req, res) => {
if (!existsSync(path.join(__dirname, 'fixtures_generated', req.url))) {
res.writeHead(404);
res.end();
return;
}
res.writeHead(200);
res.end(readFileSync(path.join(__dirname, 'fixtures_generated', req.url)));
});
app.listen(3000);
});
after(() => {
if (app) {
app.close();
}
});
}
async function getRegenerateStatus() {
let fixturesHash = null;
let needRegenerate = false;
// Check for fixture folder hash
const fixturesHashPath = path.join(__dirname, '.fixtures_hash');
const currentFixturesHashObject = await hashElement(path.join(__dirname, 'fixtures'), { files: { include: ['*.html']}});
const currentFixturesHash = currentFixturesHashObject.hash;
if (existsSync(fixturesHashPath)) {
fixturesHash = readFileSync(fixturesHashPath, { encoding: 'utf8'});
}
if (fixturesHash !== currentFixturesHash) {
needRegenerate = true;
writeFileSync(fixturesHashPath, currentFixturesHash, { encoding: 'utf8'});
}
return needRegenerate;
}
(async function() {
const needRegenerate = getRegenerateStatus();
// Generate real fixtures
const files = readdirSync(path.join(__dirname, 'fixtures'), { encoding: 'utf8' });
if (needRegenerate) {
if (!existsSync(path.join(__dirname, 'fixtures_generated'))) {
mkdirSync(path.join(__dirname, 'fixtures_generated'));
}
for (const file of files) {
const html = readFileSync(path.join(__dirname, 'fixtures', file), { encoding: 'utf8'});
for (const script of INJECT_CAPTURE_SCRIPTS) {
writeFileSync(
path.join(__dirname, 'fixtures_generated', `${file.split('.')[0]}_${script.type}.html`),
html.replace('<head>', `<head><script>${script.script}</script>`),
{ encoding: 'utf8' }
);
}
}
}
// Generate dynamic test suites
for (const file of files) {
describe(file, () => {
for (const script of INJECT_CAPTURE_SCRIPTS) {
it(`can be captured by ${script.type}`, async function() {
this.timeout(20000);
await browser.url(`${URL_PREFIX}${file.split('.')[0]}_${script.type}.html`);
const captured = await browser.executeAsync(function(done) {
done(document.getElementById('__CAPTURED') != null);
});
assert(captured === true);
});
}
});
}
}());