Skip to content

Commit

Permalink
Update error checking code in tests (#6191)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Jul 27, 2021
1 parent 151d31d commit 6ff7038
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 70 deletions.
6 changes: 3 additions & 3 deletions tests/api-tests/fields/required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import globby from 'globby';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { text } from '@keystone-next/fields';
import { setupTestRunner } from '@keystone-next/testing';
import { apiTestConfig, expectValidationFailure } from '../utils';
import { apiTestConfig, expectValidationError } from '../utils';

const testModules = globby.sync(`packages/**/src/**/test-fixtures.{js,ts}`, {
absolute: true,
Expand Down Expand Up @@ -65,7 +65,7 @@ testModules
}`,
});
expect(data).toEqual({ createTest: null });
expectValidationFailure(errors, [{ path: ['createTest'] }]);
expectValidationError(errors, [{ path: ['createTest'] }]);
})
);

Expand All @@ -85,7 +85,7 @@ testModules
}`,
});
expect(data).toEqual({ updateTest: null });
expectValidationFailure(errors, [{ path: ['updateTest'] }]);
expectValidationError(errors, [{ path: ['updateTest'] }]);
})
);

Expand Down
9 changes: 4 additions & 5 deletions tests/api-tests/fields/unique.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import globby from 'globby';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { text } from '@keystone-next/fields';
import { setupTestEnv, setupTestRunner } from '@keystone-next/testing';
import { apiTestConfig, expectInternalServerError } from '../utils';
import { apiTestConfig, expectPrismaError } from '../utils';

