From 167a805c74cd50e311443383f3f0e6d87bdec2ed Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Sat, 28 Dec 2024 03:11:39 +0200 Subject: [PATCH] fix custom server threads env variables to next server --- pnpm-lock.yaml | 3 +++ test/_community/payload-types.ts | 2 +- test/dev.ts | 36 ++++++++------------------------ test/package.json | 1 + 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 656b065bc43..42977798768 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1616,6 +1616,9 @@ importers: '@aws-sdk/client-s3': specifier: ^3.614.0 version: 3.687.0 + '@next/env': + specifier: 15.1.3 + version: 15.1.3 '@payloadcms/db-mongodb': specifier: workspace:* version: link:../packages/db-mongodb diff --git a/test/_community/payload-types.ts b/test/_community/payload-types.ts index a259f6c7c5c..fd6483846a1 100644 --- a/test/_community/payload-types.ts +++ b/test/_community/payload-types.ts @@ -336,4 +336,4 @@ export interface Auth { declare module 'payload' { // @ts-ignore export interface GeneratedTypes extends Config {} -} +} \ No newline at end of file diff --git a/test/dev.ts b/test/dev.ts index 3a0846b36d8..372d9705281 100644 --- a/test/dev.ts +++ b/test/dev.ts @@ -1,13 +1,12 @@ +import { updateInitialEnv } from '@next/env' import chalk from 'chalk' -import { createServer } from 'http' import minimist from 'minimist' -import nextImport from 'next' +import { nextDev } from 'next/dist/cli/next-dev.js' import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import open from 'open' import { loadEnv } from 'payload/node' -import { parse } from 'url' import { getNextRootDir } from './helpers/getNextRootDir.js' import startMemoryDB from './helpers/startMemoryDB.js' @@ -15,6 +14,8 @@ import { runInit } from './runInit.js' import { child, safelyRunScriptFunction } from './safelyRunScript.js' import { createTestHooks } from './testHooks.js' +process.env.PAYLOAD_DROP_DATABASE = process.env.PAYLOAD_DROP_DATABASE === 'false' ? 'false' : 'true' + const prod = process.argv.includes('--prod') if (prod) { process.argv = process.argv.filter((arg) => arg !== '--prod') @@ -65,31 +66,12 @@ if (args.o) { const port = process.env.PORT ? Number(process.env.PORT) : 3000 -// @ts-expect-error the same as in test/helpers/initPayloadE2E.ts -const app = nextImport({ - dev: true, - hostname: 'localhost', - port, - dir: rootDir, -}) - -const handle = app.getRequestHandler() - -let resolveServer +// This is needed to forward the environment variables to the next process that were created in dev.ts directly +// for example process.env.MONGODB_MEMORY_SERVER_URI process.env.PAYLOAD_DROP_DATABASE +// Otherwise nextDev won't have them +updateInitialEnv(process.env) -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' +await nextDev({ port, disableSourceMaps: true }, 'default', rootDir) // fetch the admin url to force a render void fetch(`http://localhost:${port}${adminRoute}`) diff --git a/test/package.json b/test/package.json index ff6973ba71c..9748841d74f 100644 --- a/test/package.json +++ b/test/package.json @@ -23,6 +23,7 @@ }, "devDependencies": { "@aws-sdk/client-s3": "^3.614.0", + "@next/env": "15.1.3", "@payloadcms/db-mongodb": "workspace:*", "@payloadcms/db-postgres": "workspace:*", "@payloadcms/db-sqlite": "workspace:*",