Skip to content

Commit

Permalink
refactor: extracting testing types into folder
Browse files Browse the repository at this point in the history
- updating test folder to match setup of internal ‘plugin’ library
  • Loading branch information
haysclark committed Sep 15, 2020
1 parent 25235c1 commit f0129d2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/lib/plugin/types/lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Logger from '@nexus/logger'
import * as NexusSchema from '@nexus/schema'
import * as Prompts from 'prompts'
import * as Scalars from '../../scalars'
import * as Testing from '../../../testing/testing'
import * as Testing from '../../../testing/types'
import * as Layout from '../../layout'
import * as PackageManager from '../../package-manager'
import * as Process from '../../process'
Expand Down
3 changes: 2 additions & 1 deletion src/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { createTestContext, TestContext } from './testing'
export * from './types'
export { createTestContext } from './testing'
44 changes: 5 additions & 39 deletions src/testing/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,10 @@ import { rightOrFatal } from '../lib/utils'
import app from '../runtime'
import { PrivateApp } from '../runtime/app'
import { createDevAppRunner } from '../runtime/start'
import { CreateTestContextOptions, TestContext, TestContextCore } from './types'

const pluginLogger = rootLogger.child('plugin')

export interface TestContextAppCore {
start(): Promise<void>
stop(): Promise<void>
}

export interface TestContextCore {
app: TestContextAppCore
client: GraphQLClient
}

declare global {
interface NexusTestContextApp extends TestContextAppCore { }

interface NexusTestContextRoot {
app: NexusTestContextApp
client: GraphQLClient
}
}

export type TestContext = NexusTestContextRoot

export interface CreateTestContextOptions {
/**
* A path to the entrypoint of your app. Only necessary if the entrypoint falls outside of Nexus conventions.
* You should typically use this if you're using `nexus dev --entrypoint` or `nexus build --entrypoint`.
*/
entrypointPath?: string
/**
* Nexus usually determines the project root by the first `package.json` found while traversing up the file system.
* In some cases, e.g. usage in a monorepo, this might not always be correct.
* For those cases, you can specify the `projectRoot` manually.
*
* Example: `await createTestContext({ projectRoot: path.join(__dirname, '../..') })`
*/
projectRoot?: string
}

/**
* Setup a test context providing utilities to query against your GraphQL API
*
Expand All @@ -76,15 +40,17 @@ export async function createTestContext(opts?: CreateTestContextOptions): Promis
process.env.NEXUS_STAGE = 'dev'

// todo figure out some caching system here, e.g. imagine jest --watch mode
const layout = rightOrFatal(await Layout.create({ entrypointPath: opts?.entrypointPath, projectRoot: opts?.projectRoot }))
const layout = rightOrFatal(
await Layout.create({ entrypointPath: opts?.entrypointPath, projectRoot: opts?.projectRoot })
)
const pluginManifests = await PluginWorktime.getUsedPlugins(layout)
const randomPort = await getPort({ port: getPort.makeRange(4000, 6000) })
const privateApp = app as PrivateApp

const forcedServerSettings = {
port: randomPort,
playground: false, // Disable playground during tests
startMessage() { }, // Make server silent
startMessage() {}, // Make server silent
}

// todo remove these settings hacks once we have https://github.com/graphql-nexus/nexus/issues/758
Expand Down
1 change: 1 addition & 0 deletions src/testing/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './test-context'
38 changes: 38 additions & 0 deletions src/testing/types/test-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { GraphQLClient } from '../../lib/graphql-client'

export interface TestContextAppCore {
start(): Promise<void>
stop(): Promise<void>
}

export interface TestContextCore {
app: TestContextAppCore
client: GraphQLClient
}

declare global {
interface NexusTestContextApp extends TestContextAppCore {}

interface NexusTestContextRoot {
app: NexusTestContextApp
client: GraphQLClient
}
}

export type TestContext = NexusTestContextRoot

export interface CreateTestContextOptions {
/**
* A path to the entrypoint of your app. Only necessary if the entrypoint falls outside of Nexus conventions.
* You should typically use this if you're using `nexus dev --entrypoint` or `nexus build --entrypoint`.
*/
entrypointPath?: string
/**
* Nexus usually determines the project root by the first `package.json` found while traversing up the file system.
* In some cases, e.g. usage in a monorepo, this might not always be correct.
* For those cases, you can specify the `projectRoot` manually.
*
* Example: `await createTestContext({ projectRoot: path.join(__dirname, '../..') })`
*/
projectRoot?: string
}

0 comments on commit f0129d2

Please sign in to comment.