Skip to content

Commit

Permalink
fix: disable linting on builds without a eslint configuration present
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed May 23, 2024
1 parent cda38b6 commit 5150051
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/lib/next-modes/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NextInstance } from './base'
import spawn from 'cross-spawn'
import { Span } from 'next/dist/trace'
import stripAnsi from 'strip-ansi'
import { providesESLintConfiguration } from 'next-test-utils'

export class NextStartInstance extends NextInstance {
private _buildId: string
Expand Down Expand Up @@ -68,6 +69,13 @@ export class NextStartInstance extends NextInstance {
if (this.buildCommand) {
buildArgs = this.buildCommand.split(' ')
}

// Disable linting if the directory provides an ESLint configuration. This
// prevents the project's eslint configuration from affecting the test.
if (!providesESLintConfiguration(this.testDir)) {
buildArgs.push('--no-lint')
}

if (this.startCommand) {
startArgs = this.startCommand.split(' ')
}
Expand Down
32 changes: 32 additions & 0 deletions test/lib/next-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,43 @@ export function launchApp(
)
}

/**
* Check if the directory provides an ESLint configuration.
*
* @param dir The directory to check.
* @returns Whether the directory provides an ESLint configuration.
*/
export function providesESLintConfiguration(dir: string) {
// If some of these files exist, we should not disable linting.
if (
['.eslintrc.json', '.eslintrc.js', '.eslintrc'].some((file) =>
existsSync(path.join(dir, file))
)
) {
return true
}

// If the `eslintConfig` field is present in the `package.json` file, we
// should not disable linting.
if (
existsSync(path.join(dir, 'package.json')) &&
readJson(path.join(dir, 'package.json')).eslintConfig
) {
return true
}

return false
}

export function nextBuild(
dir: string,
args: string[] = [],
opts: NextOptions = {}
) {
// Disable linting if the directory provides an ESLint configuration. This
// prevents the project's eslint configuration from affecting the test.
if (!providesESLintConfiguration(dir)) args.push('--no-lint')

return runNextCommand(['build', dir, ...args], opts)
}

Expand Down

0 comments on commit 5150051

Please sign in to comment.