Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Add test to compile typescript-yoga template
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Nov 9, 2018
1 parent ae86882 commit 97aa601
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 51 deletions.
3 changes: 2 additions & 1 deletion packages/graphqlgen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"jest": "23.6.0",
"ts-jest": "23.10.4",
"ts-node": "7.0.1",
"tslint": "5.11.0"
"tslint": "5.11.0",
"typescript": "^3.1.6"
}
}
6 changes: 2 additions & 4 deletions packages/graphqlgen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function generateCode(
return { generatedTypes, generatedResolvers }
}

function writeTypes(types: string, config: GraphQLGenDefinition): void {
export function writeTypes(types: string, config: GraphQLGenDefinition): void {
// Create generation target folder, if it does not exist
// TODO: Error handling around this
mkdirp.sync(path.dirname(config.output))
Expand All @@ -135,7 +135,7 @@ function writeTypes(types: string, config: GraphQLGenDefinition): void {
)
}

function writeResolversScaffolding(
export function writeResolversScaffolding(
resolvers: CodeFileLike[],
config: GraphQLGenDefinition,
) {
Expand Down Expand Up @@ -172,8 +172,6 @@ function writeResolversScaffolding(
})

console.log(chalk.green(`Resolvers scaffolded at ${outputResolversDir}`))

process.exit(0)
}

function bootstrapYamlFile() {
Expand Down
6 changes: 3 additions & 3 deletions packages/graphqlgen/src/tests/fixtures/context/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
interface Data {
export interface Data {
users: User[]
}

interface Context {
export interface Context {
data: Data
}

interface User {
export interface User {
id: string
}
8 changes: 4 additions & 4 deletions packages/graphqlgen/src/tests/flow/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('basic enum', async () => {
models: {
files: [relative('../fixtures/enum/types-flow.js')],
},
output: relative('./generated/enum/graphqlgen.ts'),
output: relative('./generated/enum/graphqlgen.js'),
['resolver-scaffolding']: {
output: relative('./tmp/enum/'),
layout: 'file-per-type',
Expand All @@ -41,7 +41,7 @@ test('basic union', async () => {
models: {
files: [relative('../fixtures/union/flow-types.js')],
},
output: relative('./generated/union/graphqlgen.ts'),
output: relative('./generated/union/graphqlgen.js'),
['resolver-scaffolding']: {
output: relative('./tmp/union/'),
layout: 'file-per-type',
Expand All @@ -61,7 +61,7 @@ test('defaultName', async () => {
},
],
},
output: relative('./generated/defaultName/graphqlgen.ts'),
output: relative('./generated/defaultName/graphqlgen.js'),
['resolver-scaffolding']: {
output: relative('./tmp/scalar/'),
layout: 'file-per-type',
Expand All @@ -76,7 +76,7 @@ test('basic scalar', async () => {
models: {
files: [relative('../fixtures/scalar/flow-types.js')],
},
output: relative('./generated/scalar/graphqlgen.ts'),
output: relative('./generated/scalar/graphqlgen.js'),
['resolver-scaffolding']: {
output: relative('./tmp/scalar/'),
layout: 'file-per-type',
Expand Down
91 changes: 86 additions & 5 deletions packages/graphqlgen/src/tests/generation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
import { GraphQLGenDefinition } from "graphqlgen-json-schema";
import { parseModels, parseSchema } from "../parse";
import { validateConfig } from "../validation";
import { generateCode } from "../index";
import * as ts from 'typescript'
import { GraphQLGenDefinition } from 'graphqlgen-json-schema'
import { join } from 'path'
import { parseModels, parseSchema } from '../parse'
import { validateConfig } from '../validation'
import { generateCode, writeResolversScaffolding, writeTypes } from '../index'
import * as rimraf from 'rimraf'
import * as path from 'path'

function printTypescriptErrors(diagnotics: ReadonlyArray<ts.Diagnostic>) {
diagnotics.forEach(diagnostic => {
if (diagnostic.file) {
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
diagnostic.start!,
)
let message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
'\n',
)
console.log(
`${diagnostic.file.fileName} (${line + 1},${character +
1}): ${message}`,
)
} else {
console.log(
`${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`,
)
}
})
}

function compileTypescript(fileNames: string[], compiledOutputDir: string) {
const errors = ts
.createProgram(fileNames, {
sourceMap: false,
noEmitOnError: true,
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.CommonJS,
outDir: compiledOutputDir
})
.emit().diagnostics

if (errors.length > 0) {
printTypescriptErrors(errors)
}

expect(errors.length).toEqual(0)
}

export function testGeneration(config: GraphQLGenDefinition) {
const schema = parseSchema(config.schema)

expect(validateConfig(config, schema)).toBe(true)

const modelMap = parseModels(config.models, schema, config.output, config.language)
const modelMap = parseModels(
config.models,
schema,
config.output,
config.language,
)
const { generatedTypes, generatedResolvers } = generateCode({
schema,
language: config.language,
Expand All @@ -19,4 +68,36 @@ export function testGeneration(config: GraphQLGenDefinition) {

expect(generatedTypes).toMatchSnapshot()
expect(generatedResolvers).toMatchSnapshot()

const restoreLog = console.log

// Mock console.log
console.log = jest.fn()

writeTypes(generatedTypes, config)
writeResolversScaffolding(generatedResolvers, config)

// Restore console log to print errors if there are any
console.log = restoreLog

const outputResolversDir = config['resolver-scaffolding']!.output

const fileNames = [
...generatedResolvers.map(resolver =>
join(outputResolversDir, resolver.path),
),
config.output,
]

const compiledOutputDir = path.join(path.dirname(config.output), 'compiled')

if (config.language === 'typescript') {
compileTypescript(fileNames, compiledOutputDir)
}

if (config.language === 'flow') {
// compileFlow(fileNames)
}

rimraf.sync(path.dirname(config.output))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`basic enum 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { User } from \\"../../../fixtures/enum/types\\";
import { User } from \\"../../fixtures/enum/types\\";
type Context = any;
type EnumAnnotation = \\"EDITOR\\" | \\"COLLABORATOR\\";
Expand Down Expand Up @@ -167,7 +167,7 @@ exports[`basic input 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { AddMemberPayload } from \\"../../../fixtures/input/types\\";
import { AddMemberPayload } from \\"../../fixtures/input/types\\";
type Context = any;
export namespace MutationResolvers {
Expand Down Expand Up @@ -337,7 +337,7 @@ exports[`basic scalar 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { AddMemberPayload } from \\"../../../fixtures/scalar/types\\";
import { AddMemberPayload } from \\"../../fixtures/scalar/types\\";
type Context = any;
export namespace MutationResolvers {
Expand Down Expand Up @@ -453,7 +453,7 @@ exports[`basic schema 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { Number } from \\"../../../fixtures/basic\\";
import { Number } from \\"../../fixtures/basic\\";
type Context = any;
export namespace QueryResolvers {
Expand Down Expand Up @@ -795,7 +795,7 @@ exports[`basic union 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { User, Student, Professor } from \\"../../../fixtures/union/types\\";
import { User, Student, Professor } from \\"../../fixtures/union/types\\";
type Context = any;
export namespace UserResolvers {
Expand Down Expand Up @@ -972,8 +972,8 @@ exports[`context 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { User } from \\"../../../fixtures/context/types\\";
import { Context } from \\"../../../fixtures/context/types\\";
import { User } from \\"../../fixtures/context/types\\";
import { Context } from \\"../../fixtures/context/types\\";
export namespace QueryResolvers {
export const defaultResolvers = {};
Expand Down Expand Up @@ -1079,7 +1079,7 @@ exports[`defaultName 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { NumberNode } from \\"../../../fixtures/defaultName\\";
import { NumberNode } from \\"../../fixtures/defaultName\\";
type Context = any;
export namespace QueryResolvers {
Expand Down Expand Up @@ -1421,7 +1421,7 @@ exports[`subscription 1`] = `
"// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT.
import { GraphQLResolveInfo } from \\"graphql\\";
import { User } from \\"../../../fixtures/subscription/types\\";
import { User } from \\"../../fixtures/subscription/types\\";
type Context = any;
export namespace SubscriptionResolvers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
Message,
AuthPayload,
MutationResult
} from \\"../../../fixtures/prisma/types\\";
} from \\"../../fixtures/prisma/types\\";
type Context = any;

type PLACE_SIZES =
Expand Down
42 changes: 23 additions & 19 deletions packages/graphqlgen/src/tests/typescript/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { testGeneration } from '../generation'
import { join } from 'path'
import testConfig from './config'

const language = 'typescript'
const relative = (p: string) => join(__dirname, p)
const {
language,
relative,
resolversDir,
typesDir
} = testConfig

test('basic schema', async () => {
testGeneration({
Expand All @@ -11,9 +15,9 @@ test('basic schema', async () => {
models: {
files: [relative('../fixtures/basic/index.ts')],
},
output: relative('./generated/basic/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/basic/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand All @@ -26,9 +30,9 @@ test('basic enum', async () => {
models: {
files: [relative('../fixtures/enum/types.ts')],
},
output: relative('./generated/enum/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/enum/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand All @@ -41,9 +45,9 @@ test('basic union', async () => {
models: {
files: [relative('../fixtures/union/types.ts')],
},
output: relative('./generated/union/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/union/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand All @@ -61,9 +65,9 @@ test('defaultName', async () => {
},
],
},
output: relative('./generated/defaultName/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/scalar/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand All @@ -76,9 +80,9 @@ test('basic scalar', async () => {
models: {
files: [relative('../fixtures/scalar/types.ts')],
},
output: relative('./generated/scalar/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/scalar/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand All @@ -91,9 +95,9 @@ test('basic input', async () => {
models: {
files: [relative('../fixtures/input/types.ts')],
},
output: relative('./generated/input/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/input/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand All @@ -107,9 +111,9 @@ test('context', async () => {
models: {
files: [relative('../fixtures/context/types.ts')],
},
output: relative('./generated/context/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/input/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand All @@ -122,9 +126,9 @@ test('subscription', () => {
models: {
files: [relative('../fixtures/subscription/types.ts')],
},
output: relative('./generated/subscription/graphqlgen.ts'),
output: typesDir,
['resolver-scaffolding']: {
output: relative('./tmp/input/'),
output: resolversDir,
layout: 'file-per-type',
},
})
Expand Down
Loading

0 comments on commit 97aa601

Please sign in to comment.