Skip to content

Commit

Permalink
add support for Context in test runners
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Jul 28, 2022
1 parent 1389e4a commit 703d17a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/pages/docs/apis/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ S3 options:
- `pathPrefix`: The prefix for the file, used to set the subfolder of your bucket files will be stored in.
- `endpoint`: The endpoint to use - if provided, this endpoint will be used instead of the default amazon s3 endpoint
- `forcePathStyle`: Force the old pathstyle of using the bucket name after the host
- `signed.expiry`: Use S3 URL signing to keep S3 assets private. `expiry` is in seconds
- `signed.expiry`: Use S3 URL signing to keep S3 assets private. `expiry` is in seconds

```typescript
import { config } from '@keystone-6/core';
Expand Down
3 changes: 1 addition & 2 deletions examples/ecommerce/tests/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { Context } from '.keystone/types';
const FAKE_ID = 'cinjfgbkjnfg';

const asUser = (context: Context, itemId?: string) => context.withSession({ itemId, data: {} });

const runner = setupTestRunner({ config });
const runner = setupTestRunner<Context>({ config });

describe(`Custom mutations`, () => {
describe('checkout(token)', () => {
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,29 @@ export async function setupTestEnv({
};
}

export function setupTestRunner({ config }: { config: KeystoneConfig }) {
export function setupTestRunner<Context extends KeystoneContext>({
config,
}: {
config: KeystoneConfig;
}) {
type TestArgs = {
context: Context;
graphQLRequest: GraphQLRequest;
app: express.Express;
server: Server;
};

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();

const { context } = testArgs;
try {
return await testFn(testArgs);
return await testFn({
...testArgs,
context: context as Context,
});
} finally {
await disconnect();
}
Expand Down

0 comments on commit 703d17a

Please sign in to comment.