pa11y-ci-reporter-runner 0.6.0 → 0.7.0

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 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 tranformation of results and configuration data. Functionally, it's a mock of the Pa11y CI side of the reporter interface.
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,19 +15,50 @@ 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 a dependency (e.g. `pa11y-ci-reporter-html`) or a path to a reporter file.
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
- The reporter runner has two functions:
22
+ ### Runner States
23
23
 
24
- - `runAll`: Simulates Pa11y CI running the complete analysis from the provided JSON results file, calling all associated reporter functions (`beforeAll`, `begin` and `results`/`error` for each URL, `afterAll`). Once a run has been completed the runner must be reset before running again.
25
- - `reset`: Resets the runner to the initial state.
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
+ flowchart LR;
28
+ init-->beforeAll;
29
+ beforeAll-->beginUrl;
30
+ beginUrl-->urlResults;
31
+ urlResults-->beginUrl;
32
+ urlResults-->afterAll;
33
+ ```
34
+
35
+ - `init`: The initial state of the runner.
36
+ - `beforeAll`: In this state the runner calls the reporter `beforeAll` function.
37
+ - `beginUrl`: In this state the runner calls the reporter `begin` function for a given URL.
38
+ - `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`.
39
+ - `afterAll`: In this state the runner calls the reporter `afterAll` function.
40
+
41
+ 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.
42
+
43
+ ### Runner Execution
44
+
45
+ The reporter runner has four control functions:
46
+
47
+ - `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.
48
+ - `runNext()`: Simulates Pa11y CI running through the next state from the provided JSON results file, calling the associated reporter function as noted above.
49
+ - `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:
50
+ - `targetState`: The target state of the runner (any state above except `init`).
51
+ - `targetUrl`: An optional target URL. If no URL is specified, the runner will stop at the first instance of the target state.
52
+ - `reset()`: Resets the runner to the `init` state. This can be sent from any state.
53
+
54
+ 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.
55
+
56
+ ### Example
26
57
 
27
58
  A complete example is provided below:
28
59
 
29
60
  ```js
30
- const createRunner = require("pa11y-ci-reporter-runner");
61
+ const { createRunner, RunnerStates } = require("pa11y-ci-reporter-runner");
31
62
 
32
63
  const resultsFileName = "pa11yci-results.json";
33
64
  const reporterName = "../test-reporter.js";
@@ -46,15 +77,26 @@ const config = {
46
77
  ],
47
78
  };
48
79
 
49
- test('test reporter', async () => {
80
+ test('test all reporter functions', async () => {
50
81
  const runner = createRunner(resultsFileName, reporterName, reporterOptions, config);
51
82
 
52
83
  await runner.runAll();
53
84
 
54
85
  // Test reporter results
55
86
  });
87
+
88
+ test('test reporter at urlResults state', async () => {
89
+ const runner = createRunner(resultsFileName, reporterName, reporterOptions, config);
90
+
91
+ await runner.runUntil(RunnerStates.beginUrl, 'http://localhost:8080/page1-no-errors.html');
92
+ // The runner is now in the beginUrl state for http://localhost:8080/page1-no-errors.html
93
+ await runner.runNext();
94
+ // The runner is now in the urlResults state for http://localhost:8080/page1-no-errors.html
95
+
96
+ // Test reporter results
97
+ });
56
98
  ```
57
99
 
58
100
  ## Limitations
59
101
 
60
- 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` configuration.
102
+ 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
@@ -12,6 +12,7 @@ const formatter = require('./lib/formatter');
12
12
  const reporterBuilder = require('./lib/reporter-builder');
13
13
  const createConfig = require('./lib/config');
14
14
  const { serviceFactory } = require('./lib/pa11yci-service');
15
+ const RunnerStates = require('./lib/runner-states');
15
16
 
16
17
  /**
17
18
  * Loads the Pa11y CI results from JSON file.
@@ -185,13 +186,41 @@ const createRunner = (resultsFileName, reporterName, options = {}, config = {})
185
186
  * @instance
186
187
  */
187
188
  const runAll = async () => {
188
- await service.runAll();
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);
189
215
  };
190
216
 
191
217
  return {
192
218
  reset,
193
- runAll
219
+ runAll,
220
+ runNext,
221
+ runUntil
194
222
  };
195
223
  };
196
224
 
197
- module.exports = createRunner;
225
+ module.exports.createRunner = createRunner;
226
+ module.exports.RunnerStates = RunnerStates;
@@ -3,12 +3,7 @@
3
3
  const { interpret } = require('xstate');
