Skip to content

feat: add tests #8

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

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { recommended } from "./src/index.js";

export default [
{
ignores: [
"test/_fixtures",
"test/fixtures/input"
],
},
...recommended
];
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"scripts": {
"debug": "eslint --debug ./src/index.js",
"inspect": "eslint --inspect-config",
"test": "eslint ./src",
"test:fix": "eslint --fix ./src"
"test": "vitest"
},
"dependencies": {
"@eslint-react/eslint-plugin": "^1.38.0",
Expand Down Expand Up @@ -52,6 +51,12 @@
"neostandard": "^0.12.1",
"typescript-eslint": "^8.28.0"
},
"devDependencies": {
"execa": "^9.5.2",
"fast-glob": "^3.3.3",
"fs-extra": "^11.3.0",
"vitest": "^3.0.5"
},
"peerDependencies": {
"eslint": "^9.17.0"
}
Expand Down
2,310 changes: 1,589 additions & 721 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions test/fixtures.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { join, resolve } from "node:path";

import { execa } from "execa";
import fg from "fast-glob";
import fs from "fs-extra";
import { afterAll, beforeAll, it } from "vitest";

// Antes de ejecutar las pruebas, elimina el directorio temporal `_fixtures` si existe
beforeAll(async () => {
await fs.rm("test/_fixtures", { recursive: true, force: true });
});

// Después de ejecutar las pruebas, elimina nuevamente el directorio temporal `_fixtures`
afterAll(async () => {
await fs.rm("test/_fixtures", { recursive: true, force: true });
});

// Lista de configuraciones ESLint que se probarán
const configs = [
{ name: "basic", configPath: "src/flat/basic.js" },
{ name: "recommended", configPath: "src/flat/recommended.js" },
];

configs.forEach(({ name, configPath }) => {
runWithConfig(name, configPath);
});

// Función que define una prueba concurrente para una configuración específica
function runWithConfig(name, configPath) {
it.concurrent(name, async ({ expect }) => {
// Define las rutas necesarias para la prueba
const from = resolve("test/fixtures/input");
const output = resolve("test/fixtures/output", name);
const temp = resolve("test/_fixtures", name);

// Copia los archivos de entrada a la carpeta temporal, excluyendo `node_modules`
await fs.copy(from, temp, {
filter: (src) => !src.includes("node_modules"),
});

// Crea un archivo de configuración ESLint dinámico en la carpeta temporal
await fs.writeFile(join(temp, "eslint.config.js"), `
import config from '${resolve(configPath)}';
export default config;
`);

// Verifica que el archivo de configuración se ha creado correctamente
const eslintConfigPath = join(temp, "eslint.config.js");
if (!await fs.pathExists(eslintConfigPath)) {
throw new Error(`No se pudo crear el archivo de configuración ESLint en ${eslintConfigPath}`);
}

// Ejecuta ESLint con la configuración generada y aplica correcciones automáticas
await execa("npx", ["eslint", "--fix", "."], {
cwd: temp,
stdio: "pipe",
});

// Busca todos los archivos en la carpeta temporal, excluyendo ciertos patrones
const files = await fg("**/*", {
ignore: ["node_modules", "eslint.config.js", "test/_fixtures", "test/fixtures/input"],
cwd: temp,
});

// Compara el contenido de los archivos procesados con los archivos de entrada
await Promise.all(files.map(async (file) => {
const content = await fs.readFile(join(temp, file), "utf-8");
const source = await fs.readFile(join(from, file), "utf-8");
const outputPath = join(output, file);

// Si el contenido no cambió, elimina el archivo de salida si existe
if (content === source) {
if (fs.existsSync(outputPath)) await fs.remove(outputPath);
return;
}

// Si el contenido cambió, verifica que coincida con el archivo de snapshot esperado
await expect.soft(content).toMatchFileSnapshot(join(output, file));
}));
}, 30000);
}
72 changes: 72 additions & 0 deletions test/fixtures/input/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is generated by ChatGPT

// eslint-disable-next-line no-console
var log = console.log

