Skip to content

Commit

Permalink
fix custom server threads env variables to next server
Browse files Browse the repository at this point in the history
  • Loading branch information
r1tsuu committed Dec 28, 2024
1 parent 3927b87 commit 167a805
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/_community/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,4 @@ export interface Auth {
declare module 'payload' {
// @ts-ignore
export interface GeneratedTypes extends Config {}
}
}
36 changes: 9 additions & 27 deletions test/dev.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
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'
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')
Expand Down Expand Up @@ -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}`)
Expand Down
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down

0 comments on commit 167a805

Please sign in to comment.