From a6bad4dc979d718b47d37304787e39b72c9ecd95 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sat, 6 Jul 2024 21:39:09 -0500 Subject: [PATCH] test(scanner): add new eslint tests --- packages/scanner/src/utils/eslint.ts | 26 +-- .../eslint.test.ts => eslint/basic.test.ts} | 0 packages/scanner/tests/eslint/config.test.ts | 166 ++++++++++++++++++ 3 files changed, 181 insertions(+), 11 deletions(-) rename packages/scanner/tests/{basics/eslint.test.ts => eslint/basic.test.ts} (100%) create mode 100644 packages/scanner/tests/eslint/config.test.ts diff --git a/packages/scanner/src/utils/eslint.ts b/packages/scanner/src/utils/eslint.ts index f7e7d8a..181f6ad 100644 --- a/packages/scanner/src/utils/eslint.ts +++ b/packages/scanner/src/utils/eslint.ts @@ -1,9 +1,12 @@ +import path from 'node:path' + import { eslintFiles } from '@/constants' +import { checkDepedencies } from '@/helpers/check-dependencie' import type { ConfigReport, PackageJson } from '@/types' import type { ESLintConfig } from '@/types/configs' -import { findDependencie } from './package' // TODO: support plugins +// TODO: Check priority to obtain the configuration export function resolveESLint(files: string[], config: ConfigReport, content?: { packageJson?: PackageJson | null }) { const pathConfig = files.find((file) => { const splitPath = file.split('/') @@ -11,17 +14,22 @@ export function resolveESLint(files: string[], config: ConfigReport, content?: { return eslintFiles.find((eslintFile) => splitPath[splitPath.length - 1] === eslintFile) }) - const eslintConfig: ESLintConfig = { - installed: !config.checkDepedencies, - } + const eslintConfig: ESLintConfig = {} if (pathConfig) { eslintConfig.config = true - eslintConfig.path = pathConfig + eslintConfig.path = path.join(config.root, pathConfig) } if (!pathConfig) { - if (content?.packageJson == null) return null + if ( + content?.packageJson == null || + (config.checkDepedencies === true && + content?.packageJson?.dependencies == null && + content?.packageJson?.devDependencies == null && + content?.packageJson?.prettier == null) + ) + return null if (content.packageJson.eslintConfig != null) { eslintConfig.path = content.packageJson.path @@ -29,11 +37,7 @@ export function resolveESLint(files: string[], config: ConfigReport, content?: { } } - if (config.checkDepedencies && content?.packageJson != null) { - const installed = findDependencie(content.packageJson, 'eslint') - - eslintConfig.installed = installed - } + eslintConfig.installed = checkDepedencies(config.checkDepedencies, content?.packageJson, 'eslint') return eslintConfig } diff --git a/packages/scanner/tests/basics/eslint.test.ts b/packages/scanner/tests/eslint/basic.test.ts similarity index 100% rename from packages/scanner/tests/basics/eslint.test.ts rename to packages/scanner/tests/eslint/basic.test.ts diff --git a/packages/scanner/tests/eslint/config.test.ts b/packages/scanner/tests/eslint/config.test.ts new file mode 100644 index 0000000..6e9b348 --- /dev/null +++ b/packages/scanner/tests/eslint/config.test.ts @@ -0,0 +1,166 @@ +import path from 'node:path' +import { resolveESLint } from '@/utils/eslint' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +describe('resolve eslint config', () => { + beforeEach(() => { + vi.stubEnv('NODE_ENV', 'test') + }) + + it('should return ESLintConfig with config and path when eslint file is found in files', () => { + const files = ['src/.eslintrc.json'] + + const config = { root: '/root' } + + const result = resolveESLint(files, config) + + expect(result).toEqual({ + config: true, + path: path.join(config.root, 'src/.eslintrc.json'), + installed: true, + }) + }) + + it('should return null when files array is empty and packageJson is null', () => { + const config = { root: '/root' } + const content = { packageJson: null } + + const result = resolveESLint([], config, content) + + expect(result).toBeNull() + }) + + it('should set installed to true when eslint is found in dependencies', () => { + const files = ['package.json'] + const config = { root: '/root', checkDepedencies: true } + + const content = { packageJson: { eslintConfig: {}, path: '/root/package.json', dependencies: { eslint: '9.0.0' } } } + + const result = resolveESLint(files, config, content) + + expect(result).toEqual({ + config: true, + installed: true, + path: path.join(config.root, 'package.json'), + }) + }) + + it('should set installed to false when eslint is not found in dependencies', () => { + const files = ['package.json'] + const config = { root: '/root', checkDepedencies: true } + + const content = { packageJson: { path: '/root/package.json', dependencies: { '@biomejs/biome': '9.0.0' } } } + + const result = resolveESLint(files, config, content) + + expect(result).toEqual({ + installed: false, + }) + }) + + it('should return null when no eslint file is found and packageJson is null', () => { + const files = ['src/.lint'] + const config = { root: 'false' } + const content = { packageJson: null } + + const result = resolveESLint(files, config, content) + + expect(result).toBeNull() + }) + + it('should return null when files array is empty', () => { + const config = { root: '/root', checkDepedencies: true } + const result = resolveESLint([], config) + + expect(result).toBe(null) + }) + + it('should return config with path and installed false when packageJson is missing and prettier config is found', () => { + const files = ['src/.eslintrc.json'] + const config = { root: '/root', checkDepedencies: true } + const result = resolveESLint(files, config) + + expect(result).toEqual({ + path: '/root/src/.eslintrc.json', + config: true, + installed: false, + }) + }) + + it('should return config with path when eslint config file is found in packageJson', () => { + const files = ['src/index.js', 'package.json'] + const config = { root: '/root', checkDepedencies: false } + const content = { + packageJson: { + path: 'root/package.json', + eslintConfig: {}, + }, + } + + const result = resolveESLint(files, config, content) + + expect(result).toEqual({ + installed: false, + config: true, + path: 'root/package.json', + }) + }) + + it('should return null when packageJson has no eslintConfig field and dependencies field and eslint config is not found', () => { + const files = ['src/index.js', 'package.json'] + const config = { root: '/root', checkDepedencies: true } + const content = { packageJson: { path: 'package.json' } } + const result = resolveESLint(files, config, content) + + expect(result).toBeNull() + }) + + it('NODE_ENV=test should return installed true when checkDepedencies is missing', () => { + const files = ['src/file1.ts', 'src/file2.ts', '.eslintrc.json'] + const config = { root: '/root' } + + const result = resolveESLint(files, config) + + expect(result).toEqual({ + installed: true, + config: true, + path: '/root/.eslintrc.json', + }) + }) + + it('NODE_ENV=development should return installed false when checkDepedencies is missing', () => { + vi.stubEnv('NODE_ENV', 'development') + + const files = ['src/file1.ts', 'src/file2.ts', '.eslintrc.json'] + const config = { root: '/root' } + + const result = resolveESLint(files, config) + + expect(result).toEqual({ + installed: false, + config: true, + path: '/root/.eslintrc.json', + }) + }) + + it('should return config with path when both dependencies and devDependencies are present in package.json', () => { + const files = ['src/index.js', 'package.json'] + const config = { root: '/project', checkDepedencies: true } + const content = { + packageJson: { + path: '/project/package.json', + dependencies: { eslint: '^9.0.0' }, + devDependencies: { eslint: '^9.0.0' }, + eslintConfig: {}, + }, + } + + const result = resolveESLint(files, config, content) + + expect(result).toEqual({ + config: true, + path: '/project/package.json', + installed: true, + }) + }) +})