-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
setupTestSuiteClient.ts
100 lines (90 loc) · 3.45 KB
/
setupTestSuiteClient.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
import { getConfig, parseEnvValue } from '@prisma/internals'
import path from 'path'
import { generateClient } from '../../../src/generation/generateClient'
import { getDMMF } from '../../../src/generation/getDMMF'
import type { NamedTestSuiteConfig } from './getTestSuiteInfo'
import {
getTestSuiteFolderPath,
getTestSuitePreviewFeatures,
getTestSuiteSchema,
getTestSuiteSchemaPath,
} from './getTestSuiteInfo'
import { DatasourceInfo, setupTestSuiteDatabase, setupTestSuiteFiles, setupTestSuiteSchema } from './setupTestSuiteEnv'
import type { TestSuiteMeta } from './setupTestSuiteMatrix'
import { AlterStatementCallback, ClientMeta, ClientRuntime } from './types'
/**
* Does the necessary setup to get a test suite client ready to run.
* @param suiteMeta
* @param suiteConfig
* @returns loaded client module
*/
export async function setupTestSuiteClient({
suiteMeta,
suiteConfig,
skipDb,
datasourceInfo,
clientMeta,
alterStatementCallback,
}: {
suiteMeta: TestSuiteMeta
suiteConfig: NamedTestSuiteConfig
skipDb?: boolean
datasourceInfo: DatasourceInfo
clientMeta: ClientMeta
alterStatementCallback?: AlterStatementCallback
}) {
const suiteFolderPath = getTestSuiteFolderPath(suiteMeta, suiteConfig)
const previewFeatures = getTestSuitePreviewFeatures(suiteConfig.matrixOptions)
const schema = getTestSuiteSchema(suiteMeta, suiteConfig.matrixOptions)
const dmmf = await getDMMF({ datamodel: schema, previewFeatures })
const config = await getConfig({ datamodel: schema, ignoreEnvVarErrors: true })
const generator = config.generators.find((g) => parseEnvValue(g.provider) === 'prisma-client-js')
await setupTestSuiteFiles(suiteMeta, suiteConfig)
await setupTestSuiteSchema(suiteMeta, suiteConfig, schema)
if (!skipDb) {
process.env[datasourceInfo.envVarName] = datasourceInfo.databaseUrl
await setupTestSuiteDatabase(suiteMeta, suiteConfig, [], alterStatementCallback)
}
process.env[datasourceInfo.envVarName] = datasourceInfo.dataProxyUrl ?? datasourceInfo.databaseUrl
await generateClient({
datamodel: schema,
schemaPath: getTestSuiteSchemaPath(suiteMeta, suiteConfig),
binaryPaths: { libqueryEngine: {}, queryEngine: {} },
datasources: config.datasources,
outputDir: path.join(suiteFolderPath, 'node_modules/@prisma/client'),
copyRuntime: false,
dmmf: dmmf,
generator: generator,
engineVersion: '0000000000000000000000000000000000000000',
clientVersion: '0.0.0',
transpile: false,
testMode: true,
activeProvider: suiteConfig.matrixOptions['provider'] as string,
// Change \\ to / for windows support
runtimeDirs: {
node: [__dirname.replace(/\\/g, '/'), '..', '..', '..', 'runtime'].join('/'),
edge: [__dirname.replace(/\\/g, '/'), '..', '..', '..', 'runtime'].join('/'),
},
projectRoot: suiteFolderPath,
dataProxy: clientMeta.dataProxy,
})
const clientPathForRuntime: Record<ClientRuntime, string> = {
node: 'node_modules/@prisma/client',
edge: 'node_modules/@prisma/client/edge',
}
return require(path.join(suiteFolderPath, clientPathForRuntime[clientMeta.runtime]))
}
/**
* Get `ClientMeta` from the environment variables
*/
export function getClientMeta(): ClientMeta {
const dataProxy = Boolean(process.env.DATA_PROXY)
const edge = Boolean(process.env.TEST_DATA_PROXY_EDGE_CLIENT)
if (edge && !dataProxy) {
throw new Error('Edge client requires Data Proxy')
}
return {
dataProxy,
runtime: edge ? 'edge' : 'node',
}
}