Skip to content

Commit

Permalink
Merge pull request #17 from rodneylab/test__add_graphql_snapshot_testing
Browse files Browse the repository at this point in the history
test: ☑️ add snapshot testing
  • Loading branch information
rodneylab authored Nov 8, 2024
2 parents f3ed061 + 5c956c9 commit 0a36fbe
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# files listed here will be ignored by cargo watch, it also ignores .gitignore files. Other tools migth use this file. Add files to .gitignore instead, if cargo watch should ignore them and they don't need version control.
**/*.snap
**/*.snap.new
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ repository = "https://github.com/rodneylab/axum-graphql"
rust-version = "1.70.0"
description = "Rust GraphQL demo/test API written in Rust, using Axum for routing, async-graphql and SQLx."

# Faster snapshot runs
# See: https://docs.rs/insta/latest/insta/#optional-faster-runs
[profile.dev.package.insta]
opt-level = 3

[profile.dev.package.similar]
opt-level = 3

# Faster cargo check and cargo build
# See: https://github.com/launchbadge/sqlx#compile-time-verification
[profile.dev.package.sqlx-macros]
Expand Down Expand Up @@ -42,6 +50,7 @@ tracing-subscriber = { version = "0.3.18", features = ["std", "env-filter"] }

[dev-dependencies]
http-body-util = "0.1.2"
insta = { version = "1.41.1", features = ["json"] }
mime = "0.3.17"
serde_json = "1.0"
tower = { version = "0.5.1", features = ['util'] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/model/tests.rs
expression: body
snapshot_kind: text
---
b"{\"data\":{\"hello\":\"Hello everybody!\"},\"extensions\":{\"traceId\":\"00000000000000000000000000000000\"}}"
28 changes: 28 additions & 0 deletions src/model/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ mod helpers {
}
}

#[tokio::test]
async fn snapshot_hello_query() {
// arrange
let app = helpers::get_app().await;
let json_request_body: Value = json!({
"operationName":"HelloQuery",
"variables":{},
"query":"query HelloQuery { hello }"
});

// act
let response = app
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
.body(Body::from(json_request_body.to_string()))
.unwrap(),
)
.await
.unwrap();

// assert
let body = response.into_body().collect().await.unwrap().to_bytes();
insta::assert_debug_snapshot!(body);
}

#[tokio::test]
async fn graphql_endpoint_responds_to_hello_query() {
// arrange
Expand Down

0 comments on commit 0a36fbe

Please sign in to comment.