Skip to content

Commit

Permalink
ci: add matrix node test
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Dec 15, 2023
1 parent f7b8d5e commit a3f9533
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
26 changes: 25 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,32 @@ jobs:
- <<: *restore_dependency_cache_unix
- run: npm run test:rule-help-version

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

# Test node versions
test_node:
parameters:
node-version:
type: string
<<: *defaults
<<: *unix_box
steps:
- checkout
- <<: *restore_dependency_cache_unix
- <<: *restore_build
- node/install:
node-version: << parameters.node-version >>
- run: npm run test:node


# Release a "next" version
next_release:
<<: *defaults
Expand Down Expand Up @@ -360,9 +376,15 @@ workflows:
- test_rule_help_version:
requires:
- build_unix
- test_jsdom:
requires:
- build_unix
- test_node:
requires:
- build_unix
matrix:
parameters:
node-version: ["16.9.0", "18.17.1"]
# Verify the sri history is correct
- verify_sri:
requires:
Expand All @@ -385,6 +407,7 @@ workflows:
- test_virtual_rules
- build_api_docs
- test_rule_help_version
- test_jsdom
- test_node
- verify_sri
filters:
Expand All @@ -403,6 +426,7 @@ workflows:
- test_virtual_rules
- build_api_docs
- test_rule_help_version
- test_jsdom
- test_node
filters:
branches:
Expand Down
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": "mocha 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
79 changes: 79 additions & 0 deletions test/node/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// this file is purposefully written without mocha and in non-es6 syntax in order
// to be compatible with node 4+

var axe = require('../../');
var assert = require('assert');
var exec = require('child_process').exec;

var domStr =
'<!DOCTYPE html>' +
'<html lang="en">' +
'<head>' +
'<meta charset="UTF-8">' +
'<meta name="viewport" content="width=device-width, initial-scale=1.0">' +
'<title>Document</title>' +
'</head>' +
'<body>' +
'Hello' +
'<a id="hash-link" href="#main">Main</a>' +
'<a id="skip" href="https://page.com#main">Skip Link</a>' +
'</body>' +
'</html>';

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

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

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';

exec('npm install jsdom@' + jsdomVersion, function (installError) {
if (installError) {
callback(installError);
}

var jsdom = require('jsdom');

// 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 a3f9533

Please sign in to comment.