Skip to content

Commit

Permalink
chore: Mock yup error shape in tests in lieu of full dependency.
Browse files Browse the repository at this point in the history
It shouldn't be necessary to bring this entire `yup` dependency just to test
compatibility with the shape of a `yup` (https://npm.im/yup) error.

Ref: #1288
  • Loading branch information
abernix committed Jul 23, 2019
1 parent 36edb35 commit ce85abb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 68 deletions.
59 changes: 0 additions & 59 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
"@types/test-listen": "1.1.0",
"@types/type-is": "1.6.2",
"@types/ws": "6.0.1",
"@types/yup": "0.26.22",
"apollo-fetch": "0.7.0",
"apollo-link": "1.2.12",
"apollo-link-http": "1.5.15",
Expand Down Expand Up @@ -139,8 +138,7 @@
"ts-jest": "24.0.2",
"tslint": "5.18.0",
"typescript": "3.5.3",
"ws": "6.2.1",
"yup": "0.27.0"
"ws": "6.2.1"
},
"jest": {
"projects": [
Expand Down
46 changes: 40 additions & 6 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,49 @@ export function testApolloServer<AS extends ApolloServerBase>(
});

it('works with errors similar to GraphQL errors, such as yup', async () => {
// https://npm.im/yup is a package that produces a particular type of
// error that we test compatibility with. This test was first brought
// with https://github.com/apollographql/apollo-server/pull/1288. We
// used to use the actual `yup` package to generate the error, but we
// don't need to actually bundle that dependency just to test
// compatibility with that particular error shape. To be honest, it's
// not clear from the original PR which attribute of this error need be
// mocked, but for the sake not not breaking anything, all of yup's
// error properties have been reproduced here.
const throwError = jest.fn(async () => {
const schema = yup.object().shape({
email: yup
.string()
.email()
.required('Please enter your email address'),
// Intentionally `any` because this is a custom Error class with
// various custom properties (like `value` and `params`).
const yuppieError: any = new Error('email must be a valid email');
yuppieError.name = 'ValidationError';

// Set `message` to enumerable, which `yup` does and `Error` doesn't.
Object.defineProperty(yuppieError, 'message', {
enumerable: true,
});

await schema.validate({ email: 'lol' });
// Set other properties which `yup` sets.
yuppieError.path = 'email';
yuppieError.type = undefined;
yuppieError.value = { email: 'invalid-email' };
yuppieError.errors = ['email must be a valid email'];
yuppieError.inner = [];
yuppieError.params = {
path: 'email',
value: 'invalid-email',
originalValue: 'invalid-email',
label: undefined,
regex: /@/,
};

// This stack is fake, but roughly what `yup` generates!
yuppieError.stack = [
'ValidationError: email must be a valid email',
' at createError (yup/lib/util/createValidation.js:64:35)',
' at yup/lib/util/createValidation.js:113:108',
' at process._tickCallback (internal/process/next_tick.js:68:7)',
].join('\n');

throw yuppieError;
});

const formatError = jest.fn(error => {
Expand Down

0 comments on commit ce85abb

Please sign in to comment.