Skip to content

Commit

Permalink
fix: support config defined in next.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed May 23, 2024
1 parent f1d03d5 commit 8c89ae5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
4 changes: 2 additions & 2 deletions test/lib/next-modes/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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'
import { shouldDisableLinting } from 'next-test-utils'

export class NextStartInstance extends NextInstance {
private _buildId: string
Expand Down Expand Up @@ -72,7 +72,7 @@ export class NextStartInstance extends NextInstance {

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

Expand Down
68 changes: 49 additions & 19 deletions test/lib/next-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import fetch from 'node-fetch'
import qs from 'querystring'
import treeKill from 'tree-kill'
import { once } from 'events'
import vm from 'vm'

import server from 'next/dist/server/next'
import _pkg from 'next/package.json'
Expand Down Expand Up @@ -437,32 +438,61 @@ export function launchApp(
)
}

/**
* Read a JavaScript file and return the exports.
*
* @param path The path to the JavaScript file.
* @returns The exports of the JavaScript file.
*/
function readJS(path: string) {
const data = readFileSync(path, 'utf8')
const script = new vm.Script(data, { filename: path })
const context = { module: { exports: {} } }
script.runInNewContext(context)
return context.module.exports
}

/**
* 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
}
export function shouldDisableLinting(dir: string) {
try {
// If the project already has a `next.config.js` file that includes an eslint
// config, then don't disable linting.
if (
existsSync(path.join(dir, 'next.config.js')) &&
'eslint' in readJS(path.join(dir, 'next.config.js'))
) {
return false
}
} catch {}

// 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
}
try {
// 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 false
}
} catch {}

return false
try {
// 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 false
}
} catch {}

return true
}

export function nextBuild(
Expand All @@ -472,7 +502,7 @@ export function nextBuild(
) {
// 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')
if (shouldDisableLinting(dir)) args.push('--no-lint')

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

0 comments on commit 8c89ae5

Please sign in to comment.