-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtesting.ts
102 lines (90 loc) · 3.07 KB
/
testing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import path from 'path';
import crypto from 'crypto';
import fs from 'fs';
import { Server } from 'http';
import express from 'express';
import supertest, { Test } from 'supertest';
import memoizeOne from 'memoize-one';
import type { KeystoneConfig, KeystoneContext } from './types';
import {
getCommittedArtifacts,
writeCommittedArtifacts,
requirePrismaClient,
generateNodeModulesArtifacts,
} from './artifacts';
import { pushPrismaSchemaToDatabase } from './migrations';
import { initConfig, createSystem, createExpressServer } from './system';
export type GraphQLRequest = (arg: {
query: string;
variables?: Record<string, any>;
operationName?: string;
}) => Test;
export type TestArgs = {
context: KeystoneContext;
graphQLRequest: GraphQLRequest;
app: express.Express;
server: Server;
};
export type TestEnv = {
connect: () => Promise<void>;
disconnect: () => Promise<void>;
testArgs: TestArgs;
};
const _hashPrismaSchema = memoizeOne(prismaSchema =>
crypto.createHash('md5').update(prismaSchema).digest('hex')
);
const _alreadyGeneratedProjects = new Set<string>();
export async function setupTestEnv({
config: _config,
}: {
config: KeystoneConfig;
}): Promise<TestEnv> {
// Force the UI to always be disabled.
const config = initConfig({ ..._config, ui: { ..._config.ui, isDisabled: true } });
const { graphQLSchema, getKeystone } = createSystem(config);
const artifacts = await getCommittedArtifacts(graphQLSchema, config);
const hash = _hashPrismaSchema(artifacts.prisma);
const artifactPath = path.resolve('.keystone', 'tests', hash);
if (!_alreadyGeneratedProjects.has(hash)) {
_alreadyGeneratedProjects.add(hash);
fs.mkdirSync(artifactPath, { recursive: true });
await writeCommittedArtifacts(artifacts, artifactPath);
await generateNodeModulesArtifacts(graphQLSchema, config, artifactPath);
}
await pushPrismaSchemaToDatabase(
config.db.url,
artifacts.prisma,
path.join(artifactPath, 'schema.prisma'),
true // shouldDropDatabase
);
const { connect, disconnect, createContext } = getKeystone(requirePrismaClient(artifactPath));
const {
expressServer: app,
apolloServer,
httpServer: server,
} = await createExpressServer(config, graphQLSchema, createContext);
const graphQLRequest: GraphQLRequest = ({ query, variables = undefined, operationName }) =>
supertest(app)
.post(config.graphql?.path || '/api/graphql')
.send({ query, variables, operationName })
.set('Accept', 'application/json');
return {
connect,
disconnect: async () => {
await Promise.all([disconnect(), apolloServer.stop()]);
},
testArgs: { context: createContext(), graphQLRequest, app, server },
};
}
export function setupTestRunner({ config }: { config: KeystoneConfig }) {
return (testFn: (testArgs: TestArgs) => Promise<void>) => async () => {
// Reset the database to be empty for every test.
const { connect, disconnect, testArgs } = await setupTestEnv({ config });
await connect();
try {
return await testFn(testArgs);
} finally {
await disconnect();
}
};
}