Skip to content

Commit

Permalink
Remove BaseKeystone from test-utils APIs (#5694)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored May 13, 2021
1 parent 62e68c8 commit b1baeab
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-needles-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/test-utils-legacy': major
---

The `Setup` type, returned by `setupFromConfig` and passed into test functions in `multiAdapterRunners` now has `connect` and `disconnect` functions, rather than a `keystone` object.
38 changes: 22 additions & 16 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
requirePrismaClient,
generateNodeModulesArtifacts,
} from '@keystone-next/keystone/artifacts';
import type { KeystoneConfig, BaseKeystone, KeystoneContext } from '@keystone-next/types';
import type { KeystoneConfig, KeystoneContext } from '@keystone-next/types';
import memoizeOne from 'memoize-one';

export type ProviderName = 'postgresql' | 'sqlite';
Expand Down Expand Up @@ -74,7 +74,12 @@ async function setupFromConfig({

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

return { keystone, context: createContext().sudo(), app };
return {
connect: () => keystone.connect(),
disconnect: () => keystone.disconnect(),
context: createContext().sudo(),
app,
};
}

function networkedGraphqlRequest({
Expand Down Expand Up @@ -107,7 +112,12 @@ function networkedGraphqlRequest({
}));
}

type Setup = { keystone: BaseKeystone; context: KeystoneContext; app: express.Application };
type Setup = {
connect: () => Promise<void>;
disconnect: () => Promise<void>;
context: KeystoneContext;
app: express.Application;
};

function _keystoneRunner(provider: ProviderName, tearDownFunction: () => Promise<void> | void) {
return function (
Expand All @@ -127,34 +137,30 @@ function _keystoneRunner(provider: ProviderName, tearDownFunction: () => Promise
return;
}
const setup = await setupKeystoneFn(provider);
const { keystone } = setup;
await keystone.connect();
const { connect, disconnect } = setup;
await connect();

try {
await testFn(setup);
} finally {
await keystone.disconnect();
await disconnect();
await tearDownFunction();
}
};
};
}

function _before(provider: ProviderName) {
return async function (
setupKeystone: (
provider: ProviderName
) => Promise<{ keystone: BaseKeystone; app: any; context: any }>
) {
const { keystone, context, app } = await setupKeystone(provider);
await keystone.connect();
return { keystone, context, app };
return async function (setupKeystone: (provider: ProviderName) => Promise<Setup>) {
const setup = await setupKeystone(provider);
await setup.connect();
return setup;
};
}

function _after(tearDownFunction: () => Promise<void> | void) {
return async function (keystone: BaseKeystone) {
await keystone.disconnect();
return async function (disconnect: () => Promise<void>) {
await disconnect();
await tearDownFunction();
};
}
Expand Down
6 changes: 3 additions & 3 deletions tests/api-tests/access-control/authed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ const expectNamedArray = <T extends { id: IdType }, N extends string>(

multiAdapterRunners().map(({ before, after, provider }) =>
describe(`Provider: ${provider}`, () => {
let keystone: any,
let disconnect: any,
items: Record<string, { id: IdType; name: string }[]>,
user: { id: IdType; name: string; yesRead: string; noRead: string },
context: KeystoneContext;
beforeAll(async () => {
const _before = await before(setupKeystone);
keystone = _before.keystone;
disconnect = _before.disconnect;
context = _before.context;

// ensure every list has at least some data
Expand All @@ -78,7 +78,7 @@ multiAdapterRunners().map(({ before, after, provider }) =>
})) as { id: IdType; name: string; yesRead: string; noRead: string };
});
afterAll(async () => {
await after(keystone);
await after(disconnect);
});

describe('create', () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/api-tests/access-control/not-authed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ type IdType = any;

multiAdapterRunners().map(({ before, after, provider }) =>
describe(`Provider: ${provider}`, () => {
let keystone: any,
let disconnect: any,
items: Record<string, { id: IdType; name: string }[]>,
context: KeystoneContext;
beforeAll(async () => {
const _before = await before(setupKeystone);
keystone = _before.keystone;
disconnect = _before.disconnect;
context = _before.context;

// ensure every list has at least some data
Expand All @@ -58,7 +58,7 @@ multiAdapterRunners().map(({ before, after, provider }) =>
}
});
afterAll(async () => {
await after(keystone);
await after(disconnect);
});

describe('create', () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/api-tests/access-control/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const imperativeList = getImperativeListName({

multiAdapterRunners().map(({ before, after, provider }) =>
describe(`Provider: ${provider}`, () => {
let keystone: any,
let disconnect: any,
queries: string[],
mutations: string[],
types: string[],
Expand All @@ -56,7 +56,7 @@ multiAdapterRunners().map(({ before, after, provider }) =>
context: KeystoneContext;
beforeAll(async () => {
const _before = await before(setupKeystone);
keystone = _before.keystone;
disconnect = _before.disconnect;
context = _before.context;

const data = await context.exitSudo().graphql.run({ query: introspectionQuery });
Expand All @@ -81,7 +81,7 @@ multiAdapterRunners().map(({ before, after, provider }) =>
);
});
afterAll(async () => {
await after(keystone);
await after(disconnect);
});

describe('static', () => {
Expand Down
3 changes: 1 addition & 2 deletions tests/api-tests/fields/unique.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import globby from 'globby';
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 { BaseKeystone } from '@keystone-next/types';

const testModules = globby.sync(`{packages,packages-next}/**/src/**/test-fixtures.{js,ts}`, {
absolute: true,
Expand Down Expand Up @@ -170,7 +169,7 @@ multiAdapterRunners().map(({ runner, provider, after }) =>
expect(error.message).toMatch('isUnique is not a supported option for field type');
erroredOut = true;
} finally {
after({ disconnect: async () => {} } as BaseKeystone);
after(async () => {});
}
expect(erroredOut).toEqual(true);
});
Expand Down
3 changes: 1 addition & 2 deletions tests/api-tests/fields/unsupported.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from 'path';
import globby from 'globby';
import { multiAdapterRunners, setupFromConfig, testConfig } from '@keystone-next/test-utils-legacy';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { BaseKeystone } from '@keystone-next/types';

const testModules = globby.sync(`{packages,packages-next}/**/src/**/test-fixtures.{js,ts}`, {
absolute: true,
Expand Down Expand Up @@ -40,7 +39,7 @@ multiAdapterRunners().map(({ provider, after }) => {
await mod.afterAll();
}
// We expect setup to fail, so disconnect can be a noop
await after({ disconnect: async () => {} } as BaseKeystone);
await after(async () => {});
});

test('Throws', async () => {
Expand Down

1 comment on commit b1baeab

@vercel
Copy link

@vercel vercel bot commented on b1baeab May 13, 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.