Skip to content

Commit

Permalink
chore: remove ESLint issues from test files in core, plugin-eslint an…
Browse files Browse the repository at this point in the history
…d utils (#403)

I went through the report and removed most ESLint related issues in test
files and enabled rules where possible.
  • Loading branch information
Tlacenka authored Jan 10, 2024
1 parent 0b46c71 commit 139080d
Show file tree
Hide file tree
Showing 30 changed files with 103 additions and 122 deletions.
6 changes: 1 addition & 5 deletions packages/core/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// temporarily disable failing rules so `nx lint` passes
// number of errors/warnings per rule recorded at Tue Nov 28 2023 15:38:21 GMT+0100 (Central European Standard Time)
"rules": {
"arrow-body-style": "off", // 3 warnings
"max-lines-per-function": "off", // 1 warning
"prefer-template": "off", // 3 warnings
"@typescript-eslint/no-confusing-void-expression": "off", // 2 warnings
Expand All @@ -12,15 +11,12 @@
"@typescript-eslint/no-unnecessary-condition": "off", // 4 warnings
"@typescript-eslint/no-unsafe-return": "off", // 1 error
"@typescript-eslint/prefer-nullish-coalescing": "off", // 2 warnings
"@typescript-eslint/require-await": "off", // 13 warnings
"functional/immutable-data": "off", // 1 error, 2 warnings
"functional/no-let": "off", // 2 warnings
"n/no-sync": "off", // 21 warnings
"n/no-sync": "off", // 6 warnings
"unicorn/catch-error-name": "off", // 3 warnings
"unicorn/explicit-length-check": "off", // 2 warnings
"unicorn/import-style": "off", // 6 warnings
"unicorn/numeric-separators-style": "off", // 2 warnings
"unicorn/prefer-node-protocol": "off", // 12 warnings
"unicorn/prefer-spread": "off", // 2 warnings
"unicorn/prefer-ternary": "off" // 1 warning
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,15 @@ describe('executePlugins', () => {
command: 'node',
args: ['-v'],
outputFile: 'output.json',
outputTransform: (outputs: unknown): Promise<AuditOutputs> => {
return Promise.resolve([
outputTransform: (outputs: unknown): Promise<AuditOutputs> =>
Promise.resolve([
{
slug: (outputs as AuditOutputs)[0]!.slug,
score: 0.3,
value: 16,
displayValue: '16.0.0',
},
]);
},
]),
},
},
],
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/implementation/persist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, mkdirSync } from 'fs';
import { stat, writeFile } from 'fs/promises';
import { join } from 'path';
import { existsSync, mkdirSync } from 'node:fs';
import { stat, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { PersistConfig, Report } from '@code-pushup/models';
import {
MultipleFileResults,
Expand Down
34 changes: 17 additions & 17 deletions packages/core/src/lib/implementation/persist.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import { vol } from 'memfs';
import { join } from 'path';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { beforeEach, describe, expect, it } from 'vitest';
import { Report } from '@code-pushup/models';
import { MEMFS_VOLUME, MINIMAL_REPORT_MOCK } from '@code-pushup/testing-utils';
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('persistReport', () => {
});

const jsonReport: Report = JSON.parse(
readFileSync(join(MEMFS_VOLUME, 'report.json')).toString(),
await readFile(join(MEMFS_VOLUME, 'report.json'), 'utf8'),
);
expect(jsonReport).toEqual(
expect.objectContaining({
Expand All @@ -50,9 +50,9 @@ describe('persistReport', () => {
}),
);

expect(() => readFileSync(join(MEMFS_VOLUME, 'report.md'))).toThrow(
'no such file or directory',
);
await expect(() =>
readFile(join(MEMFS_VOLUME, 'report.md')),
).rejects.toThrow('no such file or directory');
});

it('should create a report in md format', async () => {
Expand All @@ -62,12 +62,12 @@ describe('persistReport', () => {
format: ['md'],
});

const mdReport = readFileSync(join(MEMFS_VOLUME, 'report.md')).toString();
const mdReport = await readFile(join(MEMFS_VOLUME, 'report.md'), 'utf8');
expect(mdReport).toContain('Code PushUp Report');

expect(() => readFileSync(join(MEMFS_VOLUME, 'report.json'))).toThrow(
'no such file or directory',
);
await expect(() =>
readFile(join(MEMFS_VOLUME, 'report.json'), 'utf8'),
).rejects.toThrow('no such file or directory');
});

it('should create a report in all formats', async () => {
Expand All @@ -77,12 +77,12 @@ describe('persistReport', () => {
filename: 'report',
});

const mdReport = readFileSync(join(MEMFS_VOLUME, 'report.md')).toString();
const mdReport = await readFile(join(MEMFS_VOLUME, 'report.md'), 'utf8');
expect(mdReport).toContain('Code PushUp Report');
expect(mdReport).toContain('|🏷 Category|⭐ Score|🛡 Audits|');

const jsonReport: Report = JSON.parse(
readFileSync(join(MEMFS_VOLUME, 'report.json')).toString(),
await readFile(join(MEMFS_VOLUME, 'report.json'), 'utf8'),
);
expect(jsonReport).toEqual(
expect.objectContaining({
Expand All @@ -94,8 +94,8 @@ describe('persistReport', () => {
});

describe('logPersistedResults', () => {
it('should log report sizes correctly`', async () => {
logPersistedResults([{ status: 'fulfilled', value: ['out.json', 10000] }]);
it('should log report sizes correctly`', () => {
logPersistedResults([{ status: 'fulfilled', value: ['out.json', 10_000] }]);
expect(console.info).toHaveBeenNthCalledWith(
1,
'Generated reports successfully: ',
Expand All @@ -110,7 +110,7 @@ describe('logPersistedResults', () => {
);
});

it('should log fails correctly`', async () => {
it('should log fails correctly`', () => {
logPersistedResults([{ status: 'rejected', reason: 'fail' }]);

expect(console.warn).toHaveBeenNthCalledWith(
Expand All @@ -123,9 +123,9 @@ describe('logPersistedResults', () => {
);
});

it('should log report sizes and fails correctly`', async () => {
it('should log report sizes and fails correctly`', () => {
logPersistedResults([
{ status: 'fulfilled', value: ['out.json', 10000] },
{ status: 'fulfilled', value: ['out.json', 10_000] },
{ status: 'rejected', reason: 'fail' },
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect } from 'vitest';
import { readCodePushupConfig } from './read-code-pushup-config';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/implementation/runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path';
import { join } from 'node:path';
import {
AuditOutputs,
OnProgress,
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/lib/implementation/runner.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ describe('executeRunnerConfig', () => {
command: 'node',
args: ['-v'],
outputFile: 'output.json',
outputTransform: (outputs: unknown): Promise<AuditOutputs> => {
return Promise.resolve([
outputTransform: (outputs: unknown): Promise<AuditOutputs> =>
Promise.resolve([
{
slug: (outputs as AuditOutputs)[0]!.slug,
score: 0.3,
value: 16,
displayValue: '16.0.0',
},
]);
},
]),
});

expect(runnerResult.audits[0]?.slug).toBe('node-version');
Expand All @@ -71,11 +70,8 @@ describe('executeRunnerConfig', () => {
command: 'node',
args: ['-v'],
outputFile: 'output.json',
outputTransform: () => {
return Promise.reject(
new Error('Error: outputTransform has failed.'),
);
},
outputTransform: () =>
Promise.reject(new Error('Error: outputTransform has failed.')),
}),
).rejects.toThrow('Error: outputTransform has failed.');
});
Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-eslint/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
"@typescript-eslint/no-unsafe-return": "off", // 2 errors
"functional/immutable-data": "off", // 2 errors
"sonarjs/no-nested-template-literals": "off", // 1 error
"unicorn/better-regex": "off", // 1 warning
"unicorn/explicit-length-check": "off", // 1 warning
"unicorn/import-style": "off", // 5 warnings
"unicorn/prefer-node-protocol": "off", // 14 warnings
"unicorn/prefer-top-level-await": "off" // 1 warning
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from 'os';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import os from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { SpyInstance } from 'vitest';
import type { Audit, PluginConfig, RunnerConfig } from '@code-pushup/models';
import { toUnixPath } from '@code-pushup/utils';
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-eslint/src/lib/eslint-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mkdir, writeFile } from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { PluginConfig } from '@code-pushup/models';
import { name, version } from '../../package.json';
import { ESLintPluginConfig, eslintPluginConfigSchema } from './config';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-eslint/src/lib/meta/hash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHash } from 'crypto';
import { createHash } from 'node:crypto';
import { slugify } from '@code-pushup/utils';

export function ruleIdToSlug(
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-eslint/src/lib/meta/hash.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ describe('jsonHash', () => {
])('should produce short hexadecimal hash for rule options: %j', options => {
const hash = jsonHash(options);
expect(hash).toHaveLength(16);
expect(hash).toMatch(/^[0-9a-f]+$/);
expect(hash).toMatch(/^[\da-f]+$/);
});
});
4 changes: 2 additions & 2 deletions packages/plugin-eslint/src/lib/meta/rules.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ESLint } from 'eslint';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { SpyInstance } from 'vitest';
import { RuleData, listRules, parseRuleId } from './rules';

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-eslint/src/lib/nx.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { setWorkspaceRoot } from 'nx/src/utils/workspace-root';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import type { SpyInstance } from 'vitest';
import { ESLintPluginConfig } from './config';
import { eslintConfigFromNxProject, eslintConfigFromNxProjects } from './nx';
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin-eslint/src/lib/runner.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ESLint } from 'eslint';
import { mkdir, rm, writeFile } from 'fs/promises';
import os from 'os';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import type { SpyInstance } from 'vitest';
import { mkdir, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { SpyInstance, describe, expect, it } from 'vitest';
import type { AuditOutput, Issue } from '@code-pushup/models';
import { readJsonFile } from '@code-pushup/utils';
import { listAuditsAndGroups } from './meta';
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-eslint/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mkdir, writeFile } from 'fs/promises';
import { platform } from 'os';
import { dirname, join } from 'path';
import { mkdir, writeFile } from 'node:fs/promises';
import { platform } from 'node:os';
import { dirname, join } from 'node:path';
import type { Audit, AuditOutput, RunnerConfig } from '@code-pushup/models';
import { pluginWorkDir, readJsonFile, toArray } from '@code-pushup/utils';
import { lint } from './lint';
Expand Down
9 changes: 3 additions & 6 deletions packages/utils/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,29 @@
"@typescript-eslint/naming-convention": "off", // 8 warnings
"@typescript-eslint/no-floating-promises": "off", // 2 errors
"@typescript-eslint/no-shadow": "off", // 8 warnings
"@typescript-eslint/no-unnecessary-condition": "off", // 25 warnings
"@typescript-eslint/no-unnecessary-condition": "off", // 18 warnings
"@typescript-eslint/no-unnecessary-type-assertion": "off", // 1 error
"@typescript-eslint/no-unsafe-argument": "off", // 1 error
"@typescript-eslint/no-unsafe-assignment": "off", // 15 errors
"@typescript-eslint/no-unsafe-call": "off", // 8 errors
"@typescript-eslint/no-unsafe-call": "off", // 2 errors
"@typescript-eslint/no-unsafe-member-access": "off", // 12 errors
"@typescript-eslint/no-unsafe-return": "off", // 13 errors
"@typescript-eslint/prefer-nullish-coalescing": "off", // 11 warnings
"@typescript-eslint/require-await": "off", // 23 warnings
"@typescript-eslint/restrict-plus-operands": "off", // 1 error
"@typescript-eslint/restrict-template-expressions": "off", // 2 errors
"functional/immutable-data": "off", // 22 errors, 4 warnings
"functional/immutable-data": "off", // 25 errors
"functional/no-let": "off", // 12 warnings
"import/no-cycle": "off", // 2 errors
"import/no-named-as-default": "off", // 2 warnings
"import/no-useless-path-segments": "off", // 1 warning
"sonarjs/cognitive-complexity": "off", // 1 error
"sonarjs/no-duplicate-string": "off", // 1 warning
"unicorn/better-regex": "off", // 2 warnings
"unicorn/consistent-destructuring": "off", // 31 warnings
"unicorn/explicit-length-check": "off", // 2 warnings
"unicorn/import-style": "off", // 3 warnings
"unicorn/no-negated-condition": "off", // 1 warning
"unicorn/no-new-array": "off", // 6 warnings
"unicorn/numeric-separators-style": "off", // 12 warnings
"unicorn/prefer-node-protocol": "off", // 7 warnings
"unicorn/prefer-number-properties": "off", // 4 warnings
"unicorn/prefer-spread": "off", // 7 warnings
"unicorn/prefer-ternary": "off" // 1 warning
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/perf/crawl-file-system/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Benchmark from 'benchmark';
import { join } from 'path';
import { join } from 'node:path';
import {
CrawlFileSystemOptions,
crawlFileSystem,
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/lib/execute-process.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawn } from 'child_process';
import { spawn } from 'node:child_process';
import { calcDuration } from './report';

/**
Expand Down
26 changes: 13 additions & 13 deletions packages/utils/src/lib/execute-process.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ describe('executeProcess', () => {
args: ['-v'],
observer: spyObserver,
});
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver?.onError).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toMatch(/v[0-9]{1,2}(\.[0-9]{1,2}){0,2}/);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver.onError).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toMatch(/v\d{1,2}(\.\d{1,2}){0,2}/);
});

it('should work with npx command `npx --help`', async () => {
Expand All @@ -32,9 +32,9 @@ describe('executeProcess', () => {
args: ['--help'],
observer: spyObserver,
});
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver?.onError).toHaveBeenCalledTimes(0);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver.onError).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toContain('npm exec');
});

Expand All @@ -46,9 +46,9 @@ describe('executeProcess', () => {

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toContain('process:complete');
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(6); // intro + 4 runs + complete
expect(spyObserver?.onError).toHaveBeenCalledTimes(0);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(6); // intro + 4 runs + complete
expect(spyObserver.onError).toHaveBeenCalledTimes(0);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(1);
});

it('should work with async script `node custom-script.js` that throws an error', async () => {
Expand All @@ -63,8 +63,8 @@ describe('executeProcess', () => {

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(processResult).toBeUndefined();
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(2); // intro + 1 run before error
expect(spyObserver?.onError).toHaveBeenCalledTimes(1);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(0);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(2); // intro + 1 run before error
expect(spyObserver.onError).toHaveBeenCalledTimes(1);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(0);
});
});
4 changes: 2 additions & 2 deletions packages/utils/src/lib/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Options, bundleRequire } from 'bundle-require';
import chalk from 'chalk';
import { mkdir, readFile, readdir, stat } from 'fs/promises';
import { join } from 'path';
import { mkdir, readFile, readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';
import { formatBytes } from './formatting';
import { logMultipleResults } from './log-results';

Expand Down
Loading

0 comments on commit 139080d

Please sign in to comment.