chore: uses globalSetup for starting memory db

This commit is contained in:
James
2024-04-02 09:44:55 -04:00
parent 34fe6182c8
commit 73a555788d
10 changed files with 73 additions and 51 deletions

View File

@@ -6,7 +6,7 @@ import { fileURLToPath } from 'url'
import { initPageConsoleErrorCatch } from '../helpers.js'
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
import { initPayloadE2E } from '../helpers/initPayloadE2E.js'
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
@@ -16,7 +16,7 @@ test.describe('Admin Panel', () => {
let url: AdminUrlUtil
test.beforeAll(async ({ browser }) => {
const { payload, serverURL } = await initPayloadE2E({ dirname })
const { payload, serverURL } = await initPayloadE2ENoConfig({ dirname })
url = new AdminUrlUtil(serverURL, 'posts')
const context = await browser.newContext()

View File

@@ -29,14 +29,20 @@ import {
import { type Config, buildConfig } from 'payload/config'
import sharp from 'sharp'
import { getDBAdapter } from './getDBAdapter.js'
import { reInitEndpoint } from './helpers/reInit.js'
import { localAPIEndpoint } from './helpers/sdk/endpoint.js'
// process.env.PAYLOAD_DATABASE = 'postgres'
export async function buildConfigWithDefaults(
testConfig?: Partial<Config>,
): Promise<SanitizedConfig> {
console.log('MEMORY URI', process.env.MONGODB_MEMORY_SERVER_URI)
const databaseAdapters = {
mongodb: mongooseAdapter({
url: process.env.DATABASE_URI || 'mongodb://127.0.0.1/payloadtests',
url:
process.env.MONGODB_MEMORY_SERVER_URI ||
process.env.DATABASE_URI ||
'mongodb://127.0.0.1/payloadtests',
}),
postgres: postgresAdapter({
pool: {
@@ -63,13 +69,8 @@ const databaseAdapters = {
}),
}
export async function buildConfigWithDefaults(
testConfig?: Partial<Config>,
): Promise<SanitizedConfig> {
const adapter = await getDBAdapter()
const config: Config = {
db: adapter || databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongodb'],
db: databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongodb'],
secret: 'TEST_SECRET',
//editor: slateEditor({}),
// editor: slateEditor({

View File

@@ -18,11 +18,6 @@ type Result = {
}
export async function initPayloadE2E({ dirname }: Args): Promise<Result> {
// @ts-expect-error
process.env.NODE_ENV = 'test'
process.env.NODE_OPTIONS = '--no-deprecation'
process.env.PAYLOAD_DROP_DATABASE = 'true'
const testSuiteName = dirname.split('/').pop()
const { beforeTest } = await createTestHooks(testSuiteName)
await beforeTest()

View File

@@ -21,11 +21,6 @@ type Result<T extends GeneratedTypes<T>> = {
export async function initPayloadE2ENoConfig<T extends GeneratedTypes<T>>({
dirname,
}: Args): Promise<Result<T>> {
// @ts-expect-error
process.env.NODE_ENV = 'test'
process.env.NODE_OPTIONS = '--no-deprecation'
process.env.PAYLOAD_DROP_DATABASE = 'true'
const testSuiteName = dirname.split('/').pop()
const { beforeTest } = await createTestHooks(testSuiteName)
await beforeTest()

View File

@@ -10,11 +10,6 @@ import { NextRESTClient } from './NextRESTClient.js'
export async function initPayloadInt(
config: Promise<SanitizedConfig>,
): Promise<{ config: SanitizedConfig; payload: Payload; restClient: NextRESTClient }> {
// @ts-expect-error
process.env.NODE_ENV = 'test'
process.env.NODE_OPTIONS = '--no-deprecation'
process.env.PAYLOAD_DROP_DATABASE = 'true'
const payload = await getPayload({ config })
const restClient = new NextRESTClient(payload.config)

View File

@@ -1,3 +1,9 @@
import path from 'path'
import { fileURLToPath } from 'url'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
/** @type {import('jest').Config} */
const customJestConfig = {
extensionsToTreatAsEsm: ['.ts', '.tsx'],
@@ -10,6 +16,7 @@ const customJestConfig = {
},
reporters: ['default', ['github-actions', { silent: false }], 'summary'],
testEnvironment: 'node',
globalSetup: path.resolve(dirname, 'setup.ts'),
testMatch: ['<rootDir>/**/*int.spec.ts'],
testTimeout: 90000,
transform: {

View File

@@ -1,4 +1,9 @@
import { defineConfig } from '@playwright/test'
import path from 'path'
import { fileURLToPath } from 'url'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
export const EXPECT_TIMEOUT = 45000
export const POLL_TOPASS_TIMEOUT = EXPECT_TIMEOUT * 4 // That way expect.poll() or expect().toPass can retry 4 times. 4x higher than default expect timeout => can retry 4 times if retryable expects are used inside
@@ -13,6 +18,7 @@ export default defineConfig({
trace: 'retain-on-failure',
video: 'retain-on-failure',
},
globalSetup: path.resolve(dirname, 'setup.js'),
expect: {
timeout: EXPECT_TIMEOUT,
},

23
test/setup.js Normal file
View File

@@ -0,0 +1,23 @@
import { MongoMemoryReplSet } from 'mongodb-memory-server'
// eslint-disable-next-line no-restricted-exports
export default async () => {
// @ts-expect-error
process.env.NODE_ENV = 'test'
process.env.PAYLOAD_DROP_DATABASE = 'true'
process.env.NODE_OPTIONS = '--no-deprecation'
if (
(!process.env.PAYLOAD_DATABASE || process.env.PAYLOAD_DATABASE === 'mongoose') &&
!global._mongoMemoryServer
) {
global._mongoMemoryServer = await MongoMemoryReplSet.create({
replSet: {
count: 3,
dbName: 'payloadmemory',
},
})
process.env.MONGODB_MEMORY_SERVER_URI = global._mongoMemoryServer.getUri()
}
}

View File

@@ -41,6 +41,6 @@
}
},
"exclude": ["dist", "build", "node_modules", ".eslintrc.js", "dist/**/*.js", "**/dist/**/*.js"],
"include": ["./**/*.ts", ".next/types/**/*.ts"],
"include": ["./**/*.ts", ".next/types/**/*.ts", "setup.js"],
"references": []
}