Files
payloadcms/test/testHooks.ts
Alessio Gravili f5c13deb24 build: fix tsconfig monorepo setup (#10028)
Should fix messed up import suggestions and simplifies all tsconfigs
through inheritance.

One main issue was that packages were inheriting `baseURL: "."` from the
root tsconfig. This caused incorrect import suggestions that start with
"packages/...".

This PR ensures that packages do not inherit this baseURL: "." property,
while ensuring the root, non-inherited tsconfig still keeps it to get
tests to work (the importMap needs it)
2024-12-17 14:49:29 -05:00

47 lines
1.5 KiB
TypeScript

import { parse, stringify } from 'comment-json'
import { existsSync, promises } from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { getNextRootDir } from './helpers/getNextRootDir.js'
const { readFile, writeFile, rm } = promises
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
export const createTestHooks = async (testSuiteName = '_community') => {
const rootDir = getNextRootDir().rootDir
const tsConfigBasePath = path.resolve(rootDir, './tsconfig.base.json')
const tsConfigPath = existsSync(tsConfigBasePath)
? tsConfigBasePath
: path.resolve(rootDir, './tsconfig.json')
const tsConfigContent = await readFile(tsConfigPath, 'utf8')
const tsConfig = parse(tsConfigContent)
return {
/**
* Clear next webpack cache and set '@payload-config' path in tsconfig.json
*/
beforeTest: async () => {
// Delete entire .next cache folder
const nextCache = path.resolve(rootDir, './.next')
if (existsSync(nextCache)) {
await rm(nextCache, { recursive: true })
}
// Set '@payload-config' in tsconfig.json
// @ts-expect-error
tsConfig.compilerOptions.paths['@payload-config'] = [
process.env.PAYLOAD_TEST_PROD === 'true'
? `./${testSuiteName}/config.ts`
: `./test/${testSuiteName}/config.ts`,
]
await writeFile(tsConfigPath, stringify(tsConfig, null, 2) + '\n')
process.env.PAYLOAD_CONFIG_PATH = path.resolve(dirname, testSuiteName, 'config.ts')
},
}
}