const testModules = globby.sync(`packages/**/src/**/test-fixtures.{js,ts}`, {
absolute: true,
Expand Down Expand Up @@ -70,7 +70,7 @@ testModules
variables: { data: { testField: mod.exampleValue(matrixValue) } },
});
expect(body.data).toEqual({ createTest: null });
expectInternalServerError(body.errors, [
expectPrismaError(body.errors, [
{
path: ['createTest'],
message: expect.stringMatching(
Expand All @@ -97,9 +97,8 @@ testModules
},
});

expect(body.data.foo).not.toBe(null);
expect(body.data.bar).toBe(null);
expectInternalServerError(body.errors, [
expect(body.data).toEqual({ foo: { id: expect.any(String) }, bar: null });
expectPrismaError(body.errors, [
{
path: ['bar'],
message: expect.stringMatching(
Expand Down
14 changes: 7 additions & 7 deletions tests/api-tests/hooks/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { text } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { setupTestRunner } from '@keystone-next/testing';
import { apiTestConfig, expectValidationFailure } from '../utils';
import { apiTestConfig, expectValidationError } from '../utils';

const runner = setupTestRunner({
config: apiTestConfig({
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('List Hooks: #validateInput()', () => {

// Returns null and throws an error
expect(data).toEqual({ createUser: null });
expectValidationFailure(errors, [{ path: ['createUser'] }]);
expectValidationError(errors, [{ path: ['createUser'] }]);

// Only the original user should exist
const _users = await context.lists.User.findMany({ query: 'id name' });
Expand All @@ -63,7 +63,7 @@ describe('List Hooks: #validateInput()', () => {

// Returns null and throws an error
expect(data).toEqual({ updateUser: null });
expectValidationFailure(errors, [{ path: ['updateUser'] }]);
expectValidationError(errors, [{ path: ['updateUser'] }]);

// User should have its original name
const _users = await context.lists.User.findMany({ query: 'id name' });
Expand All @@ -87,7 +87,7 @@ describe('List Hooks: #validateInput()', () => {

// Returns null and throws an error
expect(data).toEqual({ deleteUser: null });
expectValidationFailure(errors, [{ path: ['deleteUser'] }]);
expectValidationError(errors, [{ path: ['deleteUser'] }]);

// Bad users should still be in the database.
const _users = await context.lists.User.findMany({ query: 'id name' });
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('List Hooks: #validateInput()', () => {
],
});
// The invalid creates should have errors which point to the nulls in their path
expectValidationFailure(errors, [{ path: ['createUsers', 1] }, { path: ['createUsers', 3] }]);
expectValidationError(errors, [{ path: ['createUsers', 1] }, { path: ['createUsers', 3] }]);

// Three users should exist in the database
const users = await context.lists.User.findMany({
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('List Hooks: #validateInput()', () => {
],
});
// The invalid updates should have errors which point to the nulls in their path
expectValidationFailure(errors, [{ path: ['updateUsers', 1] }, { path: ['updateUsers', 3] }]);
expectValidationError(errors, [{ path: ['updateUsers', 1] }, { path: ['updateUsers', 3] }]);

// All users should still exist in the database
const _users = await context.lists.User.findMany({
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('List Hooks: #validateInput()', () => {
],
});
// The invalid deletes should have errors which point to the nulls in their path
expectValidationFailure(errors, [{ path: ['deleteUsers', 1] }, { path: ['deleteUsers', 3] }]);
expectValidationError(errors, [{ path: ['deleteUsers', 1] }, { path: ['deleteUsers', 3] }]);

// Three users should still exist in the database
const _users = await context.lists.User.findMany({
Expand Down
45 changes: 26 additions & 19 deletions tests/api-tests/id-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { text } from '@keystone-next/fields';
import { setupTestRunner } from '@keystone-next/testing';
import { isCuid } from 'cuid';
import { validate } from 'uuid';
import { apiTestConfig } from './utils';
import { apiTestConfig, expectBadUserInput } from './utils';

export function assertNever(arg: never) {
throw new Error('expected to never be called but received: ' + JSON.stringify(arg));
Expand All @@ -20,26 +20,28 @@ describe.each(['autoincrement', 'cuid', 'uuid'] as const)('%s', kind => {
});
test(
'Fetching an item uniquely with an invalid id throws an error',
runner(async ({ context }) => {
await expect(
context.lists.User.findOne({ where: { id: 'adskjnfasdfkjekfj' } })
).rejects.toMatchObject({
message: `Only ${
kind === 'autoincrement' ? 'an integer' : `a ${kind}`
} can be passed to id filters`,
runner(async ({ graphQLRequest }) => {
const { body } = await graphQLRequest({
query: `{ User(where: { id: "adskjnfasdfkjekfj"}) { id } }`,
});
expect(body.data).toEqual({ User: null });
const s = kind === 'autoincrement' ? 'an integer' : `a ${kind}`;
expectBadUserInput(body.errors, [
{ path: ['User'], message: `Only ${s} can be passed to id filters` },
]);
})
);
test(
'Filtering an item with an invalid id throws an error',
runner(async ({ context }) => {
await expect(
context.lists.User.findOne({ where: { id: 'adskjnfasdfkjekfj' } })
).rejects.toMatchObject({
message: `Only ${
kind === 'autoincrement' ? 'an integer' : `a ${kind}`
} can be passed to id filters`,
runner(async ({ graphQLRequest }) => {
const { body } = await graphQLRequest({
query: `{ allUsers(where: { id: "adskjnfasdfkjekfj"}) { id } }`,
});
expect(body.data).toEqual({ allUsers: null });
const s = kind === 'autoincrement' ? 'an integer' : `a ${kind}`;
expectBadUserInput(body.errors, [
{ path: ['allUsers'], message: `Only ${s} can be passed to id filters` },
]);
})
);
test(
Expand Down Expand Up @@ -102,14 +104,19 @@ describe.each(['autoincrement', 'cuid', 'uuid'] as const)('%s', kind => {
});
test(
'searching for uppercased cuid does not work',
runner(async ({ context }) => {
runner(async ({ context, graphQLRequest }) => {
const { id } = (await context.lists.User.createOne({
data: { name: 'something' },
})) as { id: string };

await expect(
context.lists.User.findOne({ where: { id: id.toUpperCase() } })
).rejects.toMatchInlineSnapshot(`[GraphQLError: Only a cuid can be passed to id filters]`);
const { body } = await graphQLRequest({
query: `query q($id: ID!){ User(where: { id: $id }) { id } }`,
variables: { id: id.toUpperCase() },
});
expect(body.data).toEqual({ User: null });
expectBadUserInput(body.errors, [
{ path: ['User'], message: `Only a cuid can be passed to id filters` },
]);
})
);
}
4 changes: 2 additions & 2 deletions tests/api-tests/queries/orderBy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { integer } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { setupTestRunner } from '@keystone-next/testing';
import { KeystoneContext } from '@keystone-next/types';
import { apiTestConfig, expectInternalServerError } from '../utils';
import { apiTestConfig, expectBadUserInput } from '../utils';

const runner = setupTestRunner({
config: apiTestConfig({
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('Ordering by a single field', () => {
query: 'query { allUsers(orderBy: [{ a: asc, b: asc }]) { id } }',
});
expect(body.data).toEqual({ allUsers: null });
expectInternalServerError(body.errors, [
expectBadUserInput(body.errors, [
{ path: ['allUsers'], message: 'Only a single key must be passed to UserOrderByInput' },
]);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gen, sampleOne } from 'testcheck';
import { text, relationship } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { setupTestRunner } from '@keystone-next/testing';
import { apiTestConfig, expectNestedError } from '../../utils';
import { apiTestConfig, expectRelationshipError } from '../../utils';

const alphanumGenerator = gen.alphaNumString.notEmpty();

Expand Down Expand Up @@ -241,7 +241,7 @@ describe('non-matching filter', () => {
test(
'errors if connecting items which cannot be found during creating',
runner(async ({ context }) => {
const FAKE_ID = 100;
const FAKE_ID = 'cabc123';

// Create an item that does the linking
const { data, errors } = await context.graphql.raw({
Expand All @@ -257,7 +257,7 @@ describe('non-matching filter', () => {
}`,
});
expect(data).toEqual({ createUser: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{ path: ['createUser'], message: 'Unable to create and/or connect 1 User.notes<Note>' },
]);
})
Expand All @@ -266,7 +266,7 @@ describe('non-matching filter', () => {
test(
'errors if connecting items which cannot be found during update',
runner(async ({ context }) => {
const FAKE_ID = 100;
const FAKE_ID = 'cabc123';

// Create an item to link against
const createUser = await context.lists.User.createOne({ data: {} });
Expand All @@ -289,7 +289,7 @@ describe('non-matching filter', () => {
});

expect(data).toEqual({ updateUser: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{ path: ['updateUser'], message: 'Unable to create and/or connect 1 User.notes<Note>' },
]);
})
Expand Down Expand Up @@ -321,7 +321,7 @@ describe('with access control', () => {
});

expect(data).toEqual({ createUserToNotesNoRead: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: ['createUserToNotesNoRead'],
message: 'Unable to create and/or connect 1 UserToNotesNoRead.notes<NoteNoRead>',
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('with access control', () => {
}`,
});
expect(data).toEqual({ updateUserToNotesNoRead: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: ['updateUserToNotesNoRead'],
message: 'Unable to create and/or connect 1 UserToNotesNoRead.notes<NoteNoRead>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gen, sampleOne } from 'testcheck';
import { text, relationship } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { setupTestRunner } from '@keystone-next/testing';
import { apiTestConfig, expectNestedError } from '../../utils';
import { apiTestConfig, expectRelationshipError } from '../../utils';

const runner = setupTestRunner({
config: apiTestConfig({
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('non-matching filter', () => {
});

expect(data).toEqual({ createEvent: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{ path: ['createEvent'], message: 'Unable to connect a Event.group<Group>' },
]);
})
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('non-matching filter', () => {
}`,
});
expect(data).toEqual({ updateEvent: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{ path: ['updateEvent'], message: 'Unable to connect a Event.group<Group>' },
]);
})
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('with access control', () => {
}`,
});
expect(data).toEqual({ [`updateEventTo${group.name}`]: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: [`updateEventTo${group.name}`],
message: `Unable to connect a EventTo${group.name}.group<${group.name}>`,
Expand Down Expand Up @@ -332,7 +332,7 @@ describe('with access control', () => {
});

expect(data).toEqual({ [`createEventTo${group.name}`]: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: [`createEventTo${group.name}`],
message: `Unable to connect a EventTo${group.name}.group<${group.name}>`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gen, sampleOne } from 'testcheck';
import { text, relationship } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { setupTestRunner } from '@keystone-next/testing';
import { apiTestConfig, expectNestedError } from '../../utils';
import { apiTestConfig, expectRelationshipError } from '../../utils';

const alphanumGenerator = gen.alphaNumString.notEmpty();

Expand Down Expand Up @@ -149,7 +149,7 @@ describe('errors on incomplete data', () => {
});

expect(data).toEqual({ createUser: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: ['createUser'],
message: 'Nested mutation operation invalid for User.notes<Note>',
Expand Down Expand Up @@ -189,7 +189,7 @@ describe('with access control', () => {
});

expect(data).toEqual({ createUserToNotesNoRead: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: ['createUserToNotesNoRead'],
message: 'Unable to create and/or connect 1 UserToNotesNoRead.notes<NoteNoRead>',
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('with access control', () => {
});

expect(data).toEqual({ updateUserToNotesNoRead: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: ['updateUserToNotesNoRead'],
message: 'Unable to create and/or connect 1 UserToNotesNoRead.notes<NoteNoRead>',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { text, relationship } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { setupTestRunner } from '@keystone-next/testing';
import { apiTestConfig, expectNestedError } from '../../utils';
import { apiTestConfig, expectRelationshipError } from '../../utils';

const runner = setupTestRunner({
config: apiTestConfig({
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('errors on incomplete data', () => {
});

expect(data).toEqual({ createEvent: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: ['createEvent'],
message: 'Nested mutation operation invalid for Event.group<Group>',
Expand All @@ -53,7 +53,7 @@ describe('errors on incomplete data', () => {
query: `
mutation {
createEvent(data: { group: {
connect: { id: "abc123"},
connect: { id: "cabc123"},
create: { name: "foo" }
} }) {
id
Expand All @@ -62,7 +62,7 @@ describe('errors on incomplete data', () => {
});

expect(data).toEqual({ createEvent: null });
expectNestedError(errors, [
expectRelationshipError(errors, [
{
path: ['createEvent'],
message: 'Nested mutation operation invalid for Event.group<Group>',
Expand Down
Loading

1 comment on commit 6ff7038

@vercel
Copy link

@vercel vercel bot commented on 6ff7038 Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.