Skip to content

Commit

Permalink
test(plugin-eslint-e2e): add e2e test for legacy config
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Nov 23, 2024
1 parent e935280 commit abbd764
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 9 deletions.
20 changes: 20 additions & 0 deletions e2e/plugin-eslint-e2e/mocks/fixtures/legacy-config/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"root": true,
"ignorePatterns": ["code-pushup.config.ts"],
"overrides": [
{
"files": ["*.js"],
"env": {
"node": true
},
"parserOptions": {
"sourceType": "script"
},
"rules": {
"no-unused-vars": "error",
"no-console": "warn",
"no-undef": "error"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import eslintPlugin from '@code-pushup/eslint-plugin';

export default {
plugins: [
await eslintPlugin({
eslintrc: '.eslintrc.json',
patterns: ['src/*.js'],
}),
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function unusedFn() {
return '42';
}
module.exports = function consoleLog() {
console.log('No console.log()!');
};
112 changes: 111 additions & 1 deletion e2e/plugin-eslint-e2e/tests/__snapshots__/collect.e2e.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`collect report with eslint-plugin NPM package > should run ESLint plugin and create report.json 1`] = `
exports[`collect report with eslint-plugin NPM package > should run ESLint plugin for flat config and create report.json 1`] = `
{
"packageName": "@code-pushup/core",
"plugins": [
Expand Down Expand Up @@ -115,3 +115,113 @@ Custom options:
],
}
`;
exports[`collect report with eslint-plugin NPM package > should run ESLint plugin for legacy config and create report.json 1`] = `
{
"packageName": "@code-pushup/core",
"plugins": [
{
"audits": [
{
"description": "ESLint rule **no-unused-vars**.",
"details": {
"issues": [
{
"message": "'unusedFn' is defined but never used.",
"severity": "error",
"source": {
"file": "tmp/e2e/plugin-eslint-e2e/legacy-config/src/index.js",
"position": {
"endColumn": 18,
"endLine": 1,
"startColumn": 10,
"startLine": 1,
},
},
},
],
},
"displayValue": "1 error",
"docsUrl": "https://eslint.org/docs/latest/rules/no-unused-vars",
"score": 0,
"slug": "no-unused-vars",
"title": "Disallow unused variables",
"value": 1,
},
{
"description": "ESLint rule **no-console**.",
"details": {
"issues": [
{
"message": "Unexpected console statement.",
"severity": "warning",
"source": {
"file": "tmp/e2e/plugin-eslint-e2e/legacy-config/src/index.js",
"position": {
"endColumn": 14,
"endLine": 5,
"startColumn": 3,
"startLine": 5,
},
},
},
],
},
"displayValue": "1 warning",
"docsUrl": "https://eslint.org/docs/latest/rules/no-console",
"score": 0,
"slug": "no-console",
"title": "Disallow the use of \`console\`",
"value": 1,
},
{
"description": "ESLint rule **no-undef**.",
"details": {
"issues": [],
},
"displayValue": "passed",
"docsUrl": "https://eslint.org/docs/latest/rules/no-undef",
"score": 1,
"slug": "no-undef",
"title": "Disallow the use of undeclared variables unless mentioned in \`/*global */\` comments",
"value": 0,
},
],
"description": "Official Code PushUp ESLint plugin",
"docsUrl": "https://www.npmjs.com/package/@code-pushup/eslint-plugin",
"groups": [
{
"description": "Code that either will cause an error or may cause confusing behavior. Developers should consider this a high priority to resolve.",
"refs": [
{
"slug": "no-unused-vars",
"weight": 1,
},
{
"slug": "no-undef",
"weight": 1,
},
],
"slug": "problems",
"title": "Problems",
},
{
"description": "Something that could be done in a better way but no errors will occur if the code isn't changed.",
"refs": [
{
"slug": "no-console",
"weight": 1,
},
],
"slug": "suggestions",
"title": "Suggestions",
},
],
"icon": "eslint",
"packageName": "@code-pushup/eslint-plugin",
"slug": "eslint",
"title": "ESLint",
},
],
}
`;
37 changes: 29 additions & 8 deletions e2e/plugin-eslint-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@ import { omitVariableReportData } from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';

describe('collect report with eslint-plugin NPM package', () => {
const fixturesFlatConfigDir = join(
'e2e',
'plugin-eslint-e2e',
'mocks',
'fixtures',
'flat-config',
);
const fixturesDir = join('e2e', 'plugin-eslint-e2e', 'mocks', 'fixtures');
const fixturesFlatConfigDir = join(fixturesDir, 'flat-config');
const fixturesLegacyConfigDir = join(fixturesDir, 'legacy-config');

const envRoot = join('tmp', 'e2e', 'plugin-eslint-e2e');
const flatConfigDir = join(envRoot, 'flat-config');
const legacyConfigDir = join(envRoot, 'legacy-config');
const flatConfigOutputDir = join(flatConfigDir, '.code-pushup');
const legacyConfigOutputDir = join(legacyConfigDir, '.code-pushup');

beforeAll(async () => {
await cp(fixturesFlatConfigDir, flatConfigDir, { recursive: true });
await cp(fixturesLegacyConfigDir, legacyConfigDir, { recursive: true });
});

afterAll(async () => {
await teardownTestFolder(flatConfigDir);
await teardownTestFolder(legacyConfigDir);
});

afterEach(async () => {
await teardownTestFolder(flatConfigOutputDir);
await teardownTestFolder(legacyConfigOutputDir);
});

it('should run ESLint plugin and create report.json', async () => {
it('should run ESLint plugin for flat config and create report.json', async () => {
const { code, stderr } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', 'collect', '--no-progress'],
Expand All @@ -45,4 +47,23 @@ describe('collect report with eslint-plugin NPM package', () => {
expect(() => reportSchema.parse(report)).not.toThrow();
expect(omitVariableReportData(report as Report)).toMatchSnapshot();
});

it('should run ESLint plugin for legacy config and create report.json', async () => {
const { code, stderr } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', 'collect', '--no-progress'],
cwd: legacyConfigDir,
env: { ...process.env, ESLINT_USE_FLAT_CONFIG: 'false' },
});

expect(code).toBe(0);
expect(stderr).toBe('');

const report = await readJsonFile(
join(legacyConfigOutputDir, 'report.json'),
);

expect(() => reportSchema.parse(report)).not.toThrow();
expect(omitVariableReportData(report as Report)).toMatchSnapshot();
});
});

0 comments on commit abbd764

Please sign in to comment.