Commit f85e550b authored by Jude Gao's avatar Jude Gao

Add e2e test for global lockfile server discovery

parent 9d1572d7
import { nextTestSetup } from 'e2e-utils'
import execa from 'execa'
import fs from 'fs'
import os from 'os'
import path from 'path'
import stripAnsi from 'strip-ansi'
......@@ -9,6 +10,12 @@ describe('lockfile', () => {
files: __dirname,
})
afterAll(() => {
// Clean up global lockfile created during test
const globalLockDir = path.join(os.homedir(), '.next', 'dev', next.appPort)
fs.rmSync(globalLockDir, { recursive: true, force: true })
})
it('only allows a single instance of `next dev` to run at a time', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('Page')
......@@ -67,4 +74,36 @@ describe('lockfile', () => {
await browser.refresh()
expect(await browser.elementByCss('p').text()).toBe('Page')
})
it('creates a global lockfile at ~/.next/dev/<port>/lock with flock held', async () => {
const globalLockfilePath = path.join(
os.homedir(),
'.next',
'dev',
next.appPort,
'lock'
)
expect(fs.existsSync(globalLockfilePath)).toBe(true)
// Verify lockfile content has correct server info
const serverInfo = JSON.parse(fs.readFileSync(globalLockfilePath, 'utf-8'))
expect(serverInfo).toMatchObject({
pid: expect.any(Number),
port: Number(next.appPort),
hostname: 'localhost',
appUrl: expect.stringContaining(next.appPort),
startedAt: expect.any(Number),
})
// Verify the process from the lockfile is actually alive
const isAlive = (() => {
try {
process.kill(serverInfo.pid, 0)
return true
} catch {
return false
}
})()
expect(isAlive).toBe(true)
})
})
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