diff --git a/.circleci/config.yml b/.circleci/config.yml index 8020cabbef..8ed7e05008 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 @@ -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: @@ -385,6 +407,7 @@ workflows: - test_virtual_rules - build_api_docs - test_rule_help_version + - test_jsdom - test_node - verify_sri filters: @@ -403,6 +426,7 @@ workflows: - test_virtual_rules - build_api_docs - test_rule_help_version + - test_jsdom - test_node filters: branches: diff --git a/package.json b/package.json index 878bf3d12c..a0429e55bc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/node/node.js b/test/node/node.js new file mode 100644 index 0000000000..b48422a0a8 --- /dev/null +++ b/test/node/node.js @@ -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 = + '' + + '' + + '' + + '' + + '' + + 'Document' + + '' + + '' + + 'Hello' + + 'Main' + + '' + + '' + + ''; + +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); + } +} diff --git a/test/node/package.json b/test/node/package.json new file mode 100644 index 0000000000..9ce10c5674 --- /dev/null +++ b/test/node/package.json @@ -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": {} +}