Skip to content

Commit

Permalink
Convert API test to typescript (#5098)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 11, 2021
1 parent 40d4fff commit e2edaaf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-paws-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/test-utils-legacy': minor
---

Added a function `testConfig` to be used when setting up a system under test.
30 changes: 18 additions & 12 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,39 @@ const argGenerator = {
}),
};

// 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<KeystoneConfig, 'db' | 'ui'>;
export const testConfig = (config: TestKeystoneConfig) => config;

async function setupFromConfig({
adapterName,
config,
}: {
adapterName: AdapterName;
config: KeystoneConfig;
config: TestKeystoneConfig;
}) {
let db: KeystoneConfig['db'];
if (adapterName === 'knex') {
const adapterArgs = await argGenerator[adapterName]();
config.db = { adapter: adapterName, url: adapterArgs.knexOptions.connection, ...adapterArgs };
db = { adapter: adapterName, url: adapterArgs.knexOptions.connection, ...adapterArgs };
} else if (adapterName === 'mongoose') {
const adapterArgs = await argGenerator[adapterName]();
config.db = { adapter: adapterName, url: adapterArgs.mongoUri, mongooseOptions: adapterArgs };
db = { adapter: adapterName, url: adapterArgs.mongoUri, mongooseOptions: adapterArgs };
} else if (adapterName === 'prisma_postgresql') {
const adapterArgs = await argGenerator[adapterName]();
config.db = { adapter: adapterName, ...adapterArgs };
db = { adapter: adapterName, ...adapterArgs };
}
config.ui = { isDisabled: true };
config = initConfig(config);
const _config = initConfig({ ...config, db: db!, ui: { isDisabled: true } });

const { keystone, createContext, graphQLSchema } = createSystem(
config,
_config,
path.resolve('.keystone'),
'prototype'
);

const app = await createExpressServer(config, graphQLSchema, createContext, true, '', false);
const app = await createExpressServer(_config, graphQLSchema, createContext, true, '', false);

return { keystone, context: createContext().sudo(), app };
}
Expand All @@ -88,7 +94,7 @@ async function setupServer({
keystoneOptions,
graphqlOptions = {},
}: {
adapterName: 'mongoose' | 'knex' | 'prisma_postgresql';
adapterName: AdapterName;
schemaName: string;
schemaNames: string[];
createLists: (args: Keystone<string>) => void;
Expand Down Expand Up @@ -254,19 +260,19 @@ function multiAdapterRunners(only = process.env.TEST_ADAPTER) {
return [
{
runner: _keystoneRunner('mongoose', teardownMongoMemoryServer),
adapterName: 'mongoose',
adapterName: 'mongoose' as const,
before: _before('mongoose'),
after: _after(teardownMongoMemoryServer),
},
{
runner: _keystoneRunner('knex', () => {}),
adapterName: 'knex',
adapterName: 'knex' as const,
before: _before('knex'),
after: _after(() => {}),
},
{
runner: _keystoneRunner('prisma_postgresql', () => {}),
adapterName: 'prisma_postgresql',
adapterName: 'prisma_postgresql' as const,
before: _before('prisma_postgresql'),
after: _after(() => {}),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { multiAdapterRunners, setupFromConfig } = require('@keystone-next/test-utils-legacy');
import { multiAdapterRunners, setupFromConfig, testConfig } from '@keystone-next/test-utils-legacy';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { text } from '@keystone-next/fields';
import type { AdapterName } from '@keystone-next/test-utils-legacy';

function setupKeystone(adapterName) {
function setupKeystone(adapterName: AdapterName) {
return setupFromConfig({
adapterName,
config: createSchema({ lists: { User: list({ fields: { name: text() } }) } }),
config: testConfig({ lists: createSchema({ User: list({ fields: { name: text() } }) }) }),
});
}

Expand Down

1 comment on commit e2edaaf

@vercel
Copy link

@vercel vercel bot commented on e2edaaf Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.