pa11y-ci-reporter-runner 0.5.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -7
- package/index.js +127 -26
- package/lib/config.js +1 -1
- package/lib/formatter.js +4 -8
- package/lib/pa11yci-machine.js +68 -0
- package/lib/pa11yci-service.js +183 -0
- package/lib/{reporterBuilder.js → reporter-builder.js} +5 -4
- package/lib/runner-states.js +21 -0
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Pa11y CI Reporter Runner
|
|
2
2
|
|
|
3
|
-
Pa11y CI Reporter Runner is designed to facilitate testing of [Pa11y CI reporters](https://github.com/pa11y/pa11y-ci#write-a-custom-reporter). Given a Pa11y CI JSON results file and optional configuration it simulates the Pa11y CI calls to the reporter, including proper
|
|
3
|
+
Pa11y CI Reporter Runner is designed to facilitate testing of [Pa11y CI reporters](https://github.com/pa11y/pa11y-ci#write-a-custom-reporter). Given a Pa11y CI JSON results file and optional configuration it simulates the Pa11y CI calls to the reporter, including proper transformation of results and configuration data. Functionally, it's an emulation of the Pa11y CI side of the reporter interface with finer control over execution.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -15,18 +15,52 @@ npm install pa11y-ci-reporter-runner
|
|
|
15
15
|
Pa11y CI Reporter Runner exports a factory function that creates a reporter runner. This function has four arguments:
|
|
16
16
|
|
|
17
17
|
- `resultsFileName`: Path to a Pa11y CI JSON results file.
|
|
18
|
-
- `reporterName`: Name of the reporter to execute. Can be
|
|
18
|
+
- `reporterName`: Name of the reporter to execute. Can be an npm module (e.g. `pa11y-ci-reporter-html`) or a path to a reporter file.
|
|
19
19
|
- `options`: Optional [reporter options](https://github.com/pa11y/pa11y-ci#reporter-options).
|
|
20
20
|
- `config`: Optional Pa11y CI configuration file that produces the Pa11y CI JSON results.
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
### Runner States
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
Pa11y CI Reporter Runner emulates calls from Pa11y CI to the given reporter for the given results file. There are five distinct states for the runner, as shown below:
|
|
25
|
+
|
|
26
|
+
```mermaid
|
|
27
|
+
%% It's unfortunate that mermaid charts don't render on npmjs.com,
|
|
28
|
+
%% see the README in the repository to view the flowchart
|
|
29
|
+
flowchart LR;
|
|
30
|
+
init-->beforeAll;
|
|
31
|
+
beforeAll-->beginUrl;
|
|
32
|
+
beginUrl-->urlResults;
|
|
33
|
+
urlResults-->beginUrl;
|
|
34
|
+
urlResults-->afterAll;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- `init`: The initial state of the runner.
|
|
38
|
+
- `beforeAll`: In this state the runner calls the reporter `beforeAll` function.
|
|
39
|
+
- `beginUrl`: In this state the runner calls the reporter `begin` function for a given URL.
|
|
40
|
+
- `urlResults`: In this state the runner calls the reporter `results` or `error` function depending on the result for that URL. If there are subsequent URLs, the runner next moves to `beginUrl` for the next URL. If this is the last URL, the runner moves to `afterAll`.
|
|
41
|
+
- `afterAll`: In this state the runner calls the reporter `afterAll` function.
|
|
42
|
+
|
|
43
|
+
When calling reporter functions, the runner transforms the Pa11y CI results and configuration data to provide the appropriate arguments. For example, the `results` function is called with the results as returned from Pa11y (slightly different than those returned from Pa11y CI) and the consolidated configuration for the analysis of that URL.
|
|
44
|
+
|
|
45
|
+
### Runner Execution
|
|
46
|
+
|
|
47
|
+
The reporter runner has four control functions:
|
|
48
|
+
|
|
49
|
+
- `runAll()`: Simulates Pa11y CI running the analysis from the provided JSON results file from the current state through the end, calling all associated reporter functions.
|
|
50
|
+
- `runNext()`: Simulates Pa11y CI running through the next state from the provided JSON results file, calling the associated reporter function as noted above.
|
|
51
|
+
- `runUntil(targetState, targetUrl)`: Simulates Pa11y CI running the analysis from the provided JSON results file from the current state through the specified state/URL, calling the associated reporter functions as noted above. An error will be thrown if the end of the results are reached and the target was not found. This function takes the following arguments:
|
|
52
|
+
- `targetState`: The target state of the runner (any state above except `init`).
|
|
53
|
+
- `targetUrl`: An optional target URL. If no URL is specified, the runner will stop at the first instance of the target state.
|
|
54
|
+
- `reset()`: Resets the runner to the `init` state. This can be sent from any state.
|
|
55
|
+
|
|
56
|
+
These command are all asynchronous and must be completed before another is sent, otherwise an error will be thrown. In addition, once a run has been completed and the runner is in the `afterAll` state it must be `reset` before accepting any run command.
|
|
57
|
+
|
|
58
|
+
### Example
|
|
25
59
|
|
|
26
60
|
A complete example is provided below:
|
|
27
61
|
|
|
28
62
|
```js
|
|
29
|
-
const createRunner = require("pa11y-ci-reporter-runner");
|
|
63
|
+
const { createRunner, RunnerStates } = require("pa11y-ci-reporter-runner");
|
|
30
64
|
|
|
31
65
|
const resultsFileName = "pa11yci-results.json";
|
|
32
66
|
const reporterName = "../test-reporter.js";
|
|
@@ -45,15 +79,26 @@ const config = {
|
|
|
45
79
|
],
|
|
46
80
|
};
|
|
47
81
|
|
|
48
|
-
test('test reporter', async () => {
|
|
82
|
+
test('test all reporter functions', async () => {
|
|
49
83
|
const runner = createRunner(resultsFileName, reporterName, reporterOptions, config);
|
|
50
84
|
|
|
51
85
|
await runner.runAll();
|
|
52
86
|
|
|
53
87
|
// Test reporter results
|
|
54
88
|
});
|
|
89
|
+
|
|
90
|
+
test('test reporter at urlResults state', async () => {
|
|
91
|
+
const runner = createRunner(resultsFileName, reporterName, reporterOptions, config);
|
|
92
|
+
|
|
93
|
+
await runner.runUntil(RunnerStates.beginUrl, 'http://localhost:8080/page1-no-errors.html');
|
|
94
|
+
// The runner is now in the beginUrl state for http://localhost:8080/page1-no-errors.html
|
|
95
|
+
await runner.runNext();
|
|
96
|
+
// The runner is now in the urlResults state for http://localhost:8080/page1-no-errors.html
|
|
97
|
+
|
|
98
|
+
// Test reporter results
|
|
99
|
+
});
|
|
55
100
|
```
|
|
56
101
|
|
|
57
102
|
## Limitations
|
|
58
103
|
|
|
59
|
-
When passing config to `results`, `error`, and `afterAll`, Pa11y CI Reporter Runner includes the same properties as Pa11y CI except the `browser` property (with the `puppeteer` `browser` object used by Pa11y CI). If the `browser` object is needed, testing should be done with Pa11y CI to ensure proper `browser`
|
|
104
|
+
When passing config to `results`, `error`, and `afterAll`, Pa11y CI Reporter Runner includes the same properties as Pa11y CI except the `browser` property (with the `puppeteer` `browser` object used by Pa11y CI). If the `browser` object is needed, testing should be done with Pa11y CI to ensure proper `browser` capabilities are available.
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Pa11y CI Reporter Runner allows a
|
|
4
|
+
* Pa11y CI Reporter Runner allows a Pa11y CI reporter to be run
|
|
5
5
|
* from a JSON results file without using pa11y-ci.
|
|
6
6
|
*
|
|
7
7
|
* @module pa11y-ci-reporter-runner
|
|
@@ -9,9 +9,18 @@
|
|
|
9
9
|
|
|
10
10
|
const fs = require('fs');
|
|
11
11
|
const formatter = require('./lib/formatter');
|
|
12
|
-
const reporterBuilder = require('./lib/
|
|
12
|
+
const reporterBuilder = require('./lib/reporter-builder');
|
|
13
13
|
const createConfig = require('./lib/config');
|
|
14
|
+
const { serviceFactory } = require('./lib/pa11yci-service');
|
|
15
|
+
const RunnerStates = require('./lib/runner-states');
|
|
14
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Loads the Pa11y CI results from JSON file.
|
|
19
|
+
*
|
|
20
|
+
* @private
|
|
21
|
+
* @param {string} fileName Pa11y CI JSON results file name.
|
|
22
|
+
* @returns {object} Pa11y CI results.
|
|
23
|
+
*/
|
|
15
24
|
const loadPa11yciResults = fileName => {
|
|
16
25
|
if (typeof fileName !== 'string') {
|
|
17
26
|
throw new TypeError('fileName must be a string');
|
|
@@ -21,20 +30,17 @@ const loadPa11yciResults = fileName => {
|
|
|
21
30
|
const results = JSON.parse(fs.readFileSync(fileName, 'utf8'));
|
|
22
31
|
return formatter.convertJsonToResultsObject(results);
|
|
23
32
|
}
|
|
24
|
-
catch (
|
|
25
|
-
throw new Error(`Error loading results file - ${
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw new Error(`Error loading results file - ${error.message}`);
|
|
26
35
|
}
|
|
27
36
|
};
|
|
28
37
|
|
|
29
|
-
const isError = results => results.length === 1 && results[0] instanceof Error;
|
|
30
|
-
|
|
31
38
|
/**
|
|
32
39
|
* Pa11y CI configuration allows config.urls entries to be either url strings
|
|
33
40
|
* or object with url and other configuration, so this return a list of just
|
|
34
41
|
* the url strings.
|
|
35
42
|
*
|
|
36
43
|
* @private
|
|
37
|
-
* @static
|
|
38
44
|
* @param {object[]} values The urls array from configuration.
|
|
39
45
|
* @returns {string[]} Array of URL strings.
|
|
40
46
|
*/
|
|
@@ -55,13 +61,12 @@ const getUrlList = values => {
|
|
|
55
61
|
};
|
|
56
62
|
|
|
57
63
|
/**
|
|
58
|
-
* Compares URLs in
|
|
64
|
+
* Compares URLs in Pa11y CI results and configuration files to ensure
|
|
59
65
|
* consistency (i.e. The same URLs are in both lists, although they
|
|
60
66
|
* may not be in the same order). URLs in config are only checked if
|
|
61
67
|
* provided. If specified and inconsistent, throws error.
|
|
62
68
|
*
|
|
63
69
|
* @private
|
|
64
|
-
* @static
|
|
65
70
|
* @param {object} results Pa11y CI JSON results file.
|
|
66
71
|
* @param {object} config Pa11y CI configuration.
|
|
67
72
|
*/
|
|
@@ -78,6 +83,15 @@ const validateUrls = (results, config) => {
|
|
|
78
83
|
}
|
|
79
84
|
};
|
|
80
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Check if the given Pa11y results are an execution error.
|
|
88
|
+
*
|
|
89
|
+
* @private
|
|
90
|
+
* @param {object} results Pa11y results for a single URL.
|
|
91
|
+
* @returns {boolean} True if the results are an execution error.
|
|
92
|
+
*/
|
|
93
|
+
const isError = results => results.length === 1 && results[0] instanceof Error;
|
|
94
|
+
|
|
81
95
|
/**
|
|
82
96
|
* Factory to create a pa11y-ci reporter runner that can execute
|
|
83
97
|
* a reporter with the specified pa11y-ci JSON results file.
|
|
@@ -90,6 +104,7 @@ const validateUrls = (results, config) => {
|
|
|
90
104
|
* @param {object} config The Pa11y CI configuration.
|
|
91
105
|
* @returns {object} A Pa11y CI reporter runner.
|
|
92
106
|
*/
|
|
107
|
+
// eslint-disable-next-line max-lines-per-function
|
|
93
108
|
const createRunner = (resultsFileName, reporterName, options = {}, config = {}) => {
|
|
94
109
|
const pa11yciResults = loadPa11yciResults(resultsFileName);
|
|
95
110
|
|
|
@@ -97,29 +112,115 @@ const createRunner = (resultsFileName, reporterName, options = {}, config = {})
|
|
|
97
112
|
|
|
98
113
|
const pa11yciConfig = createConfig(config);
|
|
99
114
|
const reporter = reporterBuilder.buildReporter(reporterName, options, pa11yciConfig.defaults);
|
|
115
|
+
const urls = config.urls || Object.keys(pa11yciResults.results);
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Implements the runner beforeAll event, calling reporter.beforeAll.
|
|
119
|
+
*
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
const beforeAll = async () => {
|
|
123
|
+
await reporter.beforeAll(urls);
|
|
124
|
+
};
|
|
100
125
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Implements the runner beginUrl event, calling reporter.begin.
|
|
128
|
+
*
|
|
129
|
+
* @private
|
|
130
|
+
* @param {string} url The url being analyzed.
|
|
131
|
+
*/
|
|
132
|
+
const beginUrl = async (url) => {
|
|
133
|
+
await reporter.begin(url);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Implements the runner urlResults event, calling reporter.results or
|
|
138
|
+
* reporter.error as appropriate based on the results.
|
|
139
|
+
*
|
|
140
|
+
* @private
|
|
141
|
+
* @param {string} url The url being analyzed.
|
|
142
|
+
*/
|
|
143
|
+
const urlResults = async (url) => {
|
|
144
|
+
const results = pa11yciResults.results[url];
|
|
145
|
+
const urlConfig = pa11yciConfig.getConfigForUrl(url);
|
|
146
|
+
await (isError(results)
|
|
147
|
+
? reporter.error(results[0], url, urlConfig)
|
|
148
|
+
: reporter.results(formatter.getPa11yResultsFromPa11yCiResults(url, pa11yciResults), urlConfig));
|
|
149
|
+
};
|
|
116
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Implements the runner afterAll event, calling reporter.afterAll.
|
|
153
|
+
*
|
|
154
|
+
* @private
|
|
155
|
+
*/
|
|
156
|
+
const afterAll = async () => {
|
|
117
157
|
await reporter.afterAll(pa11yciResults, pa11yciConfig.defaults);
|
|
118
158
|
};
|
|
119
159
|
|
|
160
|
+
// Collection of runner actions to be passed to the state service.
|
|
161
|
+
const actions = {
|
|
162
|
+
beforeAll,
|
|
163
|
+
beginUrl,
|
|
164
|
+
urlResults,
|
|
165
|
+
afterAll
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// URLs for the service is always the array of result URLs since they
|
|
169
|
+
// are used to retrieve the results.
|
|
170
|
+
const service = serviceFactory(Object.keys(pa11yciResults.results), actions);
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Resets the runner to the initial state.
|
|
174
|
+
*
|
|
175
|
+
* @public
|
|
176
|
+
* @instance
|
|
177
|
+
*/
|
|
178
|
+
const reset = async () => {
|
|
179
|
+
await service.reset();
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Executes the entire Pa11y CI sequence, calling all reporter functions.
|
|
184
|
+
*
|
|
185
|
+
* @public
|
|
186
|
+
* @instance
|
|
187
|
+
*/
|
|
188
|
+
const runAll = async () => {
|
|
189
|
+
await service.runUntil(RunnerStates.afterAll);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Executes the next event in the Pa11y CI sequence, calling the
|
|
194
|
+
* appropriate reporter function.
|
|
195
|
+
*
|
|
196
|
+
* @public
|
|
197
|
+
* @instance
|
|
198
|
+
*/
|
|
199
|
+
const runNext = async () => {
|
|
200
|
+
await service.runNext();
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Executes the entire Pa11y CI sequence, calling all reporter functions,
|
|
205
|
+
* until the specified state and optional URL are reached. If no URL is provided
|
|
206
|
+
* specified, the run completes on the first occurrence of the target state.
|
|
207
|
+
*
|
|
208
|
+
* @public
|
|
209
|
+
* @interface
|
|
210
|
+
* @param {string} targetState The target state to run to.
|
|
211
|
+
* @param {string} [targetUrl] The target URL to run to.
|
|
212
|
+
*/
|
|
213
|
+
const runUntil = async (targetState, targetUrl) => {
|
|
214
|
+
await service.runUntil(targetState, targetUrl);
|
|
215
|
+
};
|
|
216
|
+
|
|
120
217
|
return {
|
|
121
|
-
|
|
218
|
+
reset,
|
|
219
|
+
runAll,
|
|
220
|
+
runNext,
|
|
221
|
+
runUntil
|
|
122
222
|
};
|
|
123
223
|
};
|
|
124
224
|
|
|
125
|
-
module.exports = createRunner;
|
|
225
|
+
module.exports.createRunner = createRunner;
|
|
226
|
+
module.exports.RunnerStates = RunnerStates;
|
package/lib/config.js
CHANGED
|
@@ -20,7 +20,7 @@ const normalizeConfig = (config) => {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Factory function that returns a config object for managing
|
|
23
|
+
* Factory function that returns a config object for managing Pa11y CI
|
|
24
24
|
* configuration including defaults and determining the consolidated
|
|
25
25
|
* configuration for each URL.
|
|
26
26
|
*
|
package/lib/formatter.js
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* @module formatter
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
const isError = issues => issues.length === 1 && Object.keys(issues[0]).length === 1
|
|
10
|
+
&& issues[0].message;
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* Converts Pa11y CI JSON output to an equivalent Pa11y CI object,
|
|
11
14
|
* allowing JSON result files to be used for reporter interface
|
|
@@ -25,13 +28,7 @@ const convertJsonToResultsObject = jsonResults => {
|
|
|
25
28
|
|
|
26
29
|
for (const url of Object.keys(jsonResults.results)) {
|
|
27
30
|
const issues = jsonResults.results[url];
|
|
28
|
-
|
|
29
|
-
if (issues.length === 1 && Object.keys(issues[0]).length === 1 && issues[0].message) {
|
|
30
|
-
formattedIssues = [new Error(issues[0].message)];
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
formattedIssues = issues;
|
|
34
|
-
}
|
|
31
|
+
const formattedIssues = isError(issues) ? [new Error(issues[0].message)] : issues;
|
|
35
32
|
results.results[url] = formattedIssues;
|
|
36
33
|
}
|
|
37
34
|
|
|
@@ -63,4 +60,3 @@ const getPa11yResultsFromPa11yCiResults = (url, results) => {
|
|
|
63
60
|
|
|
64
61
|
module.exports.convertJsonToResultsObject = convertJsonToResultsObject;
|
|
65
62
|
module.exports.getPa11yResultsFromPa11yCiResults = getPa11yResultsFromPa11yCiResults;
|
|
66
|
-
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { createMachine, assign } = require('xstate');
|
|
4
|
+
|
|
5
|
+
const pa11yciMachine = createMachine(
|
|
6
|
+
{
|
|
7
|
+
id: 'pa11yci-runner',
|
|
8
|
+
initial: 'init',
|
|
9
|
+
context: {
|
|
10
|
+
urlIndex: 0,
|
|
11
|
+
urls: []
|
|
12
|
+
},
|
|
13
|
+
states: {
|
|
14
|
+
init: {
|
|
15
|
+
entry: 'setInitialUrlIndex',
|
|
16
|
+
on: {
|
|
17
|
+
NEXT: { target: 'beforeAll' }
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
beforeAll: {
|
|
21
|
+
on: {
|
|
22
|
+
NEXT: [
|
|
23
|
+
{ target: 'beginUrl', cond: 'hasUrls' },
|
|
24
|
+
{ target: 'afterAll' }
|
|
25
|
+
],
|
|
26
|
+
RESET: { target: 'init' }
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
beginUrl: {
|
|
30
|
+
on: {
|
|
31
|
+
NEXT: { target: 'urlResults' },
|
|
32
|
+
RESET: { target: 'init' }
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
urlResults: {
|
|
36
|
+
on: {
|
|
37
|
+
NEXT: [
|
|
38
|
+
{ target: 'afterAll', cond: 'isLastUrl' },
|
|
39
|
+
{ target: 'beginUrl', actions: 'incrementUrl' }
|
|
40
|
+
],
|
|
41
|
+
RESET: { target: 'init' }
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
afterAll: {
|
|
45
|
+
on: {
|
|
46
|
+
RESET: { target: 'init' }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
actions: {
|
|
53
|
+
incrementUrl: assign({
|
|
54
|
+
urlIndex: (context) => context.urlIndex + 1
|
|
55
|
+
}),
|
|
56
|
+
setInitialUrlIndex: assign({
|
|
57
|
+
urlIndex: () => 0
|
|
58
|
+
})
|
|
59
|
+
},
|
|
60
|
+
guards: {
|
|
61
|
+
hasUrls: (context) => context.urls.length > 0,
|
|
62
|
+
isLastUrl: (context) =>
|
|
63
|
+
context.urlIndex === context.urls.length - 1
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
module.exports = pa11yciMachine;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { interpret } = require('xstate');
|
|
4
|
+
const machine = require('./pa11yci-machine');
|
|
5
|
+
|
|
6
|
+
const RunnerStates = require('./runner-states');
|
|
7
|
+
const finalState = RunnerStates.afterAll;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Gets the initial pa11yci-runner state machine context given an array of URLs.
|
|
11
|
+
*
|
|
12
|
+
* @private
|
|
13
|
+
* @param {Array} urls Array of URLs.
|
|
14
|
+
* @returns {object} The initial state machine context.
|
|
15
|
+
*/
|
|
16
|
+
const getInitialContext = urls => ({
|
|
17
|
+
urlIndex: 0,
|
|
18
|
+
urls
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Checks the state to determine whether it has an associated URL.
|
|
23
|
+
*
|
|
24
|
+
* @private
|
|
25
|
+
* @param {RunnerStates} state The state to check.
|
|
26
|
+
* @returns {boolean} True if the state has an associated url.
|
|
27
|
+
*/
|
|
28
|
+
const hasUrl = state => state === RunnerStates.beginUrl || state === RunnerStates.urlResults;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Validates that the given state is a valid RunnerStates value. Throws if not.
|
|
32
|
+
*
|
|
33
|
+
* @private
|
|
34
|
+
* @param {string} state The state to validate.
|
|
35
|
+
*/
|
|
36
|
+
const validateRunnerState = (state) => {
|
|
37
|
+
if (!Object.keys(RunnerStates).includes(state)) {
|
|
38
|
+
throw new TypeError(`targetState "${state}" invalid`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Checks if the context matches the targetState and either no
|
|
44
|
+
* targetUrl was specified or the context matches the targetUrl.
|
|
45
|
+
*
|
|
46
|
+
* @private
|
|
47
|
+
* @param {object} context The current state machine context.
|
|
48
|
+
* @param {string} targetState The target runner state.
|
|
49
|
+
* @param {string} [targetUrl] The target URL.
|
|
50
|
+
* @returns {boolean} True if the context matches the target, otherwise false.
|
|
51
|
+
*/
|
|
52
|
+
const isAtTarget = (context, targetState, targetUrl) => {
|
|
53
|
+
return context.state === targetState && (!targetUrl || context.url === targetUrl);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Factory function that return a pa11yci-runner service.
|
|
58
|
+
*
|
|
59
|
+
* @public
|
|
60
|
+
* @static
|
|
61
|
+
* @param {Array} urls Array of URLs.
|
|
62
|
+
* @param {object} actions Actions object with functions to execute for each event.
|
|
63
|
+
* @returns {object} A pa11yci-runner service.
|
|
64
|
+
*/
|
|
65
|
+
// eslint-disable-next-line max-lines-per-function
|
|
66
|
+
const serviceFactory = (urls, actions) => {
|
|
67
|
+
let pendingCommand;
|
|
68
|
+
const currentContext = {
|
|
69
|
+
state: undefined,
|
|
70
|
+
url: undefined
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Create service from pa11yci-machine with context, setup
|
|
74
|
+
// transition event handler, and start service
|
|
75
|
+
const service = interpret(machine.withContext(getInitialContext(urls)));
|
|
76
|
+
service.onTransition(async (state) => {
|
|
77
|
+
// Save the current state and url for use in manual transitions.
|
|
78
|
+
// The state machine only increments the url index, so use that
|
|
79
|
+
// to get the url from the original urls array.
|
|
80
|
+
currentContext.state = state.value;
|
|
81
|
+
currentContext.url = hasUrl(currentContext.state)
|
|
82
|
+
? urls[state.context.urlIndex] : undefined;
|
|
83
|
+
|
|
84
|
+
if (pendingCommand) {
|
|
85
|
+
// Do not take any action in init state
|
|
86
|
+
if (currentContext.state !== 'init') {
|
|
87
|
+
await actions[currentContext.state](currentContext.url);
|
|
88
|
+
}
|
|
89
|
+
pendingCommand.resolve();
|
|
90
|
+
pendingCommand = undefined;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
service.start();
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Validate that a command is allowed in the given state. Throws if invalid.
|
|
97
|
+
*
|
|
98
|
+
* @private
|
|
99
|
+
*/
|
|
100
|
+
const validateCommandAllowed = () => {
|
|
101
|
+
if (pendingCommand) {
|
|
102
|
+
throw new Error('runner cannot accept a command while another command is pending, await previous command');
|
|
103
|
+
}
|
|
104
|
+
if (currentContext.state === finalState) {
|
|
105
|
+
throw new Error(`runner must be reset before executing any other functions from the ${finalState} state`);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Sends the specified event to the pa11yci-runner service. Resolves when
|
|
111
|
+
* the next state has been reached and the reporter event raised.
|
|
112
|
+
*
|
|
113
|
+
* @private
|
|
114
|
+
* @param {string} event The event name to be sent.
|
|
115
|
+
* @returns {Promise<void>} Promise that indicates a pending state change.
|
|
116
|
+
*/
|
|
117
|
+
const sendEvent = (event) => {
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
pendingCommand = { resolve, reject };
|
|
120
|
+
service.send({ type: event });
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Resets the service to the init state.
|
|
126
|
+
*
|
|
127
|
+
* @public
|
|
128
|
+
* @instance
|
|
129
|
+
*/
|
|
130
|
+
const reset = async () => {
|
|
131
|
+
await sendEvent('RESET');
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Executes the next event in the Pa11y CI sequence, calling the
|
|
136
|
+
* appropriate reporter function.
|
|
137
|
+
*
|
|
138
|
+
* @public
|
|
139
|
+
* @instance
|
|
140
|
+
*/
|
|
141
|
+
const runNext = async () => {
|
|
142
|
+
validateCommandAllowed();
|
|
143
|
+
|
|
144
|
+
await sendEvent('NEXT');
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Executes the entire Pa11y CI sequence, calling all reporter functions,
|
|
149
|
+
* until the specified state and optional URL are reached. If no URL is provided
|
|
150
|
+
* specified, the run completes on the first occurrence of the target state.
|
|
151
|
+
*
|
|
152
|
+
* @public
|
|
153
|
+
* @interface
|
|
154
|
+
* @param {string} targetState The target state to run to.
|
|
155
|
+
* @param {string} [targetUrl] The target URL to run to.
|
|
156
|
+
*/
|
|
157
|
+
const runUntil = async (targetState, targetUrl) => {
|
|
158
|
+
validateCommandAllowed();
|
|
159
|
+
validateRunnerState(targetState);
|
|
160
|
+
|
|
161
|
+
while (!isAtTarget(currentContext, targetState, targetUrl)
|
|
162
|
+
&& currentContext.state !== finalState) {
|
|
163
|
+
await sendEvent('NEXT');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// If the finalState is reached and not at target then it is
|
|
167
|
+
// not in the results and throw to indicate the command failed.
|
|
168
|
+
if (!isAtTarget(currentContext, targetState, targetUrl)) {
|
|
169
|
+
const urlString = targetUrl ? ` for targetUrl "${targetUrl}"` : '';
|
|
170
|
+
throw new Error(`targetState "${targetState}"${urlString} was not found`);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
reset,
|
|
176
|
+
runNext,
|
|
177
|
+
runUntil
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
module.exports = {
|
|
182
|
+
serviceFactory
|
|
183
|
+
};
|
|
@@ -27,6 +27,7 @@ const loadReporter = reporterName => {
|
|
|
27
27
|
* @param {object} config The Pa11y CI configuration.
|
|
28
28
|
* @returns {object} The reporter object.
|
|
29
29
|
*/
|
|
30
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity -- allow < 10
|
|
30
31
|
const buildReporter = (reporterName, options, config) => {
|
|
31
32
|
if (typeof reporterName !== 'string') {
|
|
32
33
|
throw new TypeError('reporterName must be a string');
|
|
@@ -37,15 +38,15 @@ const buildReporter = (reporterName, options, config) => {
|
|
|
37
38
|
if (typeof reporter === 'function') {
|
|
38
39
|
reporter = reporter(options, config);
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
+
for (const method of reporterMethods) {
|
|
41
42
|
if (typeof reporter[method] !== 'function') {
|
|
42
43
|
reporter[method] = noop;
|
|
43
44
|
}
|
|
44
|
-
}
|
|
45
|
+
}
|
|
45
46
|
return reporter;
|
|
46
47
|
}
|
|
47
|
-
catch (
|
|
48
|
-
throw new Error(`Error loading reporter: ${
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new Error(`Error loading reporter: ${error.message}`);
|
|
49
50
|
}
|
|
50
51
|
};
|
|
51
52
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @module runner-states
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Enum for runner states.
|
|
9
|
+
*
|
|
10
|
+
* @enum {string}
|
|
11
|
+
* @readonly
|
|
12
|
+
* @static
|
|
13
|
+
*/
|
|
14
|
+
const RunnerStates = Object.freeze({
|
|
15
|
+
beforeAll: 'beforeAll',
|
|
16
|
+
beginUrl: 'beginUrl',
|
|
17
|
+
urlResults: 'urlResults',
|
|
18
|
+
afterAll: 'afterAll'
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
module.exports = RunnerStates;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pa11y-ci-reporter-runner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Pa11y CI Reporter Runner is designed to facilitate testing of Pa11y CI reporters. Given a Pa11y CI JSON results file and optional configuration it simulates the Pa11y CI calls to the reporter.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -35,14 +35,15 @@
|
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://gitlab.com/gitlab-ci-utils/pa11y-ci-reporter-runner#readme",
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@aarongoldenthal/eslint-config-standard": "^
|
|
39
|
-
"eslint": "^8.
|
|
40
|
-
"jest": "^27.
|
|
38
|
+
"@aarongoldenthal/eslint-config-standard": "^12.0.2",
|
|
39
|
+
"eslint": "^8.10.0",
|
|
40
|
+
"jest": "^27.5.1",
|
|
41
41
|
"jest-junit": "^13.0.0",
|
|
42
|
-
"markdownlint-cli": "^0.
|
|
42
|
+
"markdownlint-cli": "^0.31.1",
|
|
43
43
|
"pa11y-ci-reporter-cli-summary": "^1.0.1"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"lodash": "^4.17.21"
|
|
46
|
+
"lodash": "^4.17.21",
|
|
47
|
+
"xstate": "^4.30.5"
|
|
47
48
|
}
|
|
48
49
|
}
|