diff --git a/.changeset/three-lemons-invent.md b/.changeset/three-lemons-invent.md new file mode 100644 index 00000000000..ad7740ec7f3 --- /dev/null +++ b/.changeset/three-lemons-invent.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/test-utils-legacy': minor +--- + +Added exports for the `TestKeystoneConfig` and `Setup` types. diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index d87be967af6..b6ea51479b3 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -13,37 +13,34 @@ import { requirePrismaClient, generateNodeModulesArtifacts, } from '@keystone-next/keystone/artifacts'; -import type { KeystoneConfig, KeystoneContext } from '@keystone-next/types'; +import type { DatabaseConfig, KeystoneConfig, KeystoneContext } from '@keystone-next/types'; import memoizeOne from 'memoize-one'; -export type ProviderName = 'postgresql' | 'sqlite'; +export type ProviderName = NonNullable; -const hashPrismaSchema = memoizeOne(prismaSchema => +const _hashPrismaSchema = memoizeOne(prismaSchema => crypto.createHash('md5').update(prismaSchema).digest('hex') ); // Users should use testConfig({ ... }) in place of config({ ... }) when setting up // their system for test. We explicitly don't allow them to control the 'db' or 'ui' // properties as we're going to set that up as part of setupFromConfig. -type TestKeystoneConfig = Omit; +export type TestKeystoneConfig = Omit; export const testConfig = (config: TestKeystoneConfig) => config; -const alreadyGeneratedProjects = new Set(); +const _alreadyGeneratedProjects = new Set(); -async function setupFromConfig({ +export async function setupFromConfig({ provider, config: _config, }: { provider: ProviderName; config: TestKeystoneConfig; }) { + const enableLogging = false; // Turn this on if you need verbose debug info const config = initConfig({ ..._config, - db: { - url: process.env.DATABASE_URL!, - provider, - enableLogging: false, // Turn this on if you need verbose debug info - }, + db: { url: process.env.DATABASE_URL!, provider, enableLogging }, ui: { isDisabled: true }, }); @@ -51,13 +48,13 @@ async function setupFromConfig({ const prismaClient = await (async () => { const artifacts = await getCommittedArtifacts(graphQLSchema, config); - const hash = hashPrismaSchema(artifacts.prisma); + const hash = _hashPrismaSchema(artifacts.prisma); if (provider === 'postgresql') { config.db.url = `${config.db.url}?schema=${hash.toString()}`; } const cwd = path.resolve('.api-test-prisma-clients', hash); - if (!alreadyGeneratedProjects.has(hash)) { - alreadyGeneratedProjects.add(hash); + if (!_alreadyGeneratedProjects.has(hash)) { + _alreadyGeneratedProjects.add(hash); fs.mkdirSync(cwd, { recursive: true }); await writeCommittedArtifacts(artifacts, cwd); await generateNodeModulesArtifacts(graphQLSchema, config, cwd); @@ -71,26 +68,15 @@ async function setupFromConfig({ return requirePrismaClient(cwd); })(); - const keystone = getKeystone(prismaClient); - - const app = await createExpressServer( - config, - graphQLSchema, - keystone.createContext, - true, - '', - false - ); - - return { - connect: () => keystone.connect(), - disconnect: () => keystone.disconnect(), - context: keystone.createContext().sudo(), - app, - }; + const { connect, disconnect, createContext } = getKeystone(prismaClient); + + // (config, graphQLSchema, createContext, dev, projectAdminPath, isVerbose) + const app = await createExpressServer(config, graphQLSchema, createContext, true, '', false); + + return { connect, disconnect, context: createContext().sudo(), app }; } -function networkedGraphqlRequest({ +export function networkedGraphqlRequest({ app, query, variables = undefined, @@ -115,12 +101,10 @@ function networkedGraphqlRequest({ expect(res.statusCode).toBe(expectedStatusCode); return { ...JSON.parse(res.text), res }; }) - .catch((error: Error) => ({ - errors: [error], - })); + .catch((error: Error) => ({ errors: [error] })); } -type Setup = { +export type Setup = { connect: () => Promise; disconnect: () => Promise; context: KeystoneContext; @@ -173,7 +157,7 @@ function _after(tearDownFunction: () => Promise | void) { }; } -function multiAdapterRunners(only = process.env.TEST_ADAPTER) { +export function multiAdapterRunners(only = process.env.TEST_ADAPTER) { return (['postgresql', 'sqlite'] as const) .filter(provider => typeof only === 'undefined' || provider === only) .map(provider => ({ @@ -183,5 +167,3 @@ function multiAdapterRunners(only = process.env.TEST_ADAPTER) { after: _after(() => {}), })); } - -export { setupFromConfig, multiAdapterRunners, networkedGraphqlRequest };