4
4
  const machine = require('./pa11yci-machine');
5
5
 
6
- const RunnerStates = Object.freeze({
7
- beforeAll: 'beforeAll',
8
- beginUrl: 'beginUrl',
9
- urlResults: 'urlResults',
10
- afterAll: 'afterAll'
11
- });
6
+ const RunnerStates = require('./runner-states');
12
7
  const finalState = RunnerStates.afterAll;
13
8
 
14
9
  /**
@@ -32,6 +27,32 @@ const getInitialContext = urls => ({
32
27
  */
33
28
  const hasUrl = state => state === RunnerStates.beginUrl || state === RunnerStates.urlResults;
34
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
+
35
56
  /**
36
57
  * Factory function that return a pa11yci-runner service.
37
58
  *
@@ -72,30 +93,31 @@ const serviceFactory = (urls, actions) => {
72
93
  service.start();
73
94
 
74
95
  /**
75
- * Sends the NEXT event to the pa11yci-runner service. Resolves when
76
- * the next state has been reached and the reporter event raised.
96
+ * Validate that a command is allowed in the given state. Throws if invalid.
77
97
  *
78
98
  * @private
79
- * @returns {Promise<void>} Promise that indicates a pending state change.
80
99
  */
81
- const next = () => {
82
- return new Promise((resolve, reject) => {
83
- pendingCommand = { resolve, reject };
84
- service.send({ type: 'NEXT' });
85
- });
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
+ }
86
107
  };
87
108
 
88
109
  /**
89
- * Sends the RESET event to the pa11yci-runner service. Resolves when
110
+ * Sends the specified event to the pa11yci-runner service. Resolves when
90
111
  * the next state has been reached and the reporter event raised.
91
112
  *
92
113
  * @private
93
- * @returns {Promise<void>} Promise that indicates a pending state change.
114
+ * @param {string} event The event name to be sent.
115
+ * @returns {Promise<void>} Promise that indicates a pending state change.
94
116
  */
95
- const resetInternal = () => {
117
+ const sendEvent = (event) => {
96
118
  return new Promise((resolve, reject) => {
97
119
  pendingCommand = { resolve, reject };
98
- service.send({ type: 'RESET' });
120
+ service.send({ type: event });
99
121
  });
100
122
  };
101
123
 
@@ -106,31 +128,53 @@ const serviceFactory = (urls, actions) => {
106
128
  * @instance
107
129
  */
108
130
  const reset = async () => {
109
- await resetInternal();
131
+ await sendEvent('RESET');
110
132
  };
111
133
 
112
134
  /**
113
- * Executes the entire Pa11y CI sequence, calling all reporter functions.
135
+ * Executes the next event in the Pa11y CI sequence, calling the
136
+ * appropriate reporter function.
114
137
  *
115
138
  * @public
116
139
  * @instance
117
140
  */
118
- const runAll = async () => {
119
- if (currentContext.state === finalState) {
120
- throw new Error(`runner must be reset before executing any other functions from the ${finalState} state`);
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');
121
164
  }
122
165
 
123
- // Since this runs the entire sequence, send the NEXT event
124
- // until the final state is reached.
125
- while (currentContext.state !== finalState) {
126
- // eslint-disable-next-line node/callback-return -- not a callback
127
- await next();
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`);
128
171
  }
129
172
  };
130
173
 
131
174
  return {
132
175
  reset,
133
- runAll
176
+ runNext,
177
+ runUntil
134
178
  };
135
179
  };
136
180
 
@@ -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.6.0",
3
+ "version": "0.7.0",
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": {
@@ -36,7 +36,7 @@
36
36
  "homepage": "https://gitlab.com/gitlab-ci-utils/pa11y-ci-reporter-runner#readme",
37
37
  "devDependencies": {
38
38
  "@aarongoldenthal/eslint-config-standard": "^12.0.2",
39
- "eslint": "^8.9.0",
39
+ "eslint": "^8.10.0",
40
40
  "jest": "^27.5.1",
41
41
  "jest-junit": "^13.0.0",
42
42
  "markdownlint-cli": "^0.31.1",
@@ -44,6 +44,6 @@
44
44
  },
45
45
  "dependencies": {
46
46
  "lodash": "^4.17.21",
47
- "xstate": "^4.30.2"
47
+ "xstate": "^4.30.3"
48
48
  }
49
49
  }