Skip to content

Commit

Permalink
ci: add matrix node test (#4275)
Browse files Browse the repository at this point in the history
* ci: add matrix node test

* 🤖 Automated formatting fixes

* use node orb

* full node range

* fix npm command

* 🤖 Automated formatting fixes

* fix how jsdom is installed

* comments

* drop down support to just 6 + LTS

* use all-rules.html

* read file

* move node test to GHA

* fix gha

* fix upload

* try again?

* pin to v3 for upload/download

* fix prettier action

* output ignore file

* cat not echo

---------

Co-authored-by: straker <straker@users.noreply.github.com>
  • Loading branch information
straker and straker committed Dec 18, 2023
1 parent 5eb867b commit 3658df8
Show file tree
Hide file tree
Showing 8 changed files with 1,209 additions and 1,065 deletions.
13 changes: 7 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ commands:
browser-tools-job:
steps:
- browser-tools/install-browser-tools

jobs:
# Fetch and cache dependencies.
dependencies_unix:
Expand Down Expand Up @@ -240,15 +241,15 @@ jobs:
- <<: *restore_dependency_cache_unix
- run: npm run test:rule-help-version

# Test node API
test_node:
# Test jsdom API
test_jsdom:
<<: *defaults
<<: *unix_box
steps:
- checkout
- <<: *restore_dependency_cache_unix
- <<: *restore_build
- run: npm run test:node
- run: npm run test:jsdom

# Release a "next" version
next_release:
Expand Down Expand Up @@ -360,7 +361,7 @@ workflows:
- test_rule_help_version:
requires:
- build_unix
- test_node:
- test_jsdom:
requires:
- build_unix
# Verify the sri history is correct
Expand All @@ -385,7 +386,7 @@ workflows:
- test_virtual_rules
- build_api_docs
- test_rule_help_version
- test_node
- test_jsdom
- verify_sri
filters:
branches:
Expand All @@ -403,7 +404,7 @@ workflows:
- test_virtual_rules
- build_api_docs
- test_rule_help_version
- test_node
- test_jsdom
filters:
branches:
only: develop
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
cache: 'npm'
# Workflows are not allowed to edit workflows. As result, we need to prevent Prettier from formatting them.
- name: Prevent workflows from being formatted
run: echo ".github" >> .prettierignore
run: echo ".github" >> .prettierignore && cat .prettierignore
- run: npm run fmt
# Prevent the prettierignore change from being committed.
- run: git checkout .prettierignore
Expand Down
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Tests

on:
pull_request:
push:
branches:
- master
- develop

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npm run build
# v4 download seems to have some flakiness with the download of artifacts so pinning to v3 for now
# @see https://github.com/actions/download-artifact/issues/249
- uses: actions/upload-artifact@v3
with:
name: axe-core
path: axe.js
retention-days: 1

test_node:
strategy:
matrix:
node: [6, 18, 20]
runs-on: ubuntu-latest
timeout-minutes: 5
needs: build
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node}}
- uses: actions/download-artifact@v3
with:
name: axe-core
- run: npm run test:node
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules/
doc/api
doc/api
2,112 changes: 1,056 additions & 1,056 deletions locales/el.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"test:locales": "mocha test/test-locales.js",
"test:virtual-rules": "mocha test/test-virtual-rules.js",
"test:rule-help-version": "mocha test/test-rule-help-version.js",
"test:node": "mocha test/node/*.js",
"test:node": "node test/node/node.js",
"test:jsdom": "mocha test/node/jsdom.js",
"version": "echo \"use 'npm run release' to bump axe-core version\" && exit 1",
"release": "git fetch origin --tags --force && standard-version -a",
"rule-gen": "node build/rule-generator",
Expand Down
93 changes: 93 additions & 0 deletions test/node/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// this file is purposefully written without mocha and in es5 syntax in order
// to be compatible with node 4+

var axe = require('../../');
var assert = require('assert');
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');

initJsdom(function (err, window) {
assert.equal(err, null);

console.log('running axe');
axe.run(
window.document.documentElement,
{
preload: false,
rules: { 'color-contrast': { enabled: false } }
},
function (axeError, results) {
assert.equal(axeError, null);
assert.notEqual(results.violations.length, 0);
console.log('axe ran successfully');
}
);
});

/**
* Install a version of jsdom that is compatible with the currently running node
* version and return the jsdom window object.
* @param {Function} callback - callback function when jsdom is installed.
* Is passed any error object and the jsdom window object.
*/
function initJsdom(callback) {
try {
var nodeToJsdomMatrix = {
4: '9.12.0', // last jsdom version that supported this node version
6: '11.12.0',
8: '15.2.1',
10: '16.7.0',
12: '19.0.0',
14: '21.1.2',
16: '22.1.0'
};

var majorNodeVersion = process.versions.node.split('.')[0];
var jsdomVersion = nodeToJsdomMatrix[majorNodeVersion] || 'latest';

console.log('node version detected as: v' + majorNodeVersion);
console.log('installing jsdom@' + jsdomVersion);
var child = spawn(
'npm',
['install', 'jsdom@' + jsdomVersion, '--no-save'],
{
cwd: __dirname
}
);
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', function (data) {
console.log(data);
});
child.stderr.on('data', function (data) {
console.error(data);
});
child.on('close', function () {
console.log('installed');
var jsdom = require('jsdom');
var domStr = fs.readFileSync(
path.join('test', 'integration', 'full', 'all-rules', 'all-rules.html'),
'utf8'
);

// jsdom 9
if (jsdom.env) {
jsdom.env(domStr, function (jsdomError, window) {
if (jsdomError) {
callback(jsdomError);
}

callback(null, window);
});
}
// jsdom 11+
else {
var dom = new jsdom.JSDOM(domStr);
callback(null, dom.window);
}
});
} catch (err) {
callback(err);
}
}
4 changes: 4 additions & 0 deletions test/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "This package.json is intentionally left empty so running the node.js test does not install jsdom at the root level node_modules",
"dependencies": {}
}

0 comments on commit 3658df8

Please sign in to comment.