Files
payload/test/helpers/initPayloadE2ENoConfig.ts
Alessio Gravili 96e7c95ebc chore: upgrade to pnpm v9, regenerate lockfile (#7369)
- regenerates the lockfile
- upgrades pnpm from v8 to v9.7.0 minimum
- ensures playwright does not import payload config. Even after our
importmap revamp that made the payload config server-only / node-safe, I
was getting these `Error: Invariant: AsyncLocalStorage accessed in
runtime where it is not available` errors in combination with pnpm v9
and lockfile regeneration.
This does not happen with pnpm v8, however I'm still blaming playwright
for this, as this does not happen in dev and we've had this specific
error with playwright in the past when we were importing the payload
config. Perhaps it's related to both playwright and the future Next.js
process importing the same config file, and not related to the config
file containing client-side React code.
Making sure playwright doesn't import the config fixed it (it was
importing it through the import map generation). The import map
generation is now run in a separate process, and playwright simply waits
for it
- One positive thing: this pr fixes a bunch of typescript errors with
react-select components. We got those errors because react-select types
are not compatible with react 19. lockfile regeneration fixed that (not
related to pnpm v9) - probably because we were installing mismatching
react versions (I saw both `fb9a90fa48-20240614` and `06d0b89e-20240801`
in our lockfile). I have thus removed the caret for react and react-dom
in our package.json - now it's consistent
2024-08-14 08:57:04 -04:00

107 lines
2.7 KiB
TypeScript

import { createServer } from 'http'
import nextImport from 'next'
import { spawn } from 'node:child_process'
import path, { dirname, resolve } from 'path'
import { wait } from 'payload/shared'
import { fileURLToPath, parse } from 'url'
import type { GeneratedTypes } from './sdk/types.js'
import { runInitSeparateProcess } from '../runInitSeparateProcess.js'
import { createTestHooks } from '../testHooks.js'
import { getNextJSRootDir } from './getNextJSRootDir.js'
import { PayloadTestSDK } from './sdk/index.js'
import startMemoryDB from './startMemoryDB.js'
const _filename = fileURLToPath(import.meta.url)
const _dirname = dirname(_filename)
type Args = {
dirname: string
prebuild?: boolean
}
type Result<T extends GeneratedTypes<T>> = {
payload: PayloadTestSDK<T>
serverURL: string
}
export async function initPayloadE2ENoConfig<T extends GeneratedTypes<T>>({
dirname,
prebuild,
}: Args): Promise<Result<T>> {
const testSuiteName = path.basename(dirname)
await runInitSeparateProcess(testSuiteName, true)
const { beforeTest } = await createTestHooks(testSuiteName)
await beforeTest()
const port = 3000
process.env.PORT = String(port)
const serverURL = `http://localhost:${port}`
await startMemoryDB()
const { rootDir } = getNextJSRootDir(testSuiteName)
if (prebuild) {
await new Promise<void>((res, rej) => {
const buildArgs = ['--max-old-space-size=8192', resolve(_dirname, 'build.js')]
const childProcess = spawn('node', buildArgs, {
stdio: 'inherit',
env: {
PATH: process.env.PATH,
NODE_ENV: 'production',
NEXTJS_DIR: rootDir,
},
})
childProcess.on('close', (code) => {
if (code === 0) res()
rej()
})
})
}
process.env.NODE_OPTIONS = '--max-old-space-size=8192 --no-deprecation'
// @ts-expect-error
const app = nextImport({
dev: !prebuild,
hostname: 'localhost',
port,
dir: rootDir,
})
const handle = app.getRequestHandler()
let resolveServer
const serverPromise = new Promise((res) => (resolveServer = res))
// Need a custom server because calling nextDev straight
// starts up a child process, and payload.onInit() is called twice
// which seeds test data twice + other bad things.
// We initialize Payload above so we can have access to it in the tests
void app.prepare().then(() => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
createServer(async (req, res) => {
const parsedUrl = parse(req.url, true)
await handle(req, res, parsedUrl)
}).listen(port, () => {
resolveServer()
})
})
await serverPromise
await wait(port)
return {
serverURL,
payload: new PayloadTestSDK<T>({ serverURL }),
}
}