Skip to content

Commit

Permalink
Cleanup the test-utils package code (#5883)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Jun 9, 2021
1 parent 12d2c02 commit cf8825b
Show file tree
Hide file tree
Showing 55 changed files with 226 additions and 326 deletions.
6 changes: 6 additions & 0 deletions .changeset/three-lemons-invent.md
@@ -0,0 +1,6 @@
---
'@keystone-next/test-utils-legacy': major
---

Removed the `ProviderName` type, which is identical to the `DatabaseProvider` type in `@keystone-next/types`.
Added exports for the `TestKeystoneConfig` and `Setup` types.
11 changes: 3 additions & 8 deletions examples-staging/ecommerce/tests/mutations.test.ts
@@ -1,13 +1,8 @@
import { KeystoneContext } from '@keystone-next/types';
import {
multiAdapterRunners,
setupFromConfig,
ProviderName,
testConfig,
} from '@keystone-next/test-utils-legacy';
import { DatabaseProvider, KeystoneContext } from '@keystone-next/types';
import { multiAdapterRunners, setupFromConfig, testConfig } from '@keystone-next/test-utils-legacy';
import config from '../keystone';

function setupKeystone(provider: ProviderName) {
function setupKeystone(provider: DatabaseProvider) {
return setupFromConfig({
provider,
config: testConfig({
Expand Down
4 changes: 2 additions & 2 deletions packages-next/cloudinary/src/test-fixtures.skip.ts
Expand Up @@ -4,7 +4,7 @@ import mime from 'mime';
import { Upload } from 'graphql-upload';
import cloudinary from 'cloudinary';
import { text } from '@keystone-next/fields';
import { ProviderName } from '@keystone-next/test-utils-legacy';
import { DatabaseProvider } from '@keystone-next/types';
import { cloudinaryImage } from './index';

const path = require('path');
Expand Down Expand Up @@ -83,7 +83,7 @@ export const storedValues = () => [
{ image: null, name: 'file6' },
];

export const supportedFilters = (provider: ProviderName) => [
export const supportedFilters = (provider: DatabaseProvider) => [
'null_equality',
!['postgresql', 'sqlite'].includes(provider) && 'in_empty_null',
];
4 changes: 2 additions & 2 deletions packages-next/fields/src/types/decimal/tests/test-fixtures.ts
@@ -1,4 +1,4 @@
import { ProviderName } from '@keystone-next/test-utils-legacy';
import { DatabaseProvider } from '@keystone-next/types';
import { decimal } from '..';
import { text } from '../../text';

Expand Down Expand Up @@ -37,7 +37,7 @@ export const storedValues = () => [
{ name: 'price7', price: null },
];

export const supportedFilters = (provider: ProviderName) => [
export const supportedFilters = (provider: DatabaseProvider) => [
'null_equality',
'equality',
'ordering',
Expand Down
4 changes: 2 additions & 2 deletions packages-next/fields/src/types/text/tests/test-fixtures.ts
@@ -1,4 +1,4 @@
import { ProviderName } from '@keystone-next/test-utils-legacy';
import { DatabaseProvider } from '@keystone-next/types';
import { text } from '..';

export const name = 'Text';
Expand Down Expand Up @@ -32,7 +32,7 @@ export const storedValues = () => [
{ name: 'g', testField: null },
];

export const supportedFilters = (provider: ProviderName) => [
export const supportedFilters = (provider: DatabaseProvider) => [
'null_equality',
'equality',
provider !== 'sqlite' && 'equality_case_insensitive',
Expand Down
105 changes: 53 additions & 52 deletions packages-next/fields/src/types/timestamp/tests/test-fixtures.ts
@@ -1,5 +1,4 @@
import { ProviderName } from '@keystone-next/test-utils-legacy';
import { KeystoneContext } from '@keystone-next/types';
import { DatabaseProvider, KeystoneContext } from '@keystone-next/types';
import { text } from '../../text';
import { timestamp } from '..';

Expand Down Expand Up @@ -55,61 +54,63 @@ export const filterTests = (withKeystone: (args: any) => any) => {

test(
'Ordering: orderBy: { lastOnline: asc }',
withKeystone(({ context, provider }: { context: KeystoneContext; provider: ProviderName }) =>
match(
context,
undefined,
provider === 'sqlite'
? [
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
]
: [
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
],
{ lastOnline: 'asc' }
)
withKeystone(
({ context, provider }: { context: KeystoneContext; provider: DatabaseProvider }) =>
match(
context,
undefined,
provider === 'sqlite'
? [
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
]
: [
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
],
{ lastOnline: 'asc' }
)
)
);

test(
'Ordering: orderBy: { lastOnline: desc }',
withKeystone(({ context, provider }: { context: KeystoneContext; provider: ProviderName }) =>
match(
context,
undefined,
provider === 'sqlite'
? [
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
]
: [
{ name: 'person7', lastOnline: null },
{ name: 'person6', lastOnline: null },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
],
{ lastOnline: 'desc' }
)
withKeystone(
({ context, provider }: { context: KeystoneContext; provider: DatabaseProvider }) =>
match(
context,
undefined,
provider === 'sqlite'
? [
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
]
: [
{ name: 'person7', lastOnline: null },
{ name: 'person6', lastOnline: null },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
],
{ lastOnline: 'desc' }
)
)
);
};
70 changes: 25 additions & 45 deletions packages/test-utils/src/index.ts
Expand Up @@ -13,51 +13,46 @@ import {
requirePrismaClient,
generateNodeModulesArtifacts,
} from '@keystone-next/keystone/artifacts';
import type { KeystoneConfig, KeystoneContext } from '@keystone-next/types';
import type { DatabaseProvider, KeystoneConfig, KeystoneContext } from '@keystone-next/types';
import memoizeOne from 'memoize-one';

export type ProviderName = 'postgresql' | 'sqlite';

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

const alreadyGeneratedProjects = new Set<string>();
const _alreadyGeneratedProjects = new Set<string>();

async function setupFromConfig({
export async function setupFromConfig({
provider,
config: _config,
}: {
provider: ProviderName;
provider: DatabaseProvider;
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 },
});

const { graphQLSchema, getKeystone } = createSystem(config);

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);
Expand All @@ -71,26 +66,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,
Expand All @@ -115,21 +99,19 @@ 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<void>;
disconnect: () => Promise<void>;
context: KeystoneContext;
app: express.Application;
};

function _keystoneRunner(provider: ProviderName, tearDownFunction: () => Promise<void> | void) {
function _keystoneRunner(provider: DatabaseProvider, tearDownFunction: () => Promise<void> | void) {
return function (
setupKeystoneFn: (provider: ProviderName) => Promise<Setup>,
setupKeystoneFn: (provider: DatabaseProvider) => Promise<Setup>,
testFn?: (setup: Setup) => Promise<void>
) {
return async function () {
Expand Down Expand Up @@ -158,8 +140,8 @@ function _keystoneRunner(provider: ProviderName, tearDownFunction: () => Promise
};
}

function _before(provider: ProviderName) {
return async function (setupKeystone: (provider: ProviderName) => Promise<Setup>) {
function _before(provider: DatabaseProvider) {
return async function (setupKeystone: (provider: DatabaseProvider) => Promise<Setup>) {
const setup = await setupKeystone(provider);
await setup.connect();
return setup;
Expand All @@ -173,7 +155,7 @@ function _after(tearDownFunction: () => Promise<void> | 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 => ({
Expand All @@ -183,5 +165,3 @@ function multiAdapterRunners(only = process.env.TEST_ADAPTER) {
after: _after(() => {}),
}));
}

export { setupFromConfig, multiAdapterRunners, networkedGraphqlRequest };
5 changes: 3 additions & 2 deletions tests/api-tests/access-control/mutations-field-static.test.ts
@@ -1,9 +1,10 @@
import { ProviderName, testConfig } from '@keystone-next/test-utils-legacy';
import { testConfig } from '@keystone-next/test-utils-legacy';
import { text } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { multiAdapterRunners, setupFromConfig } from '@keystone-next/test-utils-legacy';
import { DatabaseProvider } from '@keystone-next/types';

function setupKeystone(provider: ProviderName) {
function setupKeystone(provider: DatabaseProvider) {
return setupFromConfig({
provider,
config: testConfig({
Expand Down
@@ -1,9 +1,10 @@
import { ProviderName, testConfig } from '@keystone-next/test-utils-legacy';
import { testConfig } from '@keystone-next/test-utils-legacy';
import { text } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { multiAdapterRunners, setupFromConfig } from '@keystone-next/test-utils-legacy';
import { DatabaseProvider } from '@keystone-next/types';

function setupKeystone(provider: ProviderName) {
function setupKeystone(provider: DatabaseProvider) {
return setupFromConfig({
provider,
config: testConfig({
Expand Down
5 changes: 3 additions & 2 deletions tests/api-tests/access-control/mutations-list-static.test.ts
@@ -1,9 +1,10 @@
import { ProviderName, testConfig } from '@keystone-next/test-utils-legacy';
import { testConfig } from '@keystone-next/test-utils-legacy';
import { text } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { multiAdapterRunners, setupFromConfig } from '@keystone-next/test-utils-legacy';
import { DatabaseProvider } from '@keystone-next/types';

function setupKeystone(provider: ProviderName) {
function setupKeystone(provider: DatabaseProvider) {
return setupFromConfig({
provider,
config: testConfig({
Expand Down

1 comment on commit cf8825b

@vercel
Copy link

@vercel vercel bot commented on cf8825b Jun 9, 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.