Skip to content

Commit 127bbbf

Browse files
committed
Add assertions for errors
1 parent 7b1c1c4 commit 127bbbf

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/main/kotlin/com/symbaloo/graphql/test/GraphQLKotlinTestDsl.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ fun GraphQLResultMatcherDsl.noErrors() {
153153
}
154154
}
155155

156+
/**
157+
* Assert that there is at least one error
158+
*/
159+
fun GraphQLResultMatcherDsl.hasError(matcher: GraphQLErrorResultMatcher.() -> Unit = { }) {
160+
val errors = this.executionResult.errors
161+
if (errors.isEmpty()) {
162+
fail(
163+
"""
164+
|Expected at least one error in the result. But there were no errors.
165+
|""".trimMargin(),
166+
null,
167+
errors
168+
)
169+
} else {
170+
GraphQLErrorResultMatcher(errors).matcher()
171+
}
172+
}
173+
156174
/**
157175
* Assert that some field has some value
158176
*/
@@ -277,6 +295,16 @@ internal class JsonPathContext(
277295
}
278296
}
279297

298+
class GraphQLErrorResultMatcher(internal val errors: List<GraphQLError>)
299+
300+
fun <R> GraphQLErrorResultMatcher.path(path: String, matcher: (GraphQLError) -> R) {
301+
val parts = path.split(".")
302+
when (val error = errors.find { it.path == parts }) {
303+
null -> fail("Error with path '$path' couldn't be found", path, errors)
304+
else -> matcher(error)
305+
}
306+
}
307+
280308
private fun fail(message: String, expected: Any?, value: Any?) {
281309
throw AssertionFailedError(
282310
"expected: <$expected> but was: <$value>",

src/test/kotlin/com/symbaloo/graphql/test/GraphQLKotlinTestDslTest.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import assertk.assertions.isNull
1313
import assertk.assertions.key
1414
import assertk.assertions.prop
1515
import assertk.assertions.startsWith
16+
import graphql.ExceptionWhileDataFetching
1617
import graphql.ExecutionResult
1718
import graphql.GraphQL
1819
import graphql.schema.idl.RuntimeWiring.newRuntimeWiring
@@ -33,6 +34,7 @@ private val schema = """
3334
| echo(echo: String): String
3435
| fromContext: String
3536
| list: [Bar!]!
37+
| error: Int
3638
|}
3739
|type Bar {
3840
| hello: String
@@ -52,6 +54,7 @@ class GraphQLKotlinTestDslTest {
5254
builder.dataFetcher("echo") { it.arguments["echo"] }
5355
builder.dataFetcher("fromContext") { it.getContext<Bar>().foo }
5456
builder.dataFetcher("list") { listOf(Bar("first"), Bar("second")) }
57+
builder.dataFetcher("error") { throw Exception("oops") }
5558
}
5659
.type("Bar") { builder ->
5760
builder.dataFetcher("hello") { it.getSource<Bar>().foo }
@@ -375,6 +378,18 @@ class GraphQLKotlinTestDslTest {
375378
assertThat(result).isEqualTo(10)
376379
}
377380
}
381+
382+
@Test
383+
fun noErrors() {
384+
assertThat {
385+
graphQLTest(createTestSchema()) { query("{ error }") }.andExpect { noErrors() }
386+
}.isFailure()
387+
}
388+
389+
@Test
390+
fun hasError() {
391+
graphQLTest(createTestSchema()) { query("{ error }") }.andExpect { hasError() }
392+
}
378393
}
379394

380395
@Nested
@@ -430,6 +445,41 @@ class GraphQLKotlinTestDslTest {
430445
}
431446
}
432447
}
448+
449+
@Nested
450+
inner class GraphQLErrorResultMatcher {
451+
452+
@Test
453+
fun `assert error path`() {
454+
graphQLTest(createTestSchema()) {
455+
query("{ error }")
456+
}
457+
.andExpect {
458+
hasError {
459+
path("error") { error ->
460+
assertThat(error)
461+
.isInstanceOf(ExceptionWhileDataFetching::class)
462+
.transform { it.exception.message }
463+
.isEqualTo("oops")
464+
}
465+
}
466+
}
467+
}
468+
469+
@Test
470+
fun `assert error of non-existing path`() {
471+
assertThat {
472+
graphQLTest(createTestSchema()) {
473+
query("{ error }")
474+
}
475+
.andExpect {
476+
hasError {
477+
path("error.bar") { }
478+
}
479+
}
480+
}.isFailure()
481+
}
482+
}
433483
}
434484

435485
private fun GraphQLJsonResultMatcherDsl.assertPath(path: String): Assert<Any?> =

0 commit comments

Comments
 (0)