Files
payloadcms/test/generateImportMap.ts
Alessio Gravili 9c559d7304 chore: fix live-preview tests against prod (#9122)
Live preview e2e tests had no CSS when tested against prod.

For all our other tests, we have a separate test/app directory that
imports CSS. Otherwise, the root-level /app directory is used.

For live-preview, we currently always run against test/live-preview/app,
that has no CSS import.

This PR adds a new test/live-preview/prod/app directory that imports CSS
and is used when we run tests against prod.

In order for this to work, I had to make import map generation smarter
2024-11-11 19:28:55 -07:00

48 lines
1.3 KiB
TypeScript

import path from 'path'
const [testConfigDir] = process.argv.slice(2)
import type { SanitizedConfig } from 'payload'
import fs from 'fs'
import { generateImportMap } from 'payload'
import { fileURLToPath } from 'url'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
let testDir: string
async function run() {
if (testConfigDir) {
testDir = path.resolve(dirname, testConfigDir)
const pathWithConfig = path.resolve(testDir, 'config.ts')
console.log('Generating ad-hoc import map for config:', pathWithConfig)
const config: SanitizedConfig = await (await import(pathWithConfig)).default
let rootDir = ''
if (testConfigDir === 'live-preview' || testConfigDir === 'admin-root') {
rootDir = testDir
if (process.env.PAYLOAD_TEST_PROD === 'true') {
// If in prod mode, there may be a testSuite/prod folder. If so, use that as the rootDir
const prodDir = path.resolve(testDir, 'prod')
try {
fs.accessSync(prodDir, fs.constants.F_OK)
rootDir = prodDir
} catch (err) {
// Swallow err - no prod folder
}
}
} else {
rootDir = path.resolve(dirname, '..')
}
process.env.ROOT_DIR = rootDir
await generateImportMap(config, { log: true, force: true })
}
}
await run()