chore: fixes dev server doesn't drop the database (#8834)
Apparently, `nextDev` seems to run in a different process and has its own env variables, we can run the dev server the same way we run it for E2Es instead via `createServer`.
This commit is contained in:
31
test/dev.ts
31
test/dev.ts
@@ -1,11 +1,13 @@
|
|||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
|
import { createServer } from 'http'
|
||||||
import minimist from 'minimist'
|
import minimist from 'minimist'
|
||||||
import { nextDev } from 'next/dist/cli/next-dev.js'
|
import nextImport from 'next'
|
||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import { fileURLToPath } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
import open from 'open'
|
import open from 'open'
|
||||||
import { loadEnv } from 'payload/node'
|
import { loadEnv } from 'payload/node'
|
||||||
|
import { parse } from 'url'
|
||||||
|
|
||||||
import { getNextRootDir } from './helpers/getNextRootDir.js'
|
import { getNextRootDir } from './helpers/getNextRootDir.js'
|
||||||
import { runInit } from './runInit.js'
|
import { runInit } from './runInit.js'
|
||||||
@@ -23,8 +25,6 @@ loadEnv()
|
|||||||
const filename = fileURLToPath(import.meta.url)
|
const filename = fileURLToPath(import.meta.url)
|
||||||
const dirname = path.dirname(filename)
|
const dirname = path.dirname(filename)
|
||||||
|
|
||||||
process.env.PAYLOAD_DROP_DATABASE = process.env.PAYLOAD_DROP_DATABASE === 'false' ? 'false' : 'true'
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
_: [testSuiteArg],
|
_: [testSuiteArg],
|
||||||
...args
|
...args
|
||||||
@@ -53,7 +53,30 @@ if (args.o) {
|
|||||||
|
|
||||||
const port = process.env.PORT ? Number(process.env.PORT) : 3000
|
const port = process.env.PORT ? Number(process.env.PORT) : 3000
|
||||||
|
|
||||||
await nextDev({ port }, 'default', rootDir)
|
const app = nextImport({
|
||||||
|
dev: true,
|
||||||
|
hostname: 'localhost',
|
||||||
|
port,
|
||||||
|
dir: rootDir,
|
||||||
|
})
|
||||||
|
|
||||||
|
const handle = app.getRequestHandler()
|
||||||
|
|
||||||
|
let resolveServer
|
||||||
|
|
||||||
|
const serverPromise = new Promise((res) => (resolveServer = res))
|
||||||
|
|
||||||
|
void app.prepare().then(() => {
|
||||||
|
createServer(async (req, res) => {
|
||||||
|
const parsedUrl = parse(req.url, true)
|
||||||
|
await handle(req, res, parsedUrl)
|
||||||
|
}).listen(port, () => {
|
||||||
|
resolveServer()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
await serverPromise
|
||||||
|
process.env.PAYLOAD_DROP_DATABASE = process.env.PAYLOAD_DROP_DATABASE === 'false' ? 'false' : 'true'
|
||||||
|
|
||||||
// fetch the admin url to force a render
|
// fetch the admin url to force a render
|
||||||
void fetch(`http://localhost:${port}${adminRoute}`)
|
void fetch(`http://localhost:${port}${adminRoute}`)
|
||||||
|
|||||||
Reference in New Issue
Block a user