-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.js
167 lines (139 loc) · 4.48 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* This script performs a simple check of the example containers for every page they were defined in.
*
* It checks if the number of rendered Handsontable instances is the same as the number of Handsontable
* initialization lines in the examples' code section.
*
* **Note:**
* The documentation version that's being checked by the script needs to be pre-built using the build script, for
* example:
* `npm run docs:build`
*/
/* eslint-enable jsdoc/require-description-complete-sentence */
const { logger } = require('../utils');
const {
logCheck,
sleep,
firstUppercase,
serveFiles,
findExampleContainersInFiles,
setupBrowser,
fetchPermalinks,
fetchPathsWithConditions,
extendPermalink
} = require('./helpers');
const {
getFrameworks
} = require('../../helpers');
const { testCases } = require('./testCases');
/**
* Port to serve the static docs pages under.
*
* @type {number}
*/
const PORT = 8088;
/**
* Timeout for the http server to serve all the documentation files.
*
* @type {number}
*/
const FILE_SERVE_TIMEOUT = 100;
/**
* Timout for the examples to get initialized after loading the page.
*
* @type {number}
*/
const EXAMPLE_INIT_TIMEOUT = 100;
/**
* Number of tries to perform if the number of the rendered examples differs from the expected count.
*
* @type {number}
*/
const CHECK_TRIES = 20;
(async() => {
const FRAMEWORKS_TO_CHECK = getFrameworks();
serveFiles(PORT);
// Wait for http-server to serve the files.
await sleep(FILE_SERVE_TIMEOUT);
const brokenExamplePaths = [];
const searchResults = await findExampleContainersInFiles();
const pathsWithConditions = fetchPathsWithConditions();
const permalinks = fetchPermalinks(searchResults, pathsWithConditions);
const {
browser,
page
} = await setupBrowser();
logger.log('Checking if the examples rendered correctly:');
/* eslint-disable no-await-in-loop, no-continue */
for (let f = 0; f < FRAMEWORKS_TO_CHECK.length; f++) {
const framework = FRAMEWORKS_TO_CHECK[f];
logger.log(`\n${firstUppercase(framework)} flavor:`);
for (let i = 0; i < permalinks.length; i++) {
if (permalinks[i].onlyFor && !permalinks[i].onlyFor.includes(framework)) {
continue;
}
const permalink = extendPermalink(permalinks[i].permalink, framework);
await page.goto(`http://localhost:${PORT}/docs${permalink}`, {
waitUntil: 'networkidle0'
});
for (let testIndex = 0; testIndex < testCases.length; testIndex++) {
let pageEvaluation = await page.evaluate(testCases[testIndex], permalink);
let tryCount = 0;
// If the test fails, do another try after a timeout (some instances might have not been initialized yet).
while (
(tryCount < CHECK_TRIES) &&
(!pageEvaluation.result || pageEvaluation.elementsNotYetRendered)
) {
tryCount += 1;
// Wait for the HOT instances to initialize.
await sleep(EXAMPLE_INIT_TIMEOUT);
pageEvaluation = await page.evaluate(testCases[testIndex], permalink);
}
if (pageEvaluation.error) {
logger.error(`${permalink}: ${pageEvaluation.error}`);
} else {
// Mark the check as suspicious, if the expected number of instances is 0.
logCheck(pageEvaluation.result);
if (!pageEvaluation.result) {
brokenExamplePaths.push({
path: permalink,
expected: pageEvaluation.expected,
received: pageEvaluation.received,
emptyExampleContainers: pageEvaluation.emptyExampleContainers
});
}
}
}
}
}
/* eslint-enable no-await-in-loop */
await browser.close();
if (brokenExamplePaths.length > 0) {
logger.error(`
Errors:
${brokenExamplePaths.map(
(entry) => {
let errorMessage = '';
if (entry.expected !== entry.received) {
errorMessage += `
- Expected ${entry.expected} Handsontable instances, ${entry.received} were rendered.`;
}
if (entry.emptyExampleContainers.length > 0) {
entry.emptyExampleContainers.forEach((exampleId) => {
errorMessage += `
- Empty example container ("${exampleId}")`;
});
}
return `
- ${entry.path}: ${errorMessage}`;
})
.join('\n')}`
);
process.exit(1);
}
if (brokenExamplePaths.length === 0) {
logger.success('\nDid not find any broken examples.');
process.exit(0);
}
})();