Skip to content

Commit 3f37da4

Browse files
committed
make test expectations easier to read
1 parent d3ad3c8 commit 3f37da4

File tree

6 files changed

+205
-26
lines changed

6 files changed

+205
-26
lines changed

src/core/graphql-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { stripIgnoredCharacters } from 'graphql'
22

33
export function normalizeSchema(typeDefs: string): string {
4+
// TODO sort fields and types
45
return stripIgnoredCharacters(typeDefs)
56
}

src/core/test-util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import execa from 'execa'
33
import { join } from 'path'
44
import { uid } from 'uid'
55
import jwt from 'jsonwebtoken'
6+
import { stripIgnoredCharacters } from 'graphql/utilities'
67
import { JwtPayload } from './jwt-auth'
78

89
export interface TestContext {
@@ -13,6 +14,10 @@ export interface TestContext {
1314
connectionUrl: string
1415
}
1516

17+
export function trimDoc(strings: ReadonlyArray<string>, ...values: ReadonlyArray<string>): string {
18+
return stripIgnoredCharacters(strings.join(''))
19+
}
20+
1621
export function getJwtHeader(payload: JwtPayload) {
1722
const jwtSecret = 'secret'
1823
const jwtToken = jwt.sign(payload, jwtSecret)

src/registry/federation/compose-schema-versions.test.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import anyTest, { TestInterface } from 'ava'
22
import build from '../../build-server'
33
import { CURRENT_VERSION } from '../../core/constants'
4-
import { cleanTest, createTestContext, createTestPrefix, TestContext } from '../../core/test-util'
4+
import {
5+
cleanTest,
6+
createTestContext,
7+
createTestPrefix,
8+
TestContext,
9+
trimDoc,
10+
} from '../../core/test-util'
511

612
const test = anyTest as TestInterface<TestContext>
713
test.before(createTestContext())
@@ -106,14 +112,22 @@ test('Should return schema of two services', async (t) => {
106112
t.truthy(response.data[0].lastUpdatedAt)
107113
t.like(response.data[0], {
108114
serviceName: `${t.context.testPrefix}_foo`,
109-
typeDefs: 'type Query{hello:String}',
115+
typeDefs: trimDoc/* GraphQL */ `
116+
type Query {
117+
hello: String
118+
}
119+
`,
110120
version: '2',
111121
})
112122

113123
t.truthy(response.data[1].lastUpdatedAt)
114124
t.like(response.data[1], {
115125
serviceName: `${t.context.testPrefix}_bar`,
116-
typeDefs: 'type Query{world:String}',
126+
typeDefs: trimDoc/* GraphQL */ `
127+
type Query {
128+
world: String
129+
}
130+
`,
117131
version: '2',
118132
})
119133
})
@@ -363,7 +377,11 @@ test('Version "current" should always return the latest (not versioned) register
363377

364378
t.like(response.data[0], {
365379
serviceName: `${t.context.testPrefix}_foo`,
366-
typeDefs: `type Query{world:String}`,
380+
typeDefs: trimDoc/* GraphQL */ `
381+
type Query {
382+
world: String
383+
}
384+
`,
367385
version: CURRENT_VERSION,
368386
})
369387
})
@@ -378,7 +396,11 @@ test('Should include "routingUrl" of the service', async (t) => {
378396
method: 'POST',
379397
url: '/schema/push',
380398
payload: {
381-
typeDefs: `type Query { hello: String }`,
399+
typeDefs: trimDoc/* GraphQL */ `
400+
type Query {
401+
hello: String
402+
}
403+
`,
382404
version: '1',
383405
serviceName: `${t.context.testPrefix}_foo`,
384406
routingUrl: 'http://localhost:3000/api/graphql',
@@ -410,7 +432,11 @@ test('Should include "routingUrl" of the service', async (t) => {
410432

411433
t.like(response.data[0], {
412434
serviceName: `${t.context.testPrefix}_foo`,
413-
typeDefs: 'type Query{hello:String}',
435+
typeDefs: trimDoc/* GraphQL */ `
436+
type Query {
437+
hello: String
438+
}
439+
`,
414440
routingUrl: 'http://localhost:3000/api/graphql',
415441
version: '1',
416442
})

src/registry/federation/compose-schema.test.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import anyTest, { TestInterface } from 'ava'
22
import build from '../../build-server'
33
import { CURRENT_VERSION } from '../../core/constants'
4-
import { cleanTest, createTestContext, createTestPrefix, TestContext } from '../../core/test-util'
4+
import {
5+
cleanTest,
6+
createTestContext,
7+
createTestPrefix,
8+
TestContext,
9+
trimDoc,
10+
} from '../../core/test-util'
511

612
const test = anyTest as TestInterface<TestContext>
713
test.before(createTestContext())
@@ -63,14 +69,22 @@ test('Should return schema of two services', async (t) => {
6369
t.truthy(response.data[0].lastUpdatedAt)
6470
t.like(response.data[0], {
6571
serviceName: `${t.context.testPrefix}_bar`,
66-
typeDefs: 'type Query{world:String}',
72+
typeDefs: trimDoc/* GraphQL */ `
73+
type Query {
74+
world: String
75+
}
76+
`,
6777
version: '2',
6878
})
6979

7080
t.truthy(response.data[1].lastUpdatedAt)
7181
t.like(response.data[1], {
7282
serviceName: `${t.context.testPrefix}_foo`,
73-
typeDefs: `type Query{hello:String}`,
83+
typeDefs: trimDoc/* GraphQL */ `
84+
type Query {
85+
hello: String
86+
}
87+
`,
7488
version: '1',
7589
})
7690
})
@@ -152,7 +166,11 @@ test('Version "current" has no precedence over the last updated', async (t) => {
152166

153167
t.like(response.data[0], {
154168
serviceName: `${t.context.testPrefix}_foo`,
155-
typeDefs: 'type Query{world:String}',
169+
typeDefs: trimDoc/* GraphQL */ `
170+
type Query {
171+
world: String
172+
}
173+
`,
156174
version: '2',
157175
})
158176
})
@@ -197,7 +215,11 @@ test('Should include "routingUrl" of the service', async (t) => {
197215

198216
t.like(response.data[0], {
199217
serviceName: `${t.context.testPrefix}_foo`,
200-
typeDefs: 'type Query{hello:String}',
218+
typeDefs: trimDoc/* GraphQL */ `
219+
type Query {
220+
hello: String
221+
}
222+
`,
201223
routingUrl: 'http://localhost:3000/api/graphql',
202224
version: '1',
203225
})

0 commit comments

Comments
 (0)