Skip to content

Commit

Permalink
test: fix node v20 tests (dequelabs#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdnoC committed Aug 22, 2023
1 parent 6824caf commit d40ec13
Show file tree
Hide file tree
Showing 7 changed files with 684 additions and 2,459 deletions.
95 changes: 30 additions & 65 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@axe-core/webdriverjs": "^4.7.3",
"axe-core": "^4.7.0",
"chromedriver": "^113.0.0",
"chromedriver": "^115.0.1",
"colors": "^1.4.0",
"commander": "^9.4.1",
"selenium-webdriver": "^4.8.1"
Expand All @@ -55,15 +55,13 @@
"@types/chai": "^4.3.3",
"@types/chromedriver": "^81.0.1",
"@types/mocha": "^10.0.0",
"@types/mock-fs": "^4.13.1",
"@types/selenium-webdriver": "^4.1.5",
"chai": "^4.3.6",
"execa": "5.1.0",
"mocha": "^10.0.0",
"mock-fs": "^5.1.4",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"tempy": "1.0.0",
"tempy": "^1.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
Expand Down
60 changes: 45 additions & 15 deletions packages/cli/src/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'mocha';
import { assert } from 'chai';
import mock = require('mock-fs');
import tempy from 'tempy';
import { join } from 'path';
import { mkdirSync, writeFileSync, rmSync } from 'fs';
import { dependencies } from '../../package.json';
import * as utils from './utils';

Expand Down Expand Up @@ -74,24 +76,52 @@ describe('utils', () => {

describe('getAxeSource', () => {
describe('mock file', () => {
beforeEach(() => {
mock({
'/node_modules/axe-core': {},
'../node_modules/axe-core': {
'axe.js': mock.load(require.resolve('axe-core'))
}
});
});
function setupTree() {
const tempDir = tempy.directory();
const parentDirname = join(tempDir, 'node_modules', 'axe-core');
mkdirSync(parentDirname, { recursive: true });
writeFileSync(join(parentDirname, 'axe.js'), 'parent');

const cliDirname = join(tempDir, 'packages', 'cli');
const nodeModDirname = join(
cliDirname,
'node_modules',
'axe-core'
);
mkdirSync(nodeModDirname, { recursive: true });
writeFileSync(join(nodeModDirname, 'axe.js'), 'node modules');

const cwdDirname = join(tempDir, 'packages', 'cli', 'lib');
mkdirSync(cwdDirname);
writeFileSync(join(cwdDirname, 'axe.js'), 'cwd');
return {
cliDirname,
parentDirname,
nodeModDirname,
cwdDirname
};
}

afterEach(() => {
mock.restore();
it('uses axe.js from the working directory if it exists', () => {
const { cwdDirname } = setupTree();
const axeSource = utils.getAxeSource(undefined, cwdDirname);
assert.include(axeSource, 'cwd');
});
it('fall back to use `locally` installed axe-core', () => {
const axeSource = utils.getAxeSource();
const axeVersionCheck = dependencies['axe-core'].replace('^', '');
assert.include(axeSource, axeVersionCheck);
it("falls back to axe-core from the working directory's node_modules if axe.js doesn't exist in the working directory", () => {
const { cliDirname, cwdDirname } = setupTree();
rmSync(join(cwdDirname, 'axe.js'));
const axeSource = utils.getAxeSource(undefined, cliDirname);
assert.include(axeSource, 'node modules');
});
it("falls back to axe-core from our own package's node_modules if no working-directory based implementation exists", () => {
const { cwdDirname, nodeModDirname } = setupTree();
rmSync(join(cwdDirname, 'axe.js'));
rmSync(join(nodeModDirname, 'axe.js'));
const axeSource = utils.getAxeSource(undefined, cwdDirname);
assert.include(axeSource, 'parent');
});
});

it('given no axe source use local source', () => {
const axeSource = utils.getAxeSource();
assert.isNotNull(axeSource);
Expand Down
20 changes: 16 additions & 4 deletions packages/cli/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,40 @@ export const parseBrowser = (browser?: string): string | Error => {
}
};

export const getAxeSource = (axePath?: string): string | void => {
export const getAxeSource = (
axePath?: string,
dirname?: string
): string | void => {
// Abort if axePath should exist, and it isn't
if (axePath && !fs.existsSync(axePath)) {
return;
}

let cwd = dirname;
if (!cwd) {
cwd = process.cwd();
}

if (!dirname) {
dirname = __dirname;
}

// Look for axe in current working directory
if (!axePath) {
axePath = path.join(process.cwd(), 'axe.js');
axePath = path.join(cwd, 'axe.js');
}

if (!fs.existsSync(axePath)) {
// Look for axe in CWD ./node_modules
axePath = path.join(process.cwd(), 'node_modules', 'axe-core', 'axe.js');
axePath = path.join(cwd, 'node_modules', 'axe-core', 'axe.js');
}

if (!fs.existsSync(axePath)) {
// `__dirname` is /@axe-core/cli/dist/src/lib when installed globally
// to access the locally installed axe-core package we need to go up 3 levels
// if all else fails, use the locally installed axe
axePath = path.join(
__dirname,
dirname,
'..',
'..',
'..',
Expand Down
Loading

0 comments on commit d40ec13

Please sign in to comment.