pa11y-ci-reporter-runner 3.0.1 → 4.1.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2022 - 2023 Aaron Goldenthal
3
+ Copyright (c) 2022 - 2024 Aaron Goldenthal
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/index.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * @module pa11y-ci-reporter-runner
9
9
  */
10
10
 
11
- const fs = require('fs');
11
+ const fs = require('node:fs');
12
12
  const formatter = require('./lib/formatter');
13
13
  const reporterBuilder = require('./lib/reporter-builder');
14
14
  const createConfig = require('./lib/config');
@@ -13,10 +13,10 @@ const { createMachine, assign } = require('xstate');
13
13
  */
14
14
  const pa11yciMachine = createMachine(
15
15
  {
16
- context: {
17
- urlIndex: 0,
18
- urls: []
19
- },
16
+ context: ({ input }) => ({
17
+ urlIndex: input.urlIndex,
18
+ urls: input.urls
19
+ }),
20
20
  id: 'pa11yci-runner',
21
21
  initial: 'init',
22
22
  // Opting in for v4, will be default in xstate v5
@@ -34,7 +34,7 @@ const pa11yciMachine = createMachine(
34
34
  beforeAll: {
35
35
  on: {
36
36
  NEXT: [
37
- { target: 'beginUrl', cond: 'hasUrls' },
37
+ { target: 'beginUrl', guard: 'hasUrls' },
38
38
  { target: 'afterAll' }
39
39
  ],
40
40
  RESET: { target: 'init' }
@@ -49,7 +49,7 @@ const pa11yciMachine = createMachine(
49
49
  urlResults: {
50
50
  on: {
51
51
  NEXT: [
52
- { target: 'afterAll', cond: 'isLastUrl' },
52
+ { target: 'afterAll', guard: 'isLastUrl' },
53
53
  { target: 'beginUrl', actions: 'incrementUrl' }
54
54
  ],
55
55
  RESET: { target: 'init' }
@@ -66,15 +66,16 @@ const pa11yciMachine = createMachine(
66
66
  {
67
67
  actions: {
68
68
  incrementUrl: assign({
69
- urlIndex: (context) => context.urlIndex + 1
69
+ urlIndex: ({ context }) => context.urlIndex + 1
70
70
  }),
71
71
  setInitialUrlIndex: assign({
72
72
  urlIndex: () => 0
73
73
  })
74
74
  },
75
75
  guards: {
76
- hasUrls: (context) => context.urls.length > 0,
77
- isLastUrl: (context) => context.urlIndex === context.urls.length - 1
76
+ hasUrls: ({ context }) => context.urls.length > 0,
77
+ isLastUrl: ({ context }) =>
78
+ context.urlIndex === context.urls.length - 1
78
79
  }
79
80
  }
80
81
  );
@@ -7,6 +7,7 @@
7
7
  * @module pa11yci-service
8
8
  */
9
9
 
10
+ const { createActor, getNextSnapshot } = require('xstate');
10
11
  const machine = require('./pa11yci-machine');
11
12
  const RunnerStates = require('./runner-states');
12
13
 
@@ -38,6 +39,14 @@ const StateTypes = Object.freeze({
38
39
  next: 'next'
39
40
  });
40
41
 
42
+ /**
43
+ * Generates a machine event object for the given event type.
44
+ *
45
+ * @param {string} eventType The type of event (a MachineEvents value).
46
+ * @returns {object} The machine event object.
47
+ */
48
+ const getMachineEvent = (eventType) => ({ type: eventType });
49
+
41
50
  /**
42
51
  * Gets the initial pa11yci-runner state machine context given an array of URLs.
43
52
  *
@@ -68,6 +77,7 @@ const hasUrl = (state) =>
68
77
  * @private
69
78
  */
70
79
  const getUrlForState = (machineState) =>
80
+ // Only a subset of states have an associated URL
71
81
  hasUrl(machineState.value)
72
82
  ? // User supplied input used to index user supplied data file
73
83
  // nosemgrep: eslint.detect-object-injection
@@ -126,12 +136,17 @@ const getStateSummary = (machineState) => ({
126
136
  const serviceFactory = (urls, actions) => {
127
137
  let pendingCommand;
128
138
 
129
- // Implement custom xstate interpreter, which allows for tracking for current
139
+ // Implement custom xstate actor, which allows for tracking for current
130
140
  // and the next state, which is required for some control functions.
131
- const pa11yMachine = machine.withContext(getInitialContext(urls));
132
- let currentState = pa11yMachine.initialState;
133
- // Machine.transition is a pure function, so only retrieves the state
134
- let nextState = machine.transition(currentState, MachineEvents.NEXT);
141
+ const pa11yActor = createActor(machine, {
142
+ input: getInitialContext(urls)
143
+ });
144
+ let currentState = pa11yActor.getSnapshot();
145
+ let nextState = getNextSnapshot(
146
+ machine,
147
+ currentState,
148
+ getMachineEvent(MachineEvents.NEXT)
149
+ );
135
150
 
136
151
  /**
137
152
  * Validates that a command is allowed in the given state. Throws if invalid.
@@ -167,24 +182,28 @@ const serviceFactory = (urls, actions) => {
167
182
  pendingCommand = true;
168
183
 
169
184
  // Send event to the machine and executes the action for the
170
- // current state (except in init, which has no action)
171
- currentState = machine.transition(currentState, event);
185
+ // new current state (except in init, which has no action).
186
+ currentState = getNextSnapshot(
187
+ machine,
188
+ currentState,
189
+ getMachineEvent(event)
190
+ );
172
191
  if (currentState.value !== 'init') {
173
- // Value is internal object state
192
+ // Value is internal object state.
174
193
  // nosemgrep: eslint.detect-object-injection, unsafe-dynamic-method
175
194
  await actions[currentState.value](getUrlForState(currentState));
176
195
  }
177
196
 
178
- // Check next state and save for reference (machine.transition
179
- // is a pure function, so only retrieves the state)
197
+ // Check next state and save for reference.
180
198
  if (currentState.value !== finalState) {
181
- nextState = machine.transition(
199
+ nextState = getNextSnapshot(
200
+ machine,
182
201
  currentState,
183
- MachineEvents.NEXT
202
+ getMachineEvent(MachineEvents.NEXT)
184
203
  );
185
204
  }
186
205
  } finally {
187
- // Ensure pending command is reset in all cases, including on error
206
+ // Ensure pending command is reset in all cases, including on error.
188
207
  pendingCommand = false;
189
208
  }
190
209
  };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const path = require('path');
3
+ const path = require('node:path');
4
4
 
5
5
  const reporterMethods = ['beforeAll', 'begin', 'results', 'error', 'afterAll'];
6
6
  const noop = () => {};
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "pa11y-ci-reporter-runner",
3
- "version": "3.0.1",
3
+ "version": "4.1.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": {
7
- "build-types": "tsc index.js --declaration --allowJs --emitDeclarationOnly",
8
- "hooks-pre-commit": "npm run lint && npm run prettier-check",
9
- "hooks-pre-push": "npm audit --audit-level=critical && npm test",
10
- "lint": "npm run lint-js && npm run lint-md",
11
- "lint-js": "eslint .",
12
- "lint-md": "markdownlint-cli2 \"**/*.md\" \"#node_modules\" \"#Archive\"",
13
- "prettier-check": "prettier --check .",
14
- "prettier-fix": "prettier --write .",
7
+ "build:types": "tsc index.js --declaration --allowJs --emitDeclarationOnly",
8
+ "hooks:pre-commit": "npm run lint && npm run prettier:check",
9
+ "hooks:pre-push": "npm audit --audit-level=critical && npm test",
10
+ "lint": "npm run lint:js && npm run lint:md",
11
+ "lint:js": "eslint .",
12
+ "lint:md": "markdownlint-cli2 \"**/*.md\" \"#node_modules\" \"#Archive\"",
13
+ "prettier:check": "prettier --check .",
14
+ "prettier:fix": "prettier --write .",
15
15
  "test": "jest --ci",
16
- "test-types": "tsd"
16
+ "test:types": "tsd"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",
@@ -29,7 +29,7 @@
29
29
  "author": "Aaron Goldenthal <npm@aarongoldenthal.com>",
30
30
  "license": "MIT",
31
31
  "engines": {
32
- "node": "^16.13.0 || ^18.12.0 || >=20.0.0"
32
+ "node": "^18.12.0 || >=20.0.0"
33
33
  },
34
34
  "files": [
35
35
  "index.d.ts",
@@ -41,18 +41,18 @@
41
41
  },
42
42
  "homepage": "https://gitlab.com/gitlab-ci-utils/pa11y-ci-reporter-runner",
43
43
  "devDependencies": {
44
- "@aarongoldenthal/eslint-config-standard": "^23.1.1",
45
- "eslint": "^8.47.0",
46
- "jest": "^29.6.2",
44
+ "@aarongoldenthal/eslint-config-standard": "^25.0.0",
45
+ "eslint": "^8.56.0",
46
+ "jest": "^29.7.0",
47
47
  "jest-junit": "^16.0.0",
48
- "markdownlint-cli2": "^0.8.1",
48
+ "markdownlint-cli2": "^0.11.0",
49
49
  "pa11y-ci-reporter-cli-summary": "^3.0.0",
50
- "prettier": "^3.0.1",
51
- "tsd": "^0.28.1",
52
- "typescript": "^5.1.6"
50
+ "prettier": "^3.1.1",
51
+ "tsd": "^0.30.3",
52
+ "typescript": "^5.3.3"
53
53
  },
54
54
  "dependencies": {
55
55
  "lodash": "^4.17.21",
56
- "xstate": "^4.38.2"
56
+ "xstate": "^5.5.1"
57
57
  }
58
58
  }