Unverified Commit dff6a71e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix config import on Windows and disable test in failing envs (#29971)

Follow-up to https://github.com/vercel/next.js/pull/29935 this fixes the failing tests on Azure as it seems the `import()` needs to be prefixed with `file://` on Windows or an error is thrown. It also disables the config unit tests since a dynamic import inside of `jest` on either an m1 on macOS or Windows using node 14 causes a segfault from being used inside of `vm`. 

<details>

<summary>segfault error in jest</summary>

<img width="962" alt="Screen Shot 2021-10-15 at 16 45 04" src="https://user-images.githubusercontent.com/22380829/137596819-072233e3-1a45-445f-a3dd-c03ec62fb4ce.png">

</details>


<details>

<summary>Windows absolute import error</summary>


<img width="1114" alt="Screen Shot 2021-10-16 at 12 45 59" src="https://user-images.githubusercontent.com/22380829/137597185-200f3164-7f79-488f-acc9-6c812ce5bc4a.png">

</details>
parent 701fa999
...@@ -530,7 +530,8 @@ export default async function loadConfig( ...@@ -530,7 +530,8 @@ export default async function loadConfig(
let userConfigModule: any let userConfigModule: any
try { try {
userConfigModule = await import(path) // we must use file for absolute dynamic imports on Windows
userConfigModule = await import(`file://${path}`)
} catch (err) { } catch (err) {
console.error( console.error(
chalk.red('Error:') + chalk.red('Error:') +
......
...@@ -4,14 +4,14 @@ import cheerio from 'cheerio' ...@@ -4,14 +4,14 @@ import cheerio from 'cheerio'
import fs, { existsSync } from 'fs-extra' import fs, { existsSync } from 'fs-extra'
import globOriginal from 'glob' import globOriginal from 'glob'
import { import {
nextServer,
renderViaHTTP, renderViaHTTP,
runNextCommand,
startApp,
stopApp,
waitFor, waitFor,
getPageFileFromPagesManifest, getPageFileFromPagesManifest,
check, check,
nextBuild,
nextStart,
findPort,
killApp,
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { import {
...@@ -31,7 +31,6 @@ const glob = promisify(globOriginal) ...@@ -31,7 +31,6 @@ const glob = promisify(globOriginal)
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
let appPort let appPort
let server
let app let app
const context = {} const context = {}
...@@ -39,28 +38,23 @@ const context = {} ...@@ -39,28 +38,23 @@ const context = {}
describe('Production Usage', () => { describe('Production Usage', () => {
let output = '' let output = ''
beforeAll(async () => { beforeAll(async () => {
const result = await runNextCommand(['build', appDir], { const result = await nextBuild(appDir, undefined, {
stderr: true, stderr: true,
stdout: true, stdout: true,
}) })
app = nextServer({ appPort = await findPort()
dir: join(__dirname, '../'), context.appPort = appPort
dev: false, app = await nextStart(appDir, appPort)
quiet: true,
})
output = (result.stderr || '') + (result.stdout || '') output = (result.stderr || '') + (result.stdout || '')
console.log(output) console.log(output)
if (result.code !== 0) { if (result.code !== 0) {
throw new Error(`Failed to build, exited with code ${result.code}`) throw new Error(`Failed to build, exited with code ${result.code}`)
} }
server = await startApp(app)
context.appPort = appPort = server.address().port
}) })
afterAll(async () => { afterAll(async () => {
await stopApp(server) await killApp(app)
}) })
it('should contain generated page count in output', async () => { it('should contain generated page count in output', async () => {
...@@ -645,19 +639,8 @@ describe('Production Usage', () => { ...@@ -645,19 +639,8 @@ describe('Production Usage', () => {
describe('Misc', () => { describe('Misc', () => {
it('should handle already finished responses', async () => { it('should handle already finished responses', async () => {
const res = { const html = await renderViaHTTP(appPort, '/finish-response')
finished: false, expect(html).toBe('hi')
end() {
this.finished = true
},
}
const html = await app.renderToHTML(
{ method: 'GET' },
res,
'/finish-response',
{}
)
expect(html).toBeFalsy()
}) })
it('should allow to access /static/ and /_next/', async () => { it('should allow to access /static/ and /_next/', async () => {
......
/* eslint-env jest */ /* eslint-env jest */
import os from 'os'
import { join } from 'path' import { join } from 'path'
import loadConfig from 'next/dist/server/config' import loadConfig from 'next/dist/server/config'
import { PHASE_DEVELOPMENT_SERVER } from 'next/constants' import { PHASE_DEVELOPMENT_SERVER } from 'next/constants'
...@@ -8,111 +8,121 @@ const pathToConfig = join(__dirname, '_resolvedata', 'without-function') ...@@ -8,111 +8,121 @@ const pathToConfig = join(__dirname, '_resolvedata', 'without-function')
const pathToConfigFn = join(__dirname, '_resolvedata', 'with-function') const pathToConfigFn = join(__dirname, '_resolvedata', 'with-function')
describe('config', () => { describe('config', () => {
it('Should get the configuration', async () => { if (os.platform() === 'linux') {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig) it('Should get the configuration', async () => {
expect(config.customConfig).toBe(true) const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig)
}) expect(config.customConfig).toBe(true)
})
it('Should pass the phase correctly', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) it('Should pass the phase correctly', async () => {
expect(config.phase).toBe(PHASE_DEVELOPMENT_SERVER) const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn)
}) expect(config.phase).toBe(PHASE_DEVELOPMENT_SERVER)
})
it('Should pass the defaultConfig correctly', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) it('Should pass the defaultConfig correctly', async () => {
expect(config.defaultConfig).toBeDefined() const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn)
}) expect(config.defaultConfig).toBeDefined()
})
it('Should assign object defaults deeply to user config', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) it('Should assign object defaults deeply to user config', async () => {
expect(config.distDir).toEqual('.next') const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn)
expect(config.onDemandEntries.maxInactiveAge).toBeDefined() expect(config.distDir).toEqual('.next')
}) expect(config.onDemandEntries.maxInactiveAge).toBeDefined()
})
it('Should pass the customConfig correctly', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { it('Should pass the customConfig correctly', async () => {
customConfig: true, const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
}) customConfig: true,
expect(config.customConfig).toBe(true) })
}) expect(config.customConfig).toBe(true)
})
it('Should not pass the customConfig when it is null', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, null) it('Should not pass the customConfig when it is null', async () => {
expect(config.webpack).toBe(null) const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, null)
}) expect(config.webpack).toBe(null)
})
it('Should assign object defaults deeply to customConfig', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { it('Should assign object defaults deeply to customConfig', async () => {
customConfig: true, const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
onDemandEntries: { custom: true }, customConfig: true,
}) onDemandEntries: { custom: true },
expect(config.customConfig).toBe(true) })
expect(config.onDemandEntries.maxInactiveAge).toBeDefined() expect(config.customConfig).toBe(true)
}) expect(config.onDemandEntries.maxInactiveAge).toBeDefined()
})
it('Should allow setting objects which do not have defaults', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { it('Should allow setting objects which do not have defaults', async () => {
bogusSetting: { custom: true }, const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
}) bogusSetting: { custom: true },
expect(config.bogusSetting).toBeDefined() })
expect(config.bogusSetting.custom).toBe(true) expect(config.bogusSetting).toBeDefined()
}) expect(config.bogusSetting.custom).toBe(true)
})
it('Should override defaults for arrays from user arrays', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { it('Should override defaults for arrays from user arrays', async () => {
pageExtensions: ['.bogus'], const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
}) pageExtensions: ['.bogus'],
expect(config.pageExtensions).toEqual(['.bogus']) })
}) expect(config.pageExtensions).toEqual(['.bogus'])
})
it('Should throw when an invalid target is provided', async () => {
await expect(async () => { it('Should throw when an invalid target is provided', async () => {
await loadConfig( await expect(async () => {
PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'invalid-target')
)
}).rejects.toThrow(/Specified target is invalid/)
})
it('Should pass when a valid target is provided', async () => {
const config = await loadConfig(
PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'valid-target')
)
expect(config.target).toBe('serverless')
})
it('Should throw an error when next.config.js is not present', async () => {
await expect(
async () =>
await loadConfig( await loadConfig(
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'typescript-config') join(__dirname, '_resolvedata', 'invalid-target')
) )
).rejects.toThrow( }).rejects.toThrow(/Specified target is invalid/)
/Configuring Next.js via .+ is not supported. Please replace the file with 'next.config.js'/ })
)
}) it('Should pass when a valid target is provided', async () => {
const config = await loadConfig(
it('Should not throw an error when two versions of next.config.js are present', async () => { PHASE_DEVELOPMENT_SERVER,
const config = await loadConfig( join(__dirname, '_resolvedata', 'valid-target')
PHASE_DEVELOPMENT_SERVER, )
join(__dirname, '_resolvedata', 'js-ts-config') expect(config.target).toBe('serverless')
) })
expect(config.__test__ext).toBe('js')
}) it('Should throw an error when next.config.js is not present', async () => {
await expect(
it('Should ignore configs set to `undefined`', async () => { async () =>
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { await loadConfig(
target: undefined, PHASE_DEVELOPMENT_SERVER,
}) join(__dirname, '_resolvedata', 'typescript-config')
expect(config.target).toBe('server') )
}) ).rejects.toThrow(
/Configuring Next.js via .+ is not supported. Please replace the file with 'next.config.js'/
it('Should ignore configs set to `null`', async () => { )
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { })
target: null,
}) it('Should not throw an error when two versions of next.config.js are present', async () => {
expect(config.target).toBe('server') const config = await loadConfig(
}) PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'js-ts-config')
)
expect(config.__test__ext).toBe('js')
})
it('Should ignore configs set to `undefined`', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
target: undefined,
})
expect(config.target).toBe('server')
})
it('Should ignore configs set to `null`', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, {
target: null,
})
expect(config.target).toBe('server')
})
} else {
// TODO: remove this when segfault no longer occurs with dynamic
// import inside of jest due to vm usage
it('should skip on non-linux platforms', () => {
console.warn(
'Warning!! These tests can not currently run on non-linux systems'
)
})
}
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment