-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move testing package into main package (#6361)
- Loading branch information
Showing
76 changed files
with
181 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@keystone-next/testing': major | ||
'@keystone-next/keystone': minor | ||
--- | ||
|
||
Moved exports of `@keystone-next/testing` to `@keystone-next/keystone/testing` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import path from 'path'; | ||
import crypto from 'crypto'; | ||
import fs from 'fs'; | ||
import express from 'express'; | ||
import supertest, { Test } from 'supertest'; | ||
import memoizeOne from 'memoize-one'; | ||
import type { KeystoneConfig, KeystoneContext } from '@keystone-next/types'; | ||
import { | ||
getCommittedArtifacts, | ||
writeCommittedArtifacts, | ||
requirePrismaClient, | ||
generateNodeModulesArtifacts, | ||
} from './artifacts'; | ||
import { pushPrismaSchemaToDatabase } from './migrations'; | ||
import { initConfig, createSystem, createExpressServer } from '.'; | ||
|
||
export type GraphQLRequest = (arg: { | ||
query: string; | ||
variables?: Record<string, any>; | ||
operationName?: string; | ||
}) => Test; | ||
|
||
export type TestArgs = { | ||
context: KeystoneContext; | ||
graphQLRequest: GraphQLRequest; | ||
app: express.Express; | ||
}; | ||
|
||
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: { 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)); | ||
|
||
// (config, graphQLSchema, createContext, dev, projectAdminPath, isVerbose) | ||
const app = await createExpressServer(config, graphQLSchema, createContext, true, '', false); | ||
|
||
const graphQLRequest: GraphQLRequest = ({ query, variables = undefined, operationName }) => | ||
supertest(app) | ||
.post('/api/graphql') | ||
.send({ query, variables, operationName }) | ||
.set('Accept', 'application/json'); | ||
|
||
return { connect, disconnect, testArgs: { context: createContext(), graphQLRequest, app } }; | ||
} | ||
|
||
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(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"main": "dist/keystone.cjs.js", | ||
"module": "dist/keystone.esm.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Testing | ||
|
||
This package provides tools to help you test your Keystone system. | ||
This exports of this package have been moved to `@keystone-next/keystone` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,5 @@ | ||
import path from 'path'; | ||
import crypto from 'crypto'; | ||
import fs from 'fs'; | ||
import express from 'express'; | ||
import supertest, { Test } from 'supertest'; | ||
import memoizeOne from 'memoize-one'; | ||
import { initConfig, createSystem, createExpressServer } from '@keystone-next/keystone'; | ||
import { pushPrismaSchemaToDatabase } from '@keystone-next/keystone/migrations'; | ||
import { | ||
getCommittedArtifacts, | ||
writeCommittedArtifacts, | ||
requirePrismaClient, | ||
generateNodeModulesArtifacts, | ||
} from '@keystone-next/keystone/artifacts'; | ||
import type { KeystoneConfig, KeystoneContext } from '@keystone-next/types'; | ||
|
||
export type GraphQLRequest = (arg: { | ||
query: string; | ||
variables?: Record<string, any>; | ||
operationName?: string; | ||
}) => Test; | ||
|
||
export type TestArgs = { | ||
context: KeystoneContext; | ||
graphQLRequest: GraphQLRequest; | ||
app: express.Express; | ||
}; | ||
|
||
export type TestEnv = { | ||
connect: () => Promise<void>; | ||
disconnect: () => Promise<void>; | ||
testArgs: TestArgs; | ||
}; | ||
|
||
const _hashPrismaSchema = memoizeOne(prismaSchema => | ||
crypto.createHash('md5').update(prismaSchema).digest('hex') | ||
throw new Error( | ||
"Keystone's testing utilities have moved to `@keystone-next/keystone/testing`, please import them from there instead." | ||
); | ||
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: { 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)); | ||
|
||
// (config, graphQLSchema, createContext, dev, projectAdminPath, isVerbose) | ||
const app = await createExpressServer(config, graphQLSchema, createContext, true, '', false); | ||
|
||
const graphQLRequest: GraphQLRequest = ({ query, variables = undefined, operationName }) => | ||
supertest(app) | ||
.post('/api/graphql') | ||
.send({ query, variables, operationName }) | ||
.set('Accept', 'application/json'); | ||
|
||
return { connect, disconnect, testArgs: { context: createContext(), graphQLRequest, app } }; | ||
} | ||
|
||
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(); | ||
} | ||
}; | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/api-tests/access-control/mutations-list-declarative.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.