Skip to content

Commit

Permalink
Merge pull request #580 from nextcloud/fix/allow-import-dev-dependenc…
Browse files Browse the repository at this point in the history
…ies-in-config

fix: Allow to import packages from `devDependencies` within config files
  • Loading branch information
susnux authored Jul 11, 2023
2 parents b7f9e81 + 540f227 commit d23421b
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ module.exports = {
ignoreGlobals: true,
}],
},
overrides: [
{
// Allow "unpublished" == devDependencies to be imported in config files
files: ['*.config.*', 'cypress/**/*', 'tests/**/*', '__tests__/**/*', '__mocks__/**/*'],
rules: {
'n/no-unpublished-import': 'off',
},
},
],
}
32 changes: 16 additions & 16 deletions tests/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -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 d23421b

Please sign in to comment.