Skip to content

Commit

Permalink
Move GraphQLSchema snapshot serializer to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
singingwolfboy committed Oct 12, 2019
1 parent 1ad1cd3 commit 05012a4
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 99 deletions.
3 changes: 3 additions & 0 deletions packages/graphile-build-pg/package.json
Expand Up @@ -58,6 +58,9 @@
"jest": "^24.8.0"
},
"jest": {
"snapshotSerializers": [
"jest-serializer-graphql-schema"
],
"transform": {
"^.+\\.jsx?$": "../../.jest-wrapper.js"
},
Expand Down
11 changes: 0 additions & 11 deletions packages/graphile-build/__tests__/emptyMutation.test.js
@@ -1,16 +1,5 @@
const { isSchema, printSchema } = require("graphql");
const { buildSchema, defaultPlugins } = require("../");

const GraphQLSchemaSerializer = {
test(val) {
return isSchema(val);
},
serialize(schema) {
return printSchema(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

test("generates empty schema (with no Mutation type)", async () => {
const schema = await buildSchema([...defaultPlugins]);
expect(schema).toMatchSnapshot();
Expand Down
12 changes: 1 addition & 11 deletions packages/graphile-build/__tests__/enum.test.js
@@ -1,16 +1,6 @@
const { GraphQLEnumType, isSchema, printSchema } = require("graphql");
const { GraphQLEnumType } = require("graphql");
const { buildSchema, defaultPlugins } = require("../");

const GraphQLSchemaSerializer = {
test(val) {
return isSchema(val);
},
serialize(schema) {
return printSchema(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

function EnumPlugin(builder) {
builder.hook("GraphQLObjectType:fields", (fields, build, context) => {
const { extend, newWithHooks } = build;
Expand Down
12 changes: 0 additions & 12 deletions packages/graphile-build/__tests__/fieldData.test.js
Expand Up @@ -6,21 +6,9 @@ const {
GraphQLString,
GraphQLNonNull,
GraphQLList,
isSchema,
printSchema,
} = require("graphql");
const { buildSchema, defaultPlugins } = require("../");

const GraphQLSchemaSerializer = {
test(val) {
return isSchema(val);
},
serialize(schema) {
return printSchema(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

const base64 = str => Buffer.from(String(str)).toString("base64");
const base64Decode = str => Buffer.from(String(str), "base64").toString("utf8");

Expand Down
11 changes: 0 additions & 11 deletions packages/graphile-build/__tests__/watch.test.js
Expand Up @@ -3,7 +3,6 @@ const {
GraphQLObjectType,
GraphQLInt,
GraphQLNonNull,
isSchema,
printSchema,
} = require("graphql");
const {
Expand All @@ -12,16 +11,6 @@ const {
MutationPlugin,
} = require("../");

const GraphQLSchemaSerializer = {
test(val) {
return isSchema(val);
},
serialize(schema) {
return printSchema(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

const options = {};

const EventEmitter = require("events");
Expand Down
3 changes: 3 additions & 0 deletions packages/graphile-build/package.json
Expand Up @@ -53,6 +53,9 @@
"graphql": ">=0.9 <0.14 || ^14.0.2"
},
"jest": {
"snapshotSerializers": [
"jest-serializer-graphql-schema"
],
"transform": {
"^.+\\.jsx?$": "../../.jest-wrapper.js"
},
Expand Down
12 changes: 1 addition & 11 deletions packages/graphile-utils/__tests__/ExtendSchemaPlugin-pg.test.js
@@ -1,18 +1,8 @@
import pg from "pg";
import { graphql, isSchema, printSchema } from "graphql";
import { graphql } from "graphql";
import { createPostGraphileSchema } from "postgraphile-core";
import { makeExtendSchemaPlugin, gql, embed } from "../";

const GraphQLSchemaSerializer = {
test(val) {
return isSchema(val);
},
serialize(schema) {
return printSchema(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

const clean = data => {
if (Array.isArray(data)) {
return data.map(clean);
Expand Down
12 changes: 1 addition & 11 deletions packages/graphile-utils/__tests__/ExtendSchemaPlugin.test.js
Expand Up @@ -13,19 +13,9 @@ import {
SubscriptionPlugin,
MutationPayloadQueryPlugin,
} from "graphile-build";
import { graphql, subscribe, parse, isSchema, printSchema } from "graphql";
import { graphql, subscribe, parse } from "graphql";
import { $$asyncIterator } from "iterall";

const GraphQLSchemaSerializer = {
test(val) {
return isSchema(val);
},
serialize(schema) {
return printSchema(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

function TestUtils_ExtractScopePlugin(
hook,
objectTypeName,
Expand Down
3 changes: 3 additions & 0 deletions packages/graphile-utils/package.json
Expand Up @@ -48,6 +48,9 @@
"graphile-build-pg": "^4.4.2"
},
"jest": {
"snapshotSerializers": [
"jest-serializer-graphql-schema"
],
"transform": {
"^.+\\.jsx?$": "../../.jest-wrapper.js"
},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-serializer-graphql-schema/.gitignore
@@ -0,0 +1 @@
dist
2 changes: 2 additions & 0 deletions packages/jest-serializer-graphql-schema/.npmignore
@@ -0,0 +1,2 @@
tsconfig.json
src
33 changes: 33 additions & 0 deletions packages/jest-serializer-graphql-schema/package.json
@@ -0,0 +1,33 @@
{
"name": "jest-serializer-graphql-schema",
"version": "1.0.0",
"description": "Jest serializer for GraphQLSchema objects",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"prepack": "tsc",
"test": "true"
},
"repository": {
"type": "git",
"url": "git+https://github.com/graphile/graphile-engine.git"
},
"keywords": [
"jest",
"graphql",
"schema"
],
"author": "David Baumgold <david@davidbaumgold.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/graphile/graphile-engine/issues"
},
"homepage": "https://github.com/graphile/graphile-engine#readme",
"peerDependencies": {
"graphql": "^14.5.8"
},
"devDependencies": {
"pretty-format": "^24.9.0",
"typescript": "^3.6.4"
}
}
13 changes: 13 additions & 0 deletions packages/jest-serializer-graphql-schema/src/index.ts
@@ -0,0 +1,13 @@
import { GraphQLSchema, isSchema, printSchema } from "graphql";
import { Plugin } from "pretty-format";

const GraphQLSchemaSnapshotSerializer: Plugin = {
test(val: any) {
return isSchema(val);
},
serialize(schema: GraphQLSchema) {
return printSchema(schema);
},
};

module.exports = GraphQLSchemaSnapshotSerializer;
9 changes: 9 additions & 0 deletions packages/jest-serializer-graphql-schema/tsconfig.json
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "./dist",
"outDir": "./dist"
},
"include": ["src/**/*"]
}
19 changes: 0 additions & 19 deletions packages/pg-pubsub/__tests__/subs.test.ts
Expand Up @@ -6,9 +6,6 @@ import {
import {
introspectionQuery as INTROSPECTION_QUERY,
buildClientSchema,
isSchema,
printSchema,
GraphQLSchema,
} from "graphql";
import { Pool, PoolClient } from "pg";
import PgPubsub from "../src";
Expand All @@ -17,22 +14,6 @@ import { runQuery, TestCtx } from "./runQuery";
let ctx: TestCtx | null = null;
const CLI_DEFAULTS = {};

const GraphQLSchemaSerializer = {
test(val: any) {
return isSchema(val);
},
// It would be nice if we could name this method `serialize`,
// so that it conforms to the shape of `pretty-format.NewPlugin`,
// but the Jest types don't seem to know about that type yet,
// so we'll name it `print` instead to satisfy the Jest types.
// https://github.com/facebook/jest/blob/master/packages/jest-snapshot/src/plugins.ts
// https://github.com/facebook/jest/blob/master/packages/pretty-format/src/types.ts#L115
print(schema: GraphQLSchema) {
return printSchema(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

const init = async (options: PostGraphileOptions = {}) => {
if (ctx) {
throw new Error("CTX wasn't torn down");
Expand Down
7 changes: 6 additions & 1 deletion packages/pg-pubsub/package.json
Expand Up @@ -53,5 +53,10 @@
},
"files": [
"dist"
]
],
"jest": {
"snapshotSerializers": [
"jest-serializer-graphql-schema"
]
}
}
1 change: 0 additions & 1 deletion packages/postgraphile-core/.gitignore
@@ -1,3 +1,2 @@
node8plus
node7minus
.env
10 changes: 0 additions & 10 deletions packages/postgraphile-core/__tests__/integration/schema/core.js
Expand Up @@ -3,16 +3,6 @@ const { withPgClient } = require("../../helpers");
const { createPostGraphileSchema } = require("../../..");
const { isSchema } = require("graphql");

const GraphQLSchemaSerializer = {
test(val) {
return isSchema(val);
},
serialize(schema) {
return printSchemaOrdered(schema);
},
};
expect.addSnapshotSerializer(GraphQLSchemaSerializer);

exports.test = (schemas, options, setup, finalCheck = () => {}) => () =>
withPgClient(async client => {
if (setup) {
Expand Down
3 changes: 3 additions & 0 deletions packages/postgraphile-core/package.json
Expand Up @@ -38,6 +38,9 @@
"pg": ">=6.1.0 <8"
},
"jest": {
"snapshotSerializers": [
"jest-serializer-graphql-schema"
],
"transform": {
"^.+\\.jsx?$": "../../.jest-wrapper.js"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/prepack-all
Expand Up @@ -2,7 +2,7 @@
set -e
cd packages;
# Do not run this in parallel; build order matters due to TypeScript.
for PACKAGE in graphile lru pg-sql2 graphile-build graphile-build-pg graphql-parse-resolve-info graphile-utils postgraphile-core lds subscriptions-lds pg-pubsub; do
for PACKAGE in jest-serializer-graphql-schema graphile lru pg-sql2 graphile-build graphile-build-pg graphql-parse-resolve-info graphile-utils postgraphile-core lds subscriptions-lds pg-pubsub; do
cd $PACKAGE;
npm run prepack;
cd -;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -8861,6 +8861,11 @@ typescript@3.6.3, typescript@^3.4.5, typescript@^3.6.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.3.tgz#fea942fabb20f7e1ca7164ff626f1a9f3f70b4da"
integrity sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==

typescript@^3.6.4:
version "3.6.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.4.tgz#b18752bb3792bc1a0281335f7f6ebf1bbfc5b91d"
integrity sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==

uglify-js@^3.1.4:
version "3.6.0"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"
Expand Down

0 comments on commit 05012a4

Please sign in to comment.