Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Reimplement the Unit Test "requires" #248

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module.exports = {
extends: ['./configs/essentials', './configs/node'].map(require.resolve),
extends: ['./configs/essentials', './configs/node', 'prettier'],

overrides: [
{
files: ['*.test.js'],
extends: ['./rules/jest'],
},
],
};
16 changes: 16 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"devDependencies": {
"eslint": "^7.32.0 || ^8.2.0",
"eslint-config-prettier": "^9.1.0",
"jest": "^29.5.0",
"prettier": "^2.8.7",
"release-it": "^15.10.1",
Expand Down
4 changes: 0 additions & 4 deletions rules/index.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this file as it's no longer used.
The reimplemented Unit Test revealed that it was unnecessary.

This file was deleted.

4 changes: 0 additions & 4 deletions rules/prettier.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this file as it's no longer used.
The reimplemented Unit Test revealed that it was unnecessary.

This file was deleted.

60 changes: 40 additions & 20 deletions tests/requires.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
const fs = require('fs');
const path = require('path');

const readdirRecursively = (dir) => {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
let files = [];
let dirs = [];
dirents.forEach((dirent) => {
if (dirent.isDirectory()) dirs.push(`${dir}/${dirent.name}`);
if (dirent.isFile()) files.push(`${dir}/${dirent.name}`);
});
dirs.forEach((dir) => {
files = readdirRecursively(dir, files);
});
return files;
};
/* eslint-disable import/no-dynamic-require */
/* eslint-disable n/global-require */
const { readdirSync } = require('fs');
Comment on lines +1 to +2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test requires dynamic require, so disable the rules for this.

const { resolve, join } = require('path');

test('parsable all configs', () => {
const files = readdirRecursively(path.resolve(`${__dirname}/../configs`));
files.forEach((file) => expect(() => require(file)).not.toThrow());
const dir = resolve(join(__dirname, '../configs'));

const files = readdirSync(dir, {
withFileTypes: true,
})
.map((dirnet) => extractFilePath(dirnet, dir))
.flat();

files.forEach((file) => {
expect(() => require(file)).not.toThrow();
});
});

test('parsable all rules', () => {
const files = readdirRecursively(path.resolve(`${__dirname}/../rules`));
files.forEach((file) => expect(() => require(file)).not.toThrow());
const dir = resolve(join(__dirname, '../rules'));

const files = readdirSync(dir, {
withFileTypes: true,
})
.map((dirnet) => extractFilePath(dirnet, dir))
.flat();

files.forEach((file) => {
expect(() => require(file)).not.toThrow();
});
});

function extractFilePath(dirent, dir) {
if (dirent.isFile()) {
return `${dir}/${dirent.name}`;
}

if (dirent.isDirectory()) {
return readdirSync(`${dir}/${dirent.name}`, {
withFileTypes: true,
})
.map((child) => extractFilePath(child, `${dir}/${dirent.name}`))
.flat();
}
}
Loading