Skip to content

Commit

Permalink
test: split eslint e2e (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Nov 19, 2024
1 parent 3fecae9 commit 1353b20
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 29 deletions.
25 changes: 2 additions & 23 deletions e2e/cli-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {
type AuditReport,
type PluginReport,
type Report,
reportSchema,
} from '@code-pushup/models';
import type { AuditReport, PluginReport, Report } from '@code-pushup/models';
import { cleanTestFolder } from '@code-pushup/test-setup';
import { executeProcess, readJsonFile, readTextFile } from '@code-pushup/utils';
import { executeProcess, readTextFile } from '@code-pushup/utils';

describe('CLI collect', () => {
const exampleCategoryTitle = 'Code style';
Expand Down Expand Up @@ -47,22 +42,6 @@ describe('CLI collect', () => {
await cleanTestFolder('tmp/e2e/react-todos-app');
});

it('should run ESLint plugin and create report.json', async () => {
const { code, stderr } = await executeProcess({
command: 'code-pushup',
args: ['collect', '--no-progress', '--onlyPlugins=eslint'],
cwd: 'examples/react-todos-app',
});

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

const report = await readJsonFile('tmp/e2e/react-todos-app/report.json');

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

it('should create report.md', async () => {
const { code, stderr } = await executeProcess({
command: 'code-pushup',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
},
"dependencies": {},
"description": ""
}
12 changes: 12 additions & 0 deletions e2e/plugin-eslint-e2e/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*", "code-pushup.config*.ts"],
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"parserOptions": {
"project": ["e2e/plugin-eslint-e2e/tsconfig.*?.json"]
}
}
]
}
20 changes: 20 additions & 0 deletions e2e/plugin-eslint-e2e/mocks/fixtures/old-version/.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,27 @@
import eslintPlugin from '@code-pushup/eslint-plugin';

export default {
plugins: [
await eslintPlugin({
eslintrc: '.eslintrc.json',
patterns: ['src/*.js'],
}),
],
categories: [
{
slug: 'bug-prevention',
title: 'Bug prevention',
description: 'Lint rules that find **potential bugs** in your code.',
refs: [{ type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 }],
},
{
slug: 'code-style',
title: 'Code style',
description:
'Lint rules that promote **good practices** and consistency in your code.',
refs: [
{ type: 'group', plugin: 'eslint', slug: 'suggestions', weight: 1 },
],
},
],
};
7 changes: 7 additions & 0 deletions e2e/plugin-eslint-e2e/mocks/fixtures/old-version/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function unusedFn() {
return '42';
}

module.exports = function consoleLog() {
console.log('No console.log()!');
};
23 changes: 23 additions & 0 deletions e2e/plugin-eslint-e2e/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "plugin-eslint-e2e",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "e2e/plugin-eslint-e2e/src",
"projectType": "application",
"targets": {
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["e2e/plugin-eslint-e2e/**/*.ts"]
}
},
"e2e": {
"executor": "@nx/vite:test",
"options": {
"configFile": "e2e/plugin-eslint-e2e/vite.config.e2e.ts"
}
}
},
"implicitDependencies": ["cli", "plugin-eslint"],
"tags": ["scope:plugin", "type:e2e"]
}
139 changes: 139 additions & 0 deletions e2e/plugin-eslint-e2e/tests/__snapshots__/collect.e2e.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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`] = `
{
"categories": [
{
"description": "Lint rules that find **potential bugs** in your code.",
"refs": [
{
"plugin": "eslint",
"slug": "problems",
"type": "group",
"weight": 1,
},
],
"slug": "bug-prevention",
"title": "Bug prevention",
},
{
"description": "Lint rules that promote **good practices** and consistency in your code.",
"refs": [
{
"plugin": "eslint",
"slug": "suggestions",
"type": "group",
"weight": 1,
},
],
"slug": "code-style",
"title": "Code style",
},
],
"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/old-version/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/old-version/src/index.js",
"position": {
"endColumn": 14,
"endLine": 6,
"startColumn": 3,
"startLine": 6,
},
},
},
],
},
"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",
},
],
}
`;
48 changes: 48 additions & 0 deletions e2e/plugin-eslint-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { cp } from 'node:fs/promises';
import { join } from 'node:path';
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';
import { type Report, reportSchema } from '@code-pushup/models';
import { teardownTestFolder } from '@code-pushup/test-setup';
import { omitVariableReportData } from '@code-pushup/test-utils';
import { executeProcess, readJsonFile } from '@code-pushup/utils';

describe('collect report with eslint-plugin NPM package', () => {
const fixturesOldVersionDir = join(
'e2e',
'plugin-eslint-e2e',
'mocks',
'fixtures',
'old-version',
);
const envRoot = join('tmp', 'e2e', 'plugin-eslint-e2e');
const oldVersionDir = join(envRoot, 'old-version');
const oldVersionOutputDir = join(oldVersionDir, '.code-pushup');

beforeAll(async () => {
await cp(fixturesOldVersionDir, oldVersionDir, { recursive: true });
});

afterAll(async () => {
await teardownTestFolder(oldVersionDir);
});

afterEach(async () => {
await teardownTestFolder(oldVersionOutputDir);
});

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

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

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

expect(() => reportSchema.parse(report)).not.toThrow();
expect(omitVariableReportData(report as Report)).toMatchSnapshot();
});
});
20 changes: 20 additions & 0 deletions e2e/plugin-eslint-e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"types": ["vitest"]
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.test.json"
}
]
}
15 changes: 15 additions & 0 deletions e2e/plugin-eslint-e2e/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"],
"target": "ES2020"
},
"exclude": ["__test-env__/**"],
"include": [
"vite.config.e2e.ts",
"tests/**/*.e2e.test.ts",
"tests/**/*.d.ts",
"mocks/**/*.ts"
]
}
21 changes: 21 additions & 0 deletions e2e/plugin-eslint-e2e/vite.config.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import { tsconfigPathAliases } from '../../tools/vitest-tsconfig-path-aliases';

export default defineConfig({
cacheDir: '../../node_modules/.vite/plugin-lighthouse-e2e',
test: {
reporters: ['basic'],
testTimeout: 120_000,
globals: true,
alias: tsconfigPathAliases(),
pool: 'threads',
poolOptions: { threads: { singleThread: true } },
cache: {
dir: '../../node_modules/.vitest',
},
environment: 'node',
include: ['tests/**/*.e2e.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
setupFiles: ['../../testing/test-setup/src/lib/reset.mocks.ts'],
},
});
4 changes: 0 additions & 4 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
"e2e": {
"dependsOn": ["^build"]
},
"nxv-e2e": {},
"nxv-env-setup": {
"executor": "@push-based/nx-verdaccio:env-setup"
},
"@nx/vite:test": {
"cache": true,
"inputs": ["default", "^production"],
Expand Down

0 comments on commit 1353b20

Please sign in to comment.