// Define a class using ES6 class syntax
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}

// Define a method within the class
sayHello() {
log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}

// Create an array of objects
const people = [
new Person('Alice', 30),
new Person('Bob', 25),
new Person('Charlie', 35)
];

// Use the forEach method to iterate over the array
people.forEach(person => {
person.sayHello();
});

// Use a template literal to create a multiline string
const multilineString = `
This is a multiline string
that spans multiple lines.
`;

// Use destructuring assignment to extract values from an object
const { name, age } = people[0];
log(`First person in the array is ${name} and they are ${age} years old.`, multilineString);

// Use the spread operator to create a new array
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
log(newNumbers);

// Use a try-catch block for error handling
try {
// Attempt to parse an invalid JSON string
JSON.parse('invalid JSON');
} catch (error) {
console.error('Error parsing JSON:', error.message);
}

// Use a ternary conditional operator
const isEven = num => num % 2 === 0;
const number = 7;
log(`${number} is ${isEven(number) ? 'even' : 'odd'}.`);

// Use a callback function with setTimeout for asynchronous code
setTimeout(() => {
log('This code runs after a delay of 2 seconds.');
}, 2000);

let a, b, c, d, foo

if (a
|| b
|| c || d
|| (d && b)
) {
foo()
}
72 changes: 72 additions & 0 deletions test/fixtures/output/basic/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is generated by ChatGPT


const log = console.log

// Define a class using ES6 class syntax
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}

// Define a method within the class
sayHello() {
log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}

// Create an array of objects
const people = [
new Person('Alice', 30),
new Person('Bob', 25),
new Person('Charlie', 35)
];

// Use the forEach method to iterate over the array
people.forEach(person => {
person.sayHello();
});

// Use a template literal to create a multiline string
const multilineString = `
This is a multiline string
that spans multiple lines.
`;

// Use destructuring assignment to extract values from an object
const { name, age } = people[0];
log(`First person in the array is ${name} and they are ${age} years old.`, multilineString);

// Use the spread operator to create a new array
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
log(newNumbers);

// Use a try-catch block for error handling
try {
// Attempt to parse an invalid JSON string
JSON.parse('invalid JSON');
} catch (error) {
console.error('Error parsing JSON:', error.message);
}

// Use a ternary conditional operator
const isEven = num => num % 2 === 0;
const number = 7;
log(`${number} is ${isEven(number) ? 'even' : 'odd'}.`);

// Use a callback function with setTimeout for asynchronous code
setTimeout(() => {
log('This code runs after a delay of 2 seconds.');
}, 2000);

let a, b, c, d, foo

if (a
|| b
|| c || d
|| (d && b)
) {
foo()
}
71 changes: 71 additions & 0 deletions test/fixtures/output/recommended/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// This file is generated by ChatGPT

const log = console.log;

// Define a class using ES6 class syntax
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}

// Define a method within the class
sayHello() {
log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}

// Create an array of objects
const people = [
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 35)
];

// Use the forEach method to iterate over the array
people.forEach(person => {
person.sayHello();
});

// Use a template literal to create a multiline string
const multilineString = `
This is a multiline string
that spans multiple lines.
`;

// Use destructuring assignment to extract values from an object
const { name, age } = people[0];
log(`First person in the array is ${name} and they are ${age} years old.`, multilineString);

// Use the spread operator to create a new array
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
log(newNumbers);

// Use a try-catch block for error handling
try {
// Attempt to parse an invalid JSON string
JSON.parse("invalid JSON");
} catch (error) {
console.error("Error parsing JSON:", error.message);
}

// Use a ternary conditional operator
const isEven = num => num % 2 === 0;
const number = 7;
log(`${number} is ${isEven(number) ? "even" : "odd"}.`);

// Use a callback function with setTimeout for asynchronous code
setTimeout(() => {
log("This code runs after a delay of 2 seconds.");
}, 2000);

let a, b, c, d, foo;

if (a ||
b ||
c || d ||
(d && b)
) {
foo();
}