Skip to content

Commit

Permalink
Minor refactoring for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 8, 2021
1 parent b958745 commit 724f4d7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 89 deletions.
75 changes: 75 additions & 0 deletions test/utils/parsers.mjs
@@ -0,0 +1,75 @@
import {createRequire} from 'node:module';
import defaultParserOptions from './default-parser-options.mjs';

const require = createRequire(import.meta.url);

const babel = {
name: '@babel/eslint-parser',
get parser() {
return require.resolve(this.name);
},
mergeParserOptions(options) {
options = options || {};
options.babelOptions = options.babelOptions || {};
options.babelOptions.parserOpts = options.babelOptions.parserOpts || {};

let babelPlugins = options.babelOptions.parserOpts.plugins || [];
babelPlugins = [
['estree', {classFeatures: true}],
'jsx',
'exportDefaultFrom',
...babelPlugins,
];

return {
...defaultParserOptions,
requireConfigFile: false,
sourceType: 'module',
allowImportExportEverywhere: true,
...options,
babelOptions: {
babelrc: false,
configFile: false,
...options.babelOptions,
parserOpts: {
...options.babelOptions.parserOpts,
plugins: babelPlugins,
},
},
};
},
};

const typescript = {
name: '@typescript-eslint/parser',
get parser() {
return require.resolve(this.name);
},
mergeParserOptions(options) {
return {
...defaultParserOptions,
...options,
};
},
};

const vue = {
name: 'vue-eslint-parser',
get parser() {
return require.resolve(this.name);
},
mergeParserOptions(options) {
return {
...defaultParserOptions,
...options,
};
},
};

const parsers = {
babel,
typescript,
vue,
};

export default parsers;
136 changes: 47 additions & 89 deletions test/utils/test.mjs
@@ -1,13 +1,11 @@
import path from 'node:path';
import url from 'node:url';
import {createRequire} from 'node:module';
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {loadRule} from '../../rules/utils/rule.js';
import SnapshotRuleTester from './snapshot-rule-tester.mjs';
import defaultParserOptions from './default-parser-options.mjs';

const require = createRequire(import.meta.url);
import * as parsers from './parsers.mjs';

function normalizeInvalidTest(test, rule) {
const {code, output, errors} = test;
Expand All @@ -31,18 +29,6 @@ function normalizeInvalidTest(test, rule) {
};
}

const parsers = {
get typescript() {
return require.resolve('@typescript-eslint/parser');
},
get babel() {
return require.resolve('@babel/eslint-parser');
},
get vue() {
return require.resolve('vue-eslint-parser');
},
};

class Tester {
constructor(ruleId) {
this.ruleId = ruleId;
Expand Down Expand Up @@ -70,76 +56,34 @@ class Tester {
);
}

typescript(tests) {
const {testerOptions = {}} = tests;
testerOptions.parserOptions = testerOptions.parserOptions || {};

return this.runTest({
...tests,
testerOptions: {
...testerOptions,
parser: parsers.typescript,
parserOptions: {
...defaultParserOptions,
...testerOptions.parserOptions,
},
},
});
}

babel(tests) {
const {testerOptions = {}} = tests;
testerOptions.parserOptions = testerOptions.parserOptions || {};
testerOptions.parserOptions.babelOptions = testerOptions.parserOptions.babelOptions || {};
testerOptions.parserOptions.babelOptions.parserOpts = testerOptions.parserOptions.babelOptions.parserOpts || {};
let babelPlugins = testerOptions.parserOptions.babelOptions.parserOpts.plugins || [];
babelPlugins = [
['estree', {classFeatures: true}],
'jsx',
'exportDefaultFrom',
...babelPlugins,
];

return this.runTest({
...tests,
testerOptions: {
...testerOptions,
parser: parsers.babel,
parserOptions: {
...defaultParserOptions,
requireConfigFile: false,
sourceType: 'module',
allowImportExportEverywhere: true,
...testerOptions.parserOptions,
babelOptions: {
babelrc: false,
configFile: false,
...testerOptions.parserOptions.babelOptions,
parserOpts: {
...testerOptions.parserOptions.babelOptions.parserOpts,
plugins: babelPlugins,
},
},
},
},
});
}

vue(tests) {
return this.runTest({
...tests,
testerOptions: {
parser: parsers.vue,
parserOptions: defaultParserOptions,
},
});
}

snapshot(tests) {
const {testerOptions, valid, invalid} = tests;
const {
testerOptions = {},
valid,
invalid,
} = tests;

const {
parser,
parserOptions,
} = testerOptions;

if (parser) {
if (parser.name) {
testerOptions.parser = parser.name;
}

if (parser.mergeParserOptions) {
testerOptions.parserOptions = parser.mergeParserOptions(parserOptions);
}
}

const tester = new SnapshotRuleTester(test, {
parserOptions: defaultParserOptions,
...testerOptions,
parserOptions: {
...defaultParserOptions,
...testerOptions.parserOptions,
},
});
return tester.run(this.ruleId, this.rule, {valid, invalid});
}
Expand All @@ -149,16 +93,31 @@ function getTester(importMeta) {
const filename = url.fileURLToPath(importMeta.url);
const ruleId = path.basename(filename, '.mjs');
const tester = new Tester(ruleId);
const test = Tester.prototype.runTest.bind(tester);
test.typescript = Tester.prototype.typescript.bind(tester);
test.babel = Tester.prototype.babel.bind(tester);
test.vue = Tester.prototype.vue.bind(tester);
test.snapshot = Tester.prototype.snapshot.bind(tester);
const runTest = Tester.prototype.runTest.bind(tester);
runTest.snapshot = Tester.prototype.snapshot.bind(tester);

for (const [parserName, parserSettings] of Object.entries(parsers)) {
Reflect.defineProperty(runTest, parserName, {
value(tests) {
const testerOptions = tests.testerOptions || {};
const {parser, mergeParserOptions} = parserSettings;

return runTest({
...tests,
testerOptions: {
...testerOptions,
parser,
parserOptions: mergeParserOptions(testerOptions.parserOptions),
},
});
},
});
}

return {
ruleId,
rule: tester.rule,
test,
test: runTest,
};
}

Expand Down Expand Up @@ -189,7 +148,6 @@ const avoidTestTitleConflict = (tests, comment) => {
};

export {
defaultParserOptions,
getTester,
avoidTestTitleConflict,
parsers,
Expand Down

0 comments on commit 724f4d7

Please sign in to comment.