Skip to content

Commit

Permalink
Drop unused JEST environment variable from examples/testing (#8854)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens authored Oct 10, 2023
1 parent a662c19 commit 62146aa
Showing 1 changed file with 42 additions and 67 deletions.
109 changes: 42 additions & 67 deletions examples/testing/example-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import assert from 'node:assert/strict';

import { resetDatabase } from '@keystone-6/core/testing';
import { getContext } from '@keystone-6/core/context';
import baseConfig from './keystone';
import config from './keystone';
import * as PrismaModule from '.myprisma/client';

const dbUrl = `file:./test-${process.env.JEST_WORKER_ID}.db`;
const prismaSchemaPath = path.join(__dirname, 'schema.prisma');
const config = { ...baseConfig, db: { ...baseConfig.db, url: dbUrl } };

const context = getContext(config, PrismaModule);

beforeEach(async () => {
await resetDatabase(dbUrl, prismaSchemaPath);
await resetDatabase(config.db.url, prismaSchemaPath);
});

test('Create a User using context.query', async () => {
Expand All @@ -27,19 +24,18 @@ test('Create a User using context.query', async () => {
});

test('Check that trying to create user with no name (required field) fails', async () => {
// the context.graphql.raw API can be useful when you expect errors
const { data, errors } = (await context.graphql.raw({
query: `mutation {
createUser(data: { password: "dont-use-me" }) {
id name password { isSet }
}
}`,
})) as any;
assert.equal(data!.createUser, null);
assert.equal(errors![0].path[0], 'createUser');
assert.equal(
errors![0].message,
'You provided invalid data for this operation.\n - User.name: Name must not be empty'
await assert.rejects(
async () => {
await context.db.User.createOne({
data: {
password: 'not-a-password',
},
});
},
{
message:
'You provided invalid data for this operation.\n - User.name: Name must not be empty',
}
);
});

Expand Down Expand Up @@ -69,60 +65,39 @@ test('Check access control by running updateTask as a specific user via context.
assert.equal(task.priority, 'high');
assert.equal(task.isComplete, false);
assert.equal(task.assignedTo.name, 'Alice');

// test that we can't update the task (without a session)
{
const { data, errors } = (await context.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
id
}
}`,
variables: { id: task.id },
})) as any;
assert.equal(data!.updateTask, null);
assert.equal(errors.length, 1);
assert.equal(errors![0].path[0], 'updateTask');
assert.equal(
errors![0].message,
'Access denied: You cannot update that Task - it may not exist'
);
}
await assert.rejects(
async () => {
await context.db.Task.updateOne({
where: { id: task.id },
data: { isComplete: true },
});
},
{
message: `Access denied: You cannot update that Task - it may not exist`,
}
);

// test that we can update the task (with a session)
{
const { data, errors } = (await context
const result = await context
.withSession({ listKey: 'User', itemId: alice.id, data: {} })
.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
id
}
}`,
variables: { id: task.id },
})) as any;
assert.equal(data!.updateTask.id, task.id);
assert.equal(errors, undefined);
.db.Task.updateOne({
where: { id: task.id },
data: { isComplete: true },
});
assert.equal(result.id, task.id);
}

// test that we can't update the task (with an invalid session (Bob))
{
const { data, errors } = (await context
.withSession({ listKey: 'User', itemId: bob.id, data: {} })
.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
id
}
}`,
variables: { id: task.id },
})) as any;
assert.equal(data!.updateTask, null);
assert.equal(errors!.length, 1);
assert.equal(errors![0].path[0], 'updateTask');
assert.equal(
errors![0].message,
`Access denied: You cannot update that Task - it may not exist`
);
}
await assert.rejects(
async () => {
await context.withSession({ listKey: 'User', itemId: bob.id, data: {} }).db.Task.updateOne({
where: { id: task.id },
data: { isComplete: true },
});
},
{
message: `Access denied: You cannot update that Task - it may not exist`,
}
);
});

0 comments on commit 62146aa

Please sign in to comment.