Skip to content

Commit

Permalink
First draft test modernization
Browse files Browse the repository at this point in the history
(They still fail)
  • Loading branch information
osmestad committed Sep 7, 2023
1 parent 0cbdca5 commit 1b5983d
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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<any> | 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',
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -97,3 +112,4 @@ test('Success on legacy config', async (t) => {
t.is(errors.length, 0);
});
*/

0 comments on commit 1b5983d

Please sign in to comment.