chore: rework test hooks to reset tsconfig.json
This commit is contained in:
@@ -40,7 +40,7 @@
|
|||||||
"clean:cache": "rimraf node_modules/.cache && rimraf packages/payload/node_modules/.cache && rimraf .next",
|
"clean:cache": "rimraf node_modules/.cache && rimraf packages/payload/node_modules/.cache && rimraf .next",
|
||||||
"clean:build": "find . \\( -type d \\( -name dist -o -name .cache -o -name .next -o -name .turbo \\) -o -type f -name tsconfig.tsbuildinfo \\) -not -path '*/node_modules/*' -exec rm -rf {} +",
|
"clean:build": "find . \\( -type d \\( -name dist -o -name .cache -o -name .next -o -name .turbo \\) -o -type f -name tsconfig.tsbuildinfo \\) -not -path '*/node_modules/*' -exec rm -rf {} +",
|
||||||
"clean:all": "find . \\( -type d \\( -name node_modules -o -name dist -o -name .cache -o -name .next -o -name .turbo \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} +",
|
"clean:all": "find . \\( -type d \\( -name node_modules -o -name dist -o -name .cache -o -name .next -o -name .turbo \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} +",
|
||||||
"dev": "cross-env node --no-deprecation ./test/dev.js",
|
"dev": "tsx ./test/dev.ts",
|
||||||
"devsafe": "rm -rf .next && cross-env node --no-deprecation ./test/dev.js",
|
"devsafe": "rm -rf .next && cross-env node --no-deprecation ./test/dev.js",
|
||||||
"dev:generate-graphql-schema": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/generateGraphQLSchema.ts",
|
"dev:generate-graphql-schema": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/generateGraphQLSchema.ts",
|
||||||
"dev:generate-types": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/generateTypes.ts",
|
"dev:generate-types": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/generateTypes.ts",
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
||||||
import { promises as __promises, promises as _promises, existsSync, promises } from 'fs'
|
|
||||||
import json5 from 'json5'
|
|
||||||
import { dirname, resolve } from 'path'
|
|
||||||
import { fileURLToPath } from 'url'
|
|
||||||
|
|
||||||
const { readFile } = promises
|
|
||||||
const { writeFile } = _promises
|
|
||||||
const { rm } = __promises
|
|
||||||
const { parse } = json5
|
|
||||||
|
|
||||||
export const beforeTest = async (testSuiteName) => {
|
|
||||||
const __filename = fileURLToPath(import.meta.url)
|
|
||||||
const __dirname = dirname(__filename)
|
|
||||||
|
|
||||||
const testSuite = testSuiteName || '_community'
|
|
||||||
|
|
||||||
console.log('\nUsing config:', testSuite, '\n')
|
|
||||||
|
|
||||||
// Delete next webpack cache
|
|
||||||
const nextWebpackCache = resolve(__dirname, '..', '.next/cache/webpack')
|
|
||||||
if (existsSync(nextWebpackCache)) {
|
|
||||||
await rm(nextWebpackCache, { recursive: true })
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set path.'payload-config' in tsconfig.json
|
|
||||||
const tsConfigPath = resolve(__dirname, '..', 'tsconfig.json')
|
|
||||||
const tsConfig = await parse(await readFile(tsConfigPath, 'utf8'))
|
|
||||||
tsConfig.compilerOptions.paths['@payload-config'] = [`./test/${testSuite}/config.ts`]
|
|
||||||
|
|
||||||
await writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2))
|
|
||||||
|
|
||||||
const PAYLOAD_CONFIG_PATH = resolve(testSuite, 'config')
|
|
||||||
process.env.PAYLOAD_CONFIG_PATH = PAYLOAD_CONFIG_PATH
|
|
||||||
}
|
|
||||||
30
test/dev.js
30
test/dev.js
@@ -1,30 +0,0 @@
|
|||||||
import minimist from 'minimist'
|
|
||||||
import { nextDev } from 'next/dist/cli/next-dev.js'
|
|
||||||
import { dirname, resolve } from 'path'
|
|
||||||
import { fileURLToPath } from 'url'
|
|
||||||
|
|
||||||
import { beforeTest } from './beforeTest.js'
|
|
||||||
|
|
||||||
const _filename = fileURLToPath(import.meta.url)
|
|
||||||
const _dirname = dirname(_filename)
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
|
||||||
process.env.PAYLOAD_DROP_DATABASE = true
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const {
|
|
||||||
_: [testSuiteArg],
|
|
||||||
...args
|
|
||||||
} = minimist(process.argv.slice(2))
|
|
||||||
|
|
||||||
if (args.turbo === true) {
|
|
||||||
process.env.TURBOPACK = '1'
|
|
||||||
}
|
|
||||||
|
|
||||||
process.env.PAYLOAD_DROP_DATABASE = 'true'
|
|
||||||
|
|
||||||
beforeTest(testSuiteArg)
|
|
||||||
|
|
||||||
nextDev({ _: [resolve(_dirname, '..')], port: process.env.PORT || 3000 })
|
|
||||||
}
|
|
||||||
34
test/dev.ts
Normal file
34
test/dev.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import minimist from 'minimist'
|
||||||
|
import { nextDev } from 'next/dist/cli/next-dev.js'
|
||||||
|
import { dirname, resolve } from 'path'
|
||||||
|
import { fileURLToPath } from 'url'
|
||||||
|
|
||||||
|
import { createTestHooks } from './testHooks.js'
|
||||||
|
|
||||||
|
const _filename = fileURLToPath(import.meta.url)
|
||||||
|
const _dirname = dirname(_filename)
|
||||||
|
|
||||||
|
process.env.PAYLOAD_DROP_DATABASE = 'true'
|
||||||
|
|
||||||
|
const {
|
||||||
|
_: [testSuiteArg],
|
||||||
|
...args
|
||||||
|
} = minimist(process.argv.slice(2))
|
||||||
|
|
||||||
|
if (args.turbo === true) {
|
||||||
|
process.env.TURBOPACK = '1'
|
||||||
|
}
|
||||||
|
|
||||||
|
process.env.PAYLOAD_DROP_DATABASE = 'true'
|
||||||
|
|
||||||
|
const { afterTest, beforeTest } = await createTestHooks(testSuiteArg)
|
||||||
|
await beforeTest()
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
await nextDev({ _: [resolve(_dirname, '..')], port: process.env.PORT || 3000 })
|
||||||
|
|
||||||
|
// On cmd+c, clean up
|
||||||
|
process.on('SIGINT', async () => {
|
||||||
|
await afterTest()
|
||||||
|
process.exit(0)
|
||||||
|
})
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { promises as _promises } from 'fs'
|
import { promises as _promises } from 'fs'
|
||||||
import { createServer } from 'http'
|
import { createServer } from 'http'
|
||||||
import nextImport from 'next'
|
import nextImport from 'next'
|
||||||
|
import { startMemoryDB } from 'test/startMemoryDB.js'
|
||||||
import { parse } from 'url'
|
import { parse } from 'url'
|
||||||
|
|
||||||
import type { SanitizedConfig } from '../../packages/payload/src/config/types.js'
|
import type { SanitizedConfig } from '../../packages/payload/src/config/types.js'
|
||||||
@@ -8,7 +9,7 @@ import type { Payload } from '../../packages/payload/src/index.js'
|
|||||||
|
|
||||||
import { getPayloadHMR } from '../../packages/next/src/utilities/getPayloadHMR.js'
|
import { getPayloadHMR } from '../../packages/next/src/utilities/getPayloadHMR.js'
|
||||||
import wait from '../../packages/payload/src/utilities/wait.js'
|
import wait from '../../packages/payload/src/utilities/wait.js'
|
||||||
import { beforeTest } from '../beforeTest.js'
|
import { createTestHooks } from '../testHooks.js'
|
||||||
|
|
||||||
type Args = {
|
type Args = {
|
||||||
config: Promise<SanitizedConfig>
|
config: Promise<SanitizedConfig>
|
||||||
@@ -22,16 +23,18 @@ type Result = {
|
|||||||
|
|
||||||
export async function initPayloadE2E({ config, dirname }: Args): Promise<Result> {
|
export async function initPayloadE2E({ config, dirname }: Args): Promise<Result> {
|
||||||
const testSuiteName = dirname.split('/').pop()
|
const testSuiteName = dirname.split('/').pop()
|
||||||
await beforeTest(testSuiteName)
|
const { beforeTest } = await createTestHooks(testSuiteName)
|
||||||
|
await beforeTest()
|
||||||
|
|
||||||
process.env.NODE_OPTIONS = '--no-deprecation'
|
process.env.NODE_OPTIONS = '--no-deprecation'
|
||||||
process.env.PAYLOAD_DROP_DATABASE = 'true'
|
process.env.PAYLOAD_DROP_DATABASE = 'true'
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
process.env.NODE_ENV = 'test'
|
process.env.NODE_ENV = 'test'
|
||||||
const payload = await getPayloadHMR({ config })
|
|
||||||
|
|
||||||
// const port = await getPort()
|
const configWithMemoryDB = await startMemoryDB(config)
|
||||||
|
const payload = await getPayloadHMR({ config: configWithMemoryDB })
|
||||||
|
|
||||||
const port = 3000
|
const port = 3000
|
||||||
process.env.PORT = String(port)
|
process.env.PORT = String(port)
|
||||||
const serverURL = `http://localhost:${port}`
|
const serverURL = `http://localhost:${port}`
|
||||||
@@ -59,7 +62,7 @@ export async function initPayloadE2E({ config, dirname }: Args): Promise<Result>
|
|||||||
|
|
||||||
await serverPromise
|
await serverPromise
|
||||||
|
|
||||||
await wait(3000)
|
await wait(port)
|
||||||
|
|
||||||
return { payload, serverURL }
|
return { payload, serverURL }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { MongoMemoryReplSet } from 'mongodb-memory-server'
|
|||||||
import type { SanitizedConfig } from '../packages/payload/src/config/types.js'
|
import type { SanitizedConfig } from '../packages/payload/src/config/types.js'
|
||||||
|
|
||||||
import { mongooseAdapter } from '../packages/db-mongodb/src/index.js'
|
import { mongooseAdapter } from '../packages/db-mongodb/src/index.js'
|
||||||
|
import Logger from '../packages/payload/src/utilities/logger.js'
|
||||||
|
|
||||||
export const startMemoryDB = async (
|
export const startMemoryDB = async (
|
||||||
configPromise: Promise<SanitizedConfig>,
|
configPromise: Promise<SanitizedConfig>,
|
||||||
@@ -11,7 +12,8 @@ export const startMemoryDB = async (
|
|||||||
|
|
||||||
process.env.NODE_OPTIONS = '--no-deprecation'
|
process.env.NODE_OPTIONS = '--no-deprecation'
|
||||||
|
|
||||||
console.log('---- CONNECTING TO MEMORY DB ----')
|
const logger = Logger()
|
||||||
|
logger.info('---- CONNECTING TO MEMORY DB ----')
|
||||||
|
|
||||||
switch (process.env.PAYLOAD_DATABASE) {
|
switch (process.env.PAYLOAD_DATABASE) {
|
||||||
case 'postgres':
|
case 'postgres':
|
||||||
|
|||||||
53
test/testHooks.ts
Normal file
53
test/testHooks.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { existsSync, promises } from 'fs'
|
||||||
|
import json5 from 'json5'
|
||||||
|
import { dirname, resolve } from 'path'
|
||||||
|
import { fileURLToPath } from 'url'
|
||||||
|
|
||||||
|
const { readFile, writeFile, rm } = promises
|
||||||
|
const { parse } = json5
|
||||||
|
|
||||||
|
type TestHooks = {
|
||||||
|
afterTest: () => Promise<void>
|
||||||
|
beforeTest: () => Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
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 originalPayloadConfigTsValue =
|
||||||
|
tsConfig.compilerOptions.paths['@payload-config'] ?? './test/_community/config.ts'
|
||||||
|
|
||||||
|
return {
|
||||||
|
/**
|
||||||
|
* Clear next webpack cache and set '@payload-config' path in tsconfig.json
|
||||||
|
*/
|
||||||
|
beforeTest: async () => {
|
||||||
|
// Delete next webpack cache
|
||||||
|
const nextWebpackCache = resolve(__dirname, '..', '.next/cache/webpack')
|
||||||
|
if (existsSync(nextWebpackCache)) {
|
||||||
|
await rm(nextWebpackCache, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set '@payload-config' in tsconfig.json
|
||||||
|
tsConfig.compilerOptions.paths['@payload-config'] = [`./test/${testSuiteName}/config.ts`]
|
||||||
|
await writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2))
|
||||||
|
|
||||||
|
const PAYLOAD_CONFIG_PATH = resolve(testSuiteName, 'config')
|
||||||
|
process.env.PAYLOAD_CONFIG_PATH = PAYLOAD_CONFIG_PATH
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Reset the changes made to tsconfig.json
|
||||||
|
*/
|
||||||
|
afterTest: async () => {
|
||||||
|
// Revert the changes made to tsconfig.json
|
||||||
|
tsConfig.compilerOptions.paths['@payload-config'] = originalPayloadConfigTsValue
|
||||||
|
|
||||||
|
await writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2) + '\n')
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user