diff --git a/test/test.js b/test/test.js index 7820783..5195d93 100644 --- a/test/test.js +++ b/test/test.js @@ -1,36 +1,50 @@ +import path from 'path'; +import { fileURLToPath } from 'url'; + // Yes, I realise the irony of ignoring the linter in a linting package. // https://github.com/avajs/ava/issues/3088 // eslint-disable-next-line import/no-unresolved -const test = require('ava'); -const tempWrite = require('temp-write'); -const isPlainObj = require('is-plain-obj'); -const { ESLint } = require('eslint'); -const path = require('path'); - -const baseConf = require('../index.js'); -const legacyConf = require('../legacy.js'); - -const getUniqueValues = (arr) => ([...new Set(arr)]); - +import test from 'ava'; +import { ESLint } from 'eslint'; +//import isPlainObj from 'is-plain-obj'; +import tempWrite from 'temp-write'; + +import baseConf from '../index.js'; +//import legacyConf from '../legacy.js'; + +// mimic CommonJS variables -- not needed if using CommonJS +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const getUniqueValues = ( + /** @type {Iterable | null | undefined} */ arr, +) => [...new Set(arr)]; + +/** + * @param {string} file + * @param {any[]} conf + */ async function runEslint(file, conf) { const linter = new ESLint({ - overrideConfigFile: tempWrite.sync(JSON.stringify(conf)), ignore: false, - useEslintrc: false, + overrideConfigFile: tempWrite.sync(JSON.stringify(conf)), resolvePluginsRelativeTo: path.join(__dirname, '..'), + useEslintrc: false, }); const results = await linter.lintFiles([file]); return results[0].messages; } -test('Fails on base config', async (t) => { +test('Fails on base config', async t => { const conf = baseConf; - t.true(isPlainObj(conf)); - t.true(isPlainObj(conf.rules)); + //t.true(isPlainObj(conf)); + //t.true(isPlainObj(conf.rules)); const errors = await runEslint('test/cases/ugly-javascript.js', conf); - const errorsAsRuleIds = getUniqueValues(errors.map((item) => item.ruleId)); + const errorsAsRuleIds = getUniqueValues( + errors.map((/** @type {{ ruleId: any; }} */ item) => item.ruleId), + ); const expectedErrors = [ 'strict', @@ -41,23 +55,24 @@ test('Fails on base config', async (t) => { 'space-before-blocks', ]; - // eslint-disable-next-line max-len - const expectedErrorsFound = errorsAsRuleIds.filter((error) => expectedErrors.indexOf(error) !== -1); + const expectedErrorsFound = errorsAsRuleIds.filter( + error => expectedErrors.indexOf(error) !== -1, + ); t.is(expectedErrorsFound.length, expectedErrors.length); }); -test('Success on base config', async (t) => { +test('Success on base config', async t => { const conf = baseConf; - t.true(isPlainObj(conf)); - t.true(isPlainObj(conf.rules)); + //t.true(isPlainObj(conf)); + //t.true(isPlainObj(conf.rules)); const errors = await runEslint('test/cases/nice-javascript.js', conf); t.is(errors.length, 0); }); - +/* test('Fails on legacy config', async (t) => { const conf = legacyConf; @@ -81,7 +96,7 @@ test('Fails on legacy config', async (t) => { 'no-console', ]; - // eslint-disable-next-line max-len + const expectedErrorsFound = errorsAsRuleIds.filter((error) => expectedErrors.indexOf(error) !== -1); t.is(expectedErrorsFound.length, expectedErrors.length); @@ -97,3 +112,4 @@ test('Success on legacy config', async (t) => { t.is(errors.length, 0); }); +*/