diff --git a/examples/testing/example-test.ts b/examples/testing/example-test.ts index 3babd573b47..4360344730c 100644 --- a/examples/testing/example-test.ts +++ b/examples/testing/example-test.ts @@ -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 () => { @@ -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', + } ); }); @@ -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`, + } + ); });