Skip to content

Commit

Permalink
feat: provide ability to mock errors - fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
joual committed Mar 13, 2020
1 parent b931510 commit 8b4027d
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 8 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Usage](#usage)
- [Default Case](#default-case)
- [Partial Nested Shape](#partial-nested-shape)
- [Providing Functions as Resolver](#providing-functions-as-resolver)
- [Mocking Errors](#mocking-errors)
- [Mocking Mutations](#mocking-mutations)
- [Roadmap](#roadmap)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -145,8 +150,41 @@ npm i graphql-ergonomock --save-dev
<!-- USAGE EXAMPLES -->
### Usage

#### Default Case

TBD

#### Partial Nested Shape

TBD

#### Providing Functions as Resolver

TBD

#### Mocking Errors

```js
const testQuery = gql`
{
getCar {
id
}
}
`;

const resp = mock(schema, testQuery, {
getCar: () => { throw new Error("Server Error"); }
// or simply getCar: new Error("Server Error")
});

console.log(resp.data.getCar); // null
console.log(resp.errors[0]); // { message: "Server Error", ...}
```

#### Mocking Mutations

TBD

<!-- ROADMAP -->
## Roadmap
Expand Down
145 changes: 137 additions & 8 deletions src/__tests__/lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mock } from "..";
import { buildSchemaFromTypeDefinitions } from "graphql-tools";
import { visitWithTypeInfo } from "graphql";
import { visitWithTypeInfo, GraphQLError } from "graphql";
// import { graphql, GraphQLResolveInfo } from "graphql";

const schemaSDL = /* GraphQL */ `
Expand Down Expand Up @@ -914,13 +914,142 @@ describe("Automocking", () => {
});

describe("mocking errors", () => {
test.todo("can provide Errors for basic types");
test.todo("can provide Errors for enums");
test.todo("can provide Errors for objects");
test.todo("can provide Errors for lists (one error among list items)");
test.todo("can provide Errors for unions");
test.todo("can provide Errors for interfaces");
test.todo("can throw errors in functions as resolver");
test("can provide Errors for basic types", () => {
const testQuery = /* GraphQL */ `
{
returnInt
returnString
}
`;
const resp: any = mock(schema, testQuery, {
returnInt: new Error("foo bar")
});
expect(resp.data).toMatchObject({
returnInt: null,
returnString: expect.toBeString()
});
expect(resp.errors).toStrictEqual([new GraphQLError("foo bar")]);
});

test("can provide Errors for enums", () => {
const testQuery = /* GraphQL */ `
{
returnEnum
returnString
}
`;
const resp: any = mock(schema, testQuery, {
returnEnum: new Error("foo enum")
});
expect(resp.data).toMatchObject({
returnEnum: null,
returnString: expect.toBeString()
});
expect(resp.errors).toStrictEqual([new GraphQLError("foo enum")]);
});

test("can provide Errors for objects", () => {
const testQuery = /* GraphQL */ `
{
returnShape {
id
}
returnString
}
`;
const resp: any = mock(schema, testQuery, {
returnShape: new Error("foo shape")
});
expect(resp.data).toMatchObject({
returnShape: null,
returnString: expect.toBeString()
});
expect(resp.errors).toStrictEqual([new GraphQLError("foo shape")]);
});

test("can provide Errors for lists (one error among list items)", () => {
const testQuery = /* GraphQL */ `
{
returnStringList
returnString
}
`;
const resp: any = mock(schema, testQuery, {
returnStringList: ["Whiskey", new Error("foo Tango"), "Foxtrot"]
});
expect(resp.data).toMatchObject({
returnStringList: ["Whiskey", null, "Foxtrot"],
returnString: expect.toBeString()
});
expect(resp.errors).toStrictEqual([new GraphQLError("foo Tango")]);
});

test("can provide Errors for unions", () => {
const testQuery = /* GraphQL */ `
{
returnBirdsAndBees {
__typename
id
}
returnString
}
`;
const resp: any = mock(schema, testQuery, {
returnBirdsAndBees: [{ __typename: "Bird" }, new Error("foo Tango"), { __typename: "Bee" }]
});
expect(resp.data).toMatchObject({
returnBirdsAndBees: [
{ __typename: "Bird", id: expect.toBeString() },
null,
{ __typename: "Bee", id: expect.toBeString() }
],
returnString: expect.toBeString()
});
expect(resp.errors).toStrictEqual([new GraphQLError("foo Tango")]);
});
test("can provide Errors for interfaces", () => {
const testQuery = /* GraphQL */ `
{
returnFlying {
__typename
id
}
returnString
}
`;
const resp: any = mock(schema, testQuery, {
returnFlying: [{ __typename: "Bird" }, new Error("foo Tango"), { __typename: "Bee" }]
});
expect(resp.data).toMatchObject({
returnFlying: [
{ __typename: "Bird", id: expect.toBeString() },
null,
{ __typename: "Bee", id: expect.toBeString() }
],
returnString: expect.toBeString()
});
expect(resp.errors).toStrictEqual([new GraphQLError("foo Tango")]);
});
test("can throw errors in functions as resolver", () => {
const testQuery = /* GraphQL */ `
{
returnShape {
id
}
returnString
}
`;
const resp: any = mock(schema, testQuery, {
returnShape: () => {
throw new Error("foo shape");
}
});
expect(resp.data).toMatchObject({
returnShape: null,
returnString: expect.toBeString()
});
expect(resp.errors).toStrictEqual([new GraphQLError("foo shape")]);
});
});

test("base case - TBD remove this test later", () => {
Expand Down

0 comments on commit 8b4027d

Please sign in to comment.