Skip to content

Commit

Permalink
test(scanner): add new eslint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Jul 7, 2024
1 parent 892002d commit a6bad4d
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packages/scanner/src/utils/eslint.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
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('/')

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
eslintConfig.config = true
}
}

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
}
File renamed without changes.
166 changes: 166 additions & 0 deletions packages/scanner/tests/eslint/config.test.ts
Original file line number Diff line number Diff line change
@@ -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,
})
})
})

0 comments on commit a6bad4d

Please sign in to comment.