Skip to content

Commit

Permalink
fix: Allow to import packages from devDependencies within config files
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jul 2, 2023
1 parent a2e61d0 commit 5e2dd3e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
9 changes: 9 additions & 0 deletions parts/base.js
Expand Up @@ -93,4 +93,13 @@ module.exports = {
ignoreGlobals: true,
}],
},
overrides: [
{
// Allow "unpublished" == devDependencies to be imported in config files
files: ['./*.config.*'],
rules: {
'n/no-unpublished-import': 'off',
},
},
],
}
32 changes: 16 additions & 16 deletions tests/setup-jest.ts
@@ -1,12 +1,12 @@
import { ESLint, Linter } from "eslint"
import { ESLint, Linter } from 'eslint'

/**
* Add some custom matchers for ESLint to jest
*/
expect.extend({
toPass: assertLintingPassed,
toHaveIssueCount: assertHavingNIssues,
toHaveIssue: assertHavingIssue
toHaveIssue: assertHavingIssue,
})

/**
Expand All @@ -24,8 +24,8 @@ function hasNoIssues(received: ESLint.LintResult) {
/**
* Check if linting of multiple fils
*
* @param received
* @returns {}
* @param received
* @return {}
*/
function assertLintingPassed(received: ESLint.LintResult | ESLint.LintResult[]) {
// allow single ESLintResult
Expand All @@ -36,7 +36,7 @@ function assertLintingPassed(received: ESLint.LintResult | ESLint.LintResult[])
const errors = [] as {file: string, errors: Linter.LintMessage[]}[]
const pass = received.every((result) => {
// save issues
errors.push({file: result.filePath, errors: result.messages})
errors.push({ file: result.filePath, errors: result.messages })
return hasNoIssues(result)
})

Expand All @@ -46,20 +46,20 @@ function assertLintingPassed(received: ESLint.LintResult | ESLint.LintResult[])
if (pass) {
return 'Expected file to not pass eslint, but got no issues'
} else {
const errorMessages = errors.map((m) =>
`file: ${m.file}\n` + m.errors.map((e) => 'line: ' + e.line + ': ' + (e.ruleId || e.message))
const errorMessages = errors.map((m) =>
`file: ${m.file}\n` + m.errors.map((e) => 'line: ' + e.line + ': ' + (e.ruleId || e.message)),
)
return 'Expected file to pass eslint, got issues:\n' + errorMessages.join('\n')
}
}
},
}
}

/**
* Count the total amount of issues
*
* @param received lint result
* @returns total amount of issues
* @return total amount of issues
*/
function countIssues(received: ESLint.LintResult) {
return received.errorCount + received.warningCount
Expand All @@ -70,7 +70,7 @@ function countIssues(received: ESLint.LintResult) {
*
* @param received the lint result
* @param expected number of expected issues
* @returns jest matcher result
* @return jest matcher result
*/
function assertHavingNIssues(received: ESLint.LintResult | ESLint.LintResult[], expected: number) {
if (!(typeof expected === 'number')) throw new Error('Expected a number as expected value')
Expand All @@ -84,7 +84,7 @@ function assertHavingNIssues(received: ESLint.LintResult | ESLint.LintResult[],

return {
pass,
message: () => pass ? `Expected not to find exactly ${expected} issues.` : `Expected ${expected} issues, found ${issues}`
message: () => pass ? `Expected not to find exactly ${expected} issues.` : `Expected ${expected} issues, found ${issues}`,
}
}

Expand All @@ -93,7 +93,7 @@ function assertHavingNIssues(received: ESLint.LintResult | ESLint.LintResult[],
*
* @param received the lint result
* @param issue the expected issue
* @returns jest matcher result
* @return jest matcher result
*/
function assertHavingIssue(received: ESLint.LintResult | ESLint.LintResult[], issue: string | {ruleId: string, line?: number}) {
if (!Array.isArray(received)) {
Expand All @@ -104,7 +104,7 @@ function assertHavingIssue(received: ESLint.LintResult | ESLint.LintResult[], is
if (assertLintingPassed(received).pass) {
return {
pass: false,
message: () => 'Expected issue, but no linting issues found.'
message: () => 'Expected issue, but no linting issues found.',
}
}

Expand All @@ -115,14 +115,14 @@ function assertHavingIssue(received: ESLint.LintResult | ESLint.LintResult[], is
if (message.ruleId !== name) return false
// if line is requested ignore not matching ones
if (typeof issue === 'object' && issue.line !== undefined && issue.line !== message.line) return false
// otherwise matched
// otherwise matched
return true
})
});
})

const onLine = typeof issue === 'string' ? '' : ` on line ${issue.line}`
return {
pass: result,
message: () => result ? `Unexpected error '${name}'${onLine} found.` : `Expected error '${name}'${onLine} not found.`
message: () => result ? `Unexpected error '${name}'${onLine} found.` : `Expected error '${name}'${onLine} not found.`,
}
}

0 comments on commit 5e2dd3e

Please sign in to comment.