test: refine testHooks

This commit is contained in:
Elliot DeNolf
2024-03-16 06:59:56 -04:00
parent ba3b2ea66f
commit 4eba651c3d
2 changed files with 9 additions and 11 deletions

View File

@@ -1,10 +1,11 @@
import { existsSync, promises } from 'fs'
import json5 from 'json5'
import { dirname, resolve } from 'path'
import path from 'path'
import { fileURLToPath } from 'url'
const { readFile, writeFile, rm } = promises
const { parse } = json5
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
type TestHooks = {
afterTest: () => Promise<void>
@@ -12,13 +13,10 @@ type TestHooks = {
}
export const createTestHooks = async (testSuiteName = '_community'): Promise<TestHooks> => {
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
console.log('\nUsing config:', testSuiteName, '\n')
const tsConfigPath = resolve(__dirname, '..', 'tsconfig.json')
const tsConfig = await parse(await readFile(tsConfigPath, 'utf8'))
const tsConfigPath = path.resolve(dirname, '..', 'tsconfig.json')
const tsConfig = await json5.parse(await readFile(tsConfigPath, 'utf8'))
const originalPayloadConfigTsValue =
tsConfig.compilerOptions.paths['@payload-config'] ?? './test/_community/config.ts'
@@ -28,7 +26,7 @@ export const createTestHooks = async (testSuiteName = '_community'): Promise<Tes
*/
beforeTest: async () => {
// Delete next webpack cache
const nextWebpackCache = resolve(__dirname, '..', '.next/cache/webpack')
const nextWebpackCache = path.resolve(dirname, '..', '.next/cache/webpack')
if (existsSync(nextWebpackCache)) {
await rm(nextWebpackCache, { recursive: true })
}
@@ -37,7 +35,7 @@ export const createTestHooks = async (testSuiteName = '_community'): Promise<Tes
tsConfig.compilerOptions.paths['@payload-config'] = [`./test/${testSuiteName}/config.ts`]
await writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2))
const PAYLOAD_CONFIG_PATH = resolve(testSuiteName, 'config')
const PAYLOAD_CONFIG_PATH = path.resolve(testSuiteName, 'config')
process.env.PAYLOAD_CONFIG_PATH = PAYLOAD_CONFIG_PATH
},
/**

View File

@@ -88,7 +88,7 @@
"./packages/graphql/src"
],
"@payload-config": [
"./test/admin/config.ts"
"./test/_community/config.ts"
]
}
},
@@ -165,4 +165,4 @@
"app/**/*.tsx",
"scripts/**/*.ts"
]
